public class DbUtil{ public Connection getConn() throws Exception{ Class.forName("com.mysql.jbdc.Driver"); Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/emp_dept", "root", "111111"); return conn; }
public void closeConn(Connection conn) throws Exception { if(conn!=null){ conn.close(); } } public static void main(String[] args) throws Exception { DbUtil dbUtil=new DbUtil(); Connection conn=dbUtil.getConn(); if(conn!=null){ System.out.println("数据库连接成功"); }else{ System.out.println("数据库连接失败"); } dbUtil.closeConn(conn); }