java实现JDBC毗连MySql及引用驱动措施包
当前位置:以往代写 > JAVA 教程 >java实现JDBC毗连MySql及引用驱动措施包
2019-06-14

java实现JDBC毗连MySql及引用驱动措施包

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectToDataBase {
	public static Connection getDataBaseConnection(){
		String driver="com.mysql.jdbc.Driver";
		String url="jdbc:mysql://localhost:3306/db_pms";
		String user="root";
		String password="123456";
		
		//加载驱动措施
		try {
			Class.forName(driver);
			//URL:http://www.bianceng.cn/Programming/Java/201608/50376.htm
			//System.out.println("驱动措施加载乐成");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		//链接数据库
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(url, user, password);
			if (!conn.isClosed()) {
				System.out.println("毗连数据库乐成");
			}
		} catch (SQLException e) {
			System.out.println("链接数据库失败: "+e.getMessage());
		}
		
		return conn;
	}
	
}
   Connection  conn=ConnectToDataBase.getDataBaseConnection();

    关键字:

在线提交作业