1.需要在链接处添加
static { //1.加载驱动 Properties properties = new Properties(); try { properties.load(new FileInputStream("src\\JDBC.properties")); } catch (IOException e) { throw new RuntimeException(e); } String user=properties.getProperty("user"); String password = properties.getProperty("password"); String driver = properties.getProperty("driver"); String url = properties.getProperty("url"); try { Class.forName(driver); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } //2.获取链接 try { conn = DriverManager.getConnection(url, user, password); statement = conn.createStatement(); } catch (SQLException e) { throw new RuntimeException(e); } }
2.配置文件只需要在这里更改就可达到链接的情况
user=root password=123456 url=jdbc:mysql://localhost:3306/2411 driver=com.mysql.cj.jdbc.Driver