At Inside JDBC (1), the introduction to the U.S. Class DriverManager interface with the Driver of the meaning and usage, from the beginning of this article, we take a look at the specific use of JDBC.
Want to operate the database, first set up the database connection (Connection), the specific function of the database connection defined in the java.sql.Connection interface. We first take a look at how to establish a connection.
Driver interface on the study and know its connect method is used to set up database connections, complete code is as follows:

package com.wwei.jdbc;
import java.sql.Connection;
import java.sql.Driver;
import java.util.Properties;

public class Conn_1 {

    public static void main(String[] args) throws Exception{

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

        String url = "jdbc:mysql://localhost/test";
        Properties info = new Properties();//连接信息
        info.put("user", "root");
        info.put("password", "wwei");
        Connection con = driver.connect(url, info);//建立连接,成功返回连接实例,否则返回null。

        if(con != null){
            System.out.println("建立连接");
        }   
       if(con != null && !con.isClosed()){
            con.close();
            System.out.println("连接已断开");
        }
    }
} 


Connection example of the use of the end, remember to turn off. If the connection pool has to return to the situation in connection pool. In this code, we have adopted a direct drive type instantiated com.mysql.jdbc.Driver, undesirable in this way, forcing the application code because MySQL is highly dependent on the Connector / J implementation, if the database code changes required reconstruction. Writing this here because, in the hope that the more in-depth understanding of JDBC, the actual development of a section of code should be the wording of the following:

package com.wwei.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

public class Conn_2 {

    public static void main(String[] args) throws Exception {
        // 连接信息可以从外部得到,比如配置文件。
        String url = "jdbc:mysql://localhost:3306/test";
        String driverClass = "com.mysql.jdbc.Driver";
        String user = "root";
        String password = "wwei";

        Class.forName(driverClass);
        Connection con = DriverManager.getConnection(url, user, password);
        if (con != null) {
            System.out.println("建立连接");
        }

        if (con != null && !con.isClosed()) {
            con.close();
            System.out.println("连接已断开");
        }
    }
}  



This time, JDBC code is not coupled to specific implementation of the JDBC driver, if there is a database change, we only need to modify the connection information and drive the implementation of the jar library file, which can link information from the JDBC driver implementation of the document access to, do not need to cram. When in order to improve skills, it would be best to remember common database connection information. Google everyone you can.
In the Connection interface, the common method for the object has SQL statements (Statement, PreparedStatement, CallableStatement) factory method: Statement createStatement () series, PreparedStatement prepareStatement (String sql) series, CallableStatement prepareCall (String sql) series, were used to create ordinary, pre-compiled, stored procedure SQL statement object; transaction processing methods: boolean getAutoCommit (), void setAutoCommit (boolean autoCommit),

void setTransactionIsolation (int level), Savepoint setSavepoint (String name), void commit (),
void rollback (), void rollback (Savepoint savepoint).
By default, Connection object in auto-commit mode, which means that each statement in the implementation of it after the changes are automatically submitted.
If you disable auto-commit mode (setAutoCommit (false)), in order to submit to change, we must explicitly call the commit method;
Database changes will not be able to save.
For example the use of the above SQL statement to be introduced objects and result set (ResultSet) followed in the preparation of the first to know about java.sql.DatabaseMetaData interface, the interface definition of the overall comprehensive information database, there is provider-driven implementation. Although rarely used but if want to know from the drive data to support implementation of the functions necessary to understand the. Code:

package com.wwei.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;

public class DBMetaData {

    public static void main(String[] args) throws Exception{    

        DatabaseMetaData meta = null;
        String url = "jdbc:mysql://localhost:3306/test";
        String driverClass = "com.mysql.jdbc.Driver";
        String user = "root";
        String password = "wwei";
        Class.forName(driverClass);

        Connection con = DriverManager.getConnection(url, user, password);
        if (con != null) {
            meta = con.getMetaData();
            System.out.println("数据库产品名称:" + meta.getDatabaseProductName() );
            System.out.println("数据库产品版本:" + meta.getDatabaseProductVersion() );
            System.out.println("数据库主版本:" + meta.getDatabaseMajorVersion());
            System.out.println("数据库次版本:" + meta.getDatabaseMinorVersion());     
            System.out.println("驱动名称:" + meta.getDriverName() );
            System.out.println("驱动版本:" + meta.getDriverVersion());          
            System.out.println("驱动实现JDBC规范主版本:" + meta.getJDBCMajorVersion());
            System.out.println("驱动实现JDBC规范次版本:" + meta.getJDBCMinorVersion());          
            System.out.println("当前数据库允许列最大字符数:" + meta.getMaxColumnNameLength());
            System.out.println("当前数据库支持最大并发连接数:" + meta.getMaxConnections());         
            System.out.println("数据库中在同一时间内可处于开放状态的最大活动语句数:" + meta.getMaxStatements());
        }

        if (con != null && !con.isClosed()) {
            con.close();  
        }
    }
}

Medium DatabaseMetaDate have more information, you can further access to Study JDK documentation. The introduction of a target all SQL statements and result sets.