当前位置:天才代写 > tutorial > JAVA 教程 > java实现JDBC毗连MySql及引用驱动措施包

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

2017-11-02 08:00 星期四 所属: JAVA 教程 浏览:412

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();

 

    关键字:

天才代写-代写联系方式