public class Connector { private static Connection connection = null; private static String driverName = "com.mysql.jdbc.Driver"; private static String url="jdbc:mysql://localhost:3306/carttest"; private static String username="root"; private static String password="***********"; public static Connection getConnection(){ try { Class.forName(driverName); connection= DriverManager.getConnection(url,username,password); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("找不到驱动类,加载失败"); } catch (SQLException e) { e.printStackTrace(); System.out.println("连接数据库失败!"); } return connection; } public static boolean closeConnection(Connection connection){ try { connection.close(); } catch (SQLException e) { e.printStackTrace(); return false; } return true; } }
java连接mysql数据库
最新推荐文章于 2022-11-16 19:26:46 发布