测试环境说明
mysql数据库:jdbc:mysql://localhost:3306/test
IDE:IDEA 2022
JDK:JDK8
mysql:mysq 5.7
JDBC:5.1.37
第一种方式
使用静态加载驱动方式,连接mysql
这种方式灵活性差,依赖性强
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public void connection01() throws SQLException { Driver driver = new Driver(); Properties properties = new Properties(); properties.setProperty("user", "root"); properties.setProperty("password", "123456"); String url = "jdbc:mysql://localhost:3306/test"; Connection connection = driver.connect(url, properties); Statement statement = connection.createStatement(); statement.close(); connection.close();
|
第二种方式
在第一种方式的基础上使用反射动态加载驱动,依赖性减小、灵活性提高
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public void connection02() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { Class<?> aClass = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) aClass.newInstance(); Properties properties = new Properties(); properties.setProperty("user", "root"); properties.setProperty("password", "123456"); String url = "jdbc:mysql://localhost:3306/test"; Connection connection = driver.connect(url, properties); Statement statement = connection.createStatement(); statement.close(); connection.close(); }
|
第三种方式
使用DriverManager统一进行管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public void connection03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException { Class<?> aClass = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) aClass.newInstance(); String user = "root"; String password = "123456"; String url = "jdbc:mysql://localhost:3306/test"; DriverManager.registerDriver(driver); Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(); statement.close(); connection.close(); }
|
第四种方式
其实Class.forName(“com.mysql.jdbc.Driver”)在底层已经自动加载好了Driver实例
所以Driver driver = (Driver) aClass.newInstance();这句话可以省略
这种方式也是开发中使用最多的一种方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public void connection04() throws ClassNotFoundException, SQLException { Class<?> aClass = Class.forName("com.mysql.jdbc.Driver"); String user = "root"; String password = "123456"; String url = "jdbc:mysql://localhost:3306/test"; Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(); statement.close(); connection.close(); }
|
第五种方式
mysql5.16后可以不用Class.forName(“com.mysql.jdbc.Driver”);来加载驱动了
从jdk1.5以后使用了jdbc4,不再需要显示调用class.forName()注册驱动而是自动调用驱动jar包下META-INF\services\java.sql.Driver文本中的类名称去注册
建议还是写上 CLass . forName(“com.mysql.jdbc.Driver”),更加明确,兼容性更好
这里同时使用properties配置文件实现动态信息动态读取,灵活性得到提升
推荐使用这种方式
src/com/mysql/mysql.properties配置文件内容如下
1 2 3
| url=jdbc:mysql://localhost:3306/test user=root password=123456
|
连接mysql程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public void connection05() throws SQLException, ClassNotFoundException, IOException { Properties properties = new Properties(); properties.load(new FileInputStream("src/com/mysql/mysql.properties")); String url = properties.getProperty("url"); String user = properties.getProperty("user"); String password = properties.getProperty("password"); Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(); statement.close(); connection.close(); }
|