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 initialization block
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:
// Use the MySQL driver version for 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");// Loaded drivers and register the driver
Driver driver = DriverManager.getDriver("jdbc:mysql://");// Driven by URL
System.out.println(driver.getMajorVersion());// Driver major version 5
System.out.println(driver.getMinorVersion());// Driving time version 1
// Get driven support features, the output of name-value pair members can control Connector /J Document access to its meaning.
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.
Related Posts of Inside JDBC (1)
-
Pring Beanfactory at the jsp, servlet, web.xml and other configuration
Keywords: spring beanfactory Spring in the web application called the Beanfactory 1) Configure web.xml Java code <? xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE web-app PUBLIC "- / / Sun Microsystems, Inc. / / ...
-
ActiveMQ practice the road (four) ActiveMQ 4.x + JBoss 4.x MDP actual articles
Keyword: ActiveMQ ActiveMQ practice the road (four) ActiveMQ 4.x + JBoss 4.x MDP actual articles At <<ActiveMQ Practice ( Three ) ActiveMQ 4.x +JBoss 4.x Consolidating articles >> which we compare in detail the ActiveMQ with JBoss integra ...
-
jBPM Development Getting Started Guide
Although the workflow is still immature stage of development, not even a recognized standard. But its application has already been launched in the Express, indicating the market's demand for job-flow framework are urgent and enormous. Backgrounds of o
-
JDBC driver types
1, JDBC is a included in the J2SE and J2EE platform API, you have access to a variety of data sources, in particular, are so typical as Oracle relational database management system. Sun Microsystems Inc. in January 1997 the introduction of JDBC techn ...
-
Java technology 25 study points
1. You need to master the object-oriented analysis and design (OOA / OOD), involving patterns (GOF, J2EEDP) as well as the integrated model. You should understand the UML, especially class, object, interaction and statediagrams. 2. You need to learn basic
-
Process migration from tomcat to websphere changes
Process migration from tomcat to websphere changes Because customers use the web application server software used by different what tomcat5, tomcat6, websphere5.1, websphere6.1, weblogic8, and so on, and the software used inconsistent standards, ibm& ...
-
Java Technology wishing cow needed 25 points of study
1. You need to master the object-oriented analysis and design (OOA / OOD), involving patterns (GOF, J2EEDP) as well as the integrated model. You should understand the UML, especially class, object, interaction and statediagrams. 2. You need to learn basic













Leave a Reply