ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
JDBC.java ~~~ package zyw.tools; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JDBC { private static String driver;//快捷键Ctrl+Art+f private static String url; private static String username; private static String password; static { try { ClassLoader classLoader = JDBC.class.getClassLoader(); InputStream resourceAsStream = classLoader.getResourceAsStream("db.properties"); Properties properties=new Properties(); properties.load(resourceAsStream); driver = properties.getProperty("driver"); url = properties.getProperty("url"); username = properties.getProperty("username"); password = properties.getProperty("password"); } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection() { Connection connection = null; //注册驱动,获取连接 try { /* Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://122.14.200.136:3306/javadb"; connection = DriverManager.getConnection(url, "root", "6a133f0024");*/ Class.forName(driver); connection = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return connection; } public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) { //释放资源 if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ~~~ 调用方法 ~~~ Connection connection =JDBC.getConnection(); JDBC.release(connection,preparedStatement,resultSet); ~~~ db.properties文件放在src目录下 ~~~ driver=com.mysql.jdbc.Driver url=jdbc:mysql://122.14.200.136:3306/javadb username=root password=xxx ~~~