Inside JDBC (1)
Now the Java application development, database operations related to time, we often think first of all are the ORM like Hibernate framework. More and more Java developers less willing to study in depth the use of, or JDBC, or in fact eventually want ORM framework of the use of JDBC. In this article, I will introduce more comprehensive basic knowledge of JDBC. This article is based on a database that the mainstream-oriented relational database.
Number of database products, such as Boeing's level Oracle, BMW every level of the MySQL database products, whether commercial or open-source database database has a set of its own API for external procedures and their interaction. If the developers face a database for each product required to learn a new API, it would be the one thing reluctantly, often because the cost of study is relatively high. Learn Java, when everyone always want to remember a word, "chaos will occur when the standard." Sun Microsystems in order to resolve this problem, by the Microsoft ODBC standard inspired the definition of the JDBC specification, up to now, JDBC has been developed to the 4.0 version. JDBC (Java DataBase Connectivity) to access the database defines the Java standards, mainly in the form of a set of interfaces defined in the java.sql package and are javax.sql. The so-called JDBC Driver is JDBC database vendors standardized on the Java implementation, packaged into a jar archive, the database vendor's official website has many downloads. There are four types of JDBC driver, this article has spent most of the Type 4 as an example. JDK release includes JDBC-ODBC Bridge driver, the majority of this type is used to access the MS Access and Excel, not to introduce this article.
With JDBC, after, Java developers only need to learn JDBC API can, Does not need to consider differences in the underlying database product. Such as occurred in the case of replacement of database products, from SQL Server to change to Oracle, we can only JDBC driver will need to be replaced. Assumptions do not use the Product-specific SQL database, the SQL Server such as the top keywords, JDBC code almost without changes, to the effect of cross-database platform.
The situation is more complex, a Java application needs to access multiple database products, such as the need to simultaneously access MySQL and Oracle. That is necessary both database-driven, the driver who will manage the money? The answer is driven manager that category java.sql.DriverManager. DriverManager has one static method static void registerDriver (java.sql.Driver driver), this method has been applied to register the driver needs to be managed (Driver). java.sql.Driver
Are one interface, the definition of a very important way: java.sql.Connection connect (String url, java.util.Properties info) This method return the application to operate a database Java database connection required. Url parameter specifies the "connection string", format: jdbc: subprotocol: subname. For example "jdbc: mysql: / / localhost: 3306/mydb", info parameters for additional transfer information, such as user names, passwords and so on. Implementation as a database-driven, such as MySQL's Connector / J, provided the driver implementation must implement the Driver interface, such as on the implementation of the com.mysql.jdbc.Driver category java.sql.Driver interface, one of us a very interesting point of view code (from the MySQL driver source code):
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
//静态初始化块
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
//...
}
As we all know, a static initialization block in the Java virtual machine class loader (JVM ClassLoader) to load the corresponding implementation class initialization time. That is to say in the implementation of Class.forName ( "com.mysql.jdbc.Driver") when carried out within the com.mysql.jdbc.Driver static initialization block. This static initialization block has only one thing, and put their own (new Driver ()) to register DriverManager. DriverManager at java.util.Vector internal storage register of the drive. If you are interested, you can decompile the implementation of Microsoft SQL Server driver (not open source, so to decompile) also has a static initialization block for the register. Now our point of view a complete program:
//使用的MySQL驱动版本为Connector/J-5.1.7
package com.wwei.jdbc;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
public class DirverInfo {
public static void main(String[] args)throws Exception {
Class.forName("com.mysql.jdbc.Driver");//加载驱动并注册驱动
Driver driver = DriverManager.getDriver("jdbc:mysql://");//通过url获得驱动
System.out.println(driver.getMajorVersion());//驱动主版本 5
System.out.println(driver.getMinorVersion());//驱动次版本 1
//获得驱动支持的特性,输出的名值对大家可以对照Connector/J文档查阅其含义。
DriverPropertyInfo [] dpis = driver.getPropertyInfo("jdbc:mysql://localhost/test", null);
for(DriverPropertyInfo info : dpis){
System.out.println(info.name + " = " + info.value);
}
}
} Next, we look at another important DriverManager Methods:
public static Connection getConnection (String url, String user, String password)
This method has the following implementation of a few internal code (from JDK src.zip):
Connection result = di.driver.connect(url, info);
if (result != null) {
// Success!
println("getConnection returning " + di);
return (result);
}
di.driver.connect (url, info) is a connect interface java.sql.Driver the above-mentioned methods, the specific implementation at the com.mysql.jdbc.Driver class.
More about the DriverManager methods, everyone can access Java documentation, will be presented under a specific use of JDBC.
Tags: database products (RSS), java database (RSS), sun microsystems (RSS), jdbc specification (RSS), database operations (RSS), java developers (RSS), development database (RSS), java implementation (RSS), database product (RSS), java application development (RSS), open source database (RSS), odbc bridge (RSS), microsoft odbc (RSS), jar archive (RSS), java standards (RSS), database number (RSS), database vendor (RSS), database vendors (RSS), learn java (RSS), database database (RSS)
Permalink: http://www.codeweblog.com/inside-jdbc-1/





















