The first way: encapsulate the username and password in the Properties class
First of all, import the database connection package this is unquestionable. Create a jdbc driver dirver. save the url of the database (MySQL for example) in the created string url. If mysql version is lower than 8.0, the url save form should be:
String url = "jdbc:mysql://localhost:3306/test"
If the mysql version is version 8.0 or above, the url is saved as:
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
I'm using mysql version 8.0, so I added the timezone after it, otherwise it defaults to UTC timezone, which is 8 hours behind Beijing time.
Then the mysql database corresponding to the user and password encapsulated in the Properties class, and finally through the Connection class to create a database connection, the source code is as follows:
Driver driver = new (); String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; /* Encapsulate username and password in Properties */ Properties info = new Properties(); ("user","root"); ("password","ab20010322"); Connection conn = (url,info); (conn);
The second way: on the basis of way one, using reflection to realize the driver
Will be in the first way:
Driver driver = new ();
Replace with the following:
Class clazz = (""); Driver driver = (Driver) ();
Relative to the first way, the two implementations of the same function, but the second way to use reflection to realize the driver, which can avoid the use of third-party interfaces, so that the code has a better portability. The source code of the second way is as follows:
/* Using reflection to get an instance of the Driver class and Driver driver = new () function is the same, but does not apply to third-party interfaces, so that the program has a better portability */ Class clazz = (""); Driver driver = (Driver) (); /* Provide the database to connect to */ String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; /* Provide the required username and password */ Properties info = new Properties(); ("user","root"); ("password","ab20010322"); Connection connection = (url,info); (connection);
Third way: use DriveManager (classess) instead of Drive
The source code is below:
Class clazz = (""); Driver driver = (Driver) (); /* Provide connection information */ String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "ab20010322"; /* Registering the driver */ (driver); /* Get Connection */ ((url,user,password));
Fourth way: hide the way the driver is loaded
Integration of modality III
Driver driver = (Driver) (); (driver);
change to
("");
This way, you can hide the driver loading situation
The source code is below:
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai"; String user = "root"; String password = "ab20010322"; /* Loading Driver */ (""); // Driver driver = (Driver) (); // (driver); /* Get Connection */ ((url,user,password));
Way 5: Put the basic information needed for the database in the configuration file
For all four of the above connections, the database information is laid bare. This is not safe. For this, we should put the basic information needed for the database in the configuration file and then read it out through InputStream. This is the safe and most common way to connect to the database.
The configuration file is as follows:
user=root password=123456 url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai Driver=
Attention:
1. Configuration files should be placed in the src folder
and password to use your own database username and password.
3. If you are using mysql 8.0 or above, you should add the time zone to the end of the url file when configuring it, otherwise it will report an error.
The source code is below:
InputStream inputStream = ().getResourceAsStream(""); Properties info = new Properties(); (inputStream); String user = ("user"); String password = ("password"); String url = ("url"); String driver = ("Driver"); /* Load Driver */ (driver); /* Get Connection */ Connection conn = (url,user,password); (conn);
to this article on the details of this java connection mysql database of the five ways of the article is introduced to this, more related java connection mysql content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!