Home>Article>Java> How many ways does java have to connect to mysql? What are the differences?

How many ways does java have to connect to mysql? What are the differences?

王林
王林 forward
2020-11-23 15:33:31 2573browse

How many ways does java have to connect to mysql? What are the differences?

There are five ways to connect java to mysql, which are:

(Learning video sharing:java teaching video)

The first way: encapsulate the username and password in the Properties class

First of all, there is no doubt about importing the database connection package. Create a jdbc driver dirver. Save the url of the database (taking MySQL as an example) in the created string url. If the mysql version is lower than 8.0, the url saving form should be:

String url = "jdbc:mysql://localhost:3306/test"

If the mysql version is 8.0 or above, the url saving form is:

String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";

The mysql version used here is 8.0, So the time zone is added at the end, otherwise the default is UTC time zone, which is 8 hours later than Beijing time.

Then encapsulate the user and password corresponding to the mysql database in the Properties class, and finally create the database connection through the Connection class. The source code is as follows:

Driver driver = new com.mysql.jdbc.Driver(); String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; /* 将用户名和密码封装在Properties中 */ Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","ab20010322"); Connection conn = driver.connect(url,info); System.out.println(conn);

The second method: in Based on the first method, use reflection to realize the driver

. Change the first method:

Driver driver = new com.mysql.jdbc.Driver();

to:

Class clazz = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance();

relative to the first method. Two methods have the same functions, but the second method uses reflection to implement the driver, which avoids the use of third-party interfaces and makes the code more portable. The source code of the second method is as follows:

/* 使用反射获取Driver类实例 与Driver driver = new com.mysql.jdbc.Driver()功能相同,只是不适用第三方接口,使得程序具有更好的可移植性 */ Class clazz = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance(); /* 提供要连接的数据库 */ String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; /* 提供需要的用户名和密码 */ Properties info = new Properties(); info.setProperty("user","root"); info.setProperty("password","ab20010322"); Connection connection = driver.connect(url,info); System.out.println(connection);

The third method: Use DriveManager(classs) instead of Drive

The source code is as follows:

Class clazz = Class.forName("com.mysql.jdbc.Driver"); Driver driver = (Driver) clazz.newInstance(); /* 提供连接信息 */ String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "ab20010322"; /* 注册驱动 */ DriverManager.registerDriver(driver); /* 获取连接 */ System.out.println(DriverManager.getConnection(url,user,password));

The fourth method: Hide the driver loading method

Change the

Driver driver = (Driver) clazz.newInstance(); DriverManager.registerDriver(driver);

in the third method to

Class.forName("com.mysql.jdbc.Driver");

In this way, you can change the driver loading status Hidden

The source code is as follows:

String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "ab20010322"; /* 加载Driver */ Class.forName("com.mysql.jdbc.Driver");// Driver driver = (Driver) clazz.newInstance();// DriverManager.registerDriver(driver); /* 获取连接 */ System.out.println(DriverManager.getConnection(url,user,password));

Method 5: Put the basic information required by the database in the jdbc.properties configuration file

For the above four connection methods , all exposing database information. It is not safe to do so. In this regard, we should put the basic information needed by the database in the jdbc.properties configuration file, and then read it out through InputStream. This is safe and our most commonly used database connection method
The configuration file jdbc.properties is as follows:

user=root password=123456url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai Driver=com.mysql.cj.jdbc.Driver

Note:

1. The configuration file should be placed in the src folder Next

2. Use your own database username and password for user and password

3. If you are using mysql8.0 or above, you should add the time zone at the end when configuring the url file, otherwise An error will be reported

The source code is as follows:

InputStream inputStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties"); Properties info = new Properties(); info.load(inputStream); String user = info.getProperty("user"); String password = info.getProperty("password"); String url = info.getProperty("url"); String driver = info.getProperty("Driver"); /* 加载驱动 */ Class.forName(driver); /* 获取连接 */ Connection conn = DriverManager.getConnection(url,user,password); System.out.println(conn);

Related recommendations:java introductory tutorial

The above is the detailed content of How many ways does java have to connect to mysql? What are the differences?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete