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