environment
- Ubuntu 22.04
- IntelliJ IDEA 2022.1.3
- JDK 17.0.3
- Db2 v11.5.0.0
- MySQL Ver 8.0.30
Prepare
Db2
In Db2sample
Create tables in the databaset1
and insert some data. as follows:
➜ ~ db2 "select * from t1" C1 C2 C3 ----------- ----------- ----------- 1 444 - 2 222 - 3 333 - 3 record(s) selected.
MySQL
In MySQLrepo
Create tables in the databaset1
and insert some data. as follows:
mysql> select * from t1; +------+-------+ | c1 | c2 | +------+-------+ | 1 | 9800 | | 2 | 10200 | +------+-------+ 2 rows in set (0.00 sec)
Code
Create a Maven projecttest0924
。
ReviseFile, add dependencies:
...... <!-- /artifact/.db2/jcc --> <dependency> <groupId>.db2</groupId> <artifactId>jcc</artifactId> <version>11.5.7.0</version> </dependency> <!-- /artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> ......
As mentioned above, the JDBC drivers of Db2 and MySQL were added to the project.
Db2
Create a classTest0924_Db2
:
package pkg1; import .*; public class Test0924_Db2 { public static void main(String[] args) throws ClassNotFoundException { // ("..DB2Driver"); try ( Connection connection = ("jdbc:db2://localhost:50000/sample", "db2inst1", "passw0rd"); Statement stmt = (); ResultSet rs = ("select * from t1"); ) { // (().getName()); while (()) { ((1)); } } catch (SQLException e) { throw new RuntimeException(e); } } }
Run the program, the result is as follows:
1
2
3
MySQL
Create a classTest0924_Mysql
:
package pkg1; import .*; public class Test0924_Mysql { public static void main(String[] args) throws ClassNotFoundException { // (""); try ( Connection connection = ("jdbc:mysql://localhost:3306/repo", "root", "123456"); Statement stmt = (); ResultSet rs = ("select * from t1"); ) { // (().getName()); while (()) { ((1)); } } catch (SQLException e) { throw new RuntimeException(e); } } }
Run the program, the result is as follows:
1
2
analyze
JDBC
Comparing two Java files is visible. The way to connect Db2 and MySQL is very similar. The only difference is that the call()
When the method is used, the URL passed in is different:
- Db2:
jdbc:db2://localhost:50000/sample
- MySQL:
jdbc:mysql://localhost:3306/repo
To be more precise, it's just the difference in the protocol:db2
VS. mysql
。
We know that JDBC is a set of standards, and each manufacturer has its own implementation, that is, its own JDBC driver. This is why we first introduced the JDBC drivers of Db2 and MySQL from the beginning.
Several important classes in JDBC:
Notice:Connection
、 Statement
、 ResultSet
All need to be closed, one way is tofinally
It is explicitly called in the blockclose()
method. In this example, the Java 7 introduced is usedtry()
Blocks automatically release resources (they all implementAutoCloseable
interface).
()
When we were studying JDBC, we were told that the first step was to use it first.()
Method, import specific JDBC driver.
However, through the two examples in this article, we see that even if this step is omitted, there is no problem, and DriverManager can automatically find the appropriate driver.
Then the question is:
- Call
()
Method, what exactly did it do? - Why is there no problem not calling this method in this article?
We know that if a class has not been used before, then()
Methods, which will do several things, including instantiating the Class object of the class, executing its static block, etc.
For JDBC driver, take Db2 driver as an example, check..DB2Driver
Class, you can find the following code:
static { (); try { registeredDriver__ = new DB2Driver(); (registeredDriver__); } catch (SQLException var1) { = ((, (ds)null, ErrorKey.ERROR_REGISTER_WITH_DRIVER_MGR, "10032"), ); } }
It can be seen that the()
The method registers the driver of Db2.
Similarly, for MySQL, its driver class(yes
The parent class of the class contains the following code:
static { try { (new Driver()); } catch (SQLException var1) { throw new RuntimeException("Can't register driver!"); } }
It can be seen that similarly, it was also called()
Method registered MySQL driver.
From this we know that the call()
The method is to load the driver, and its function is to register the driver.
So why is there no problem in this article not calling the method?
It is an interface, we print
().getName()
Let’s take a look at the specific class name (see the comments section in the code).
- Db2:
..
- MySQL:
Visible, even if not passed()
Methods to explicitly register the specified driver and call it directly()
Methods can also obtain the correct database connection according to the incoming URL.
You can check the source code of DriverManager, roughly as follows:
...... for (DriverInfo aDriver : registeredDrivers) { // If the caller does not have permission to load the driver then // skip it. if (isDriverAllowed(, callerCL)) { try { println(" trying " + ().getName()); Connection con = (url, info); if (con != null) { // Success! println("getConnection returning " + ().getName()); return (con); } } catch (SQLException ex) { if (reason == null) { reason = ex; } } ......
That is to say, it will create a list of drivers, then iterate over the list, and try to use the current driver to connect to the database according to the incoming URL. If it can be connected, it will be OK, otherwise try the next driver.
Of course, if called()
If the method explicitly registers the driver, it will put the driver class in the first place in the list and use it first to connect to the database.
This is all about this article about JDBC's () issue. For more information about JDBC, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!