1、 Using JDBC to connect database

1. Use jdbc-odbc bridge driver to connect database

Basic steps:

(1) Load and register the database driver

(2) Get database connection through drivermanager

(3) Get the statement object through the connection object

(4) Executing SQL statements using statement interface

(5) Operation resultset result set

(6) Close the connection and release the resources

2. The following code demonstration

1. The syntax format of the registered database driver is as follows:

DriverManager.registerDriver (Driverdriver)


Class.forName (“DriverName”);

2. Create database connection

Stringurl=“ jdbc:odbc :student”;

//Student is the name of the data source created in the data source manager

ConnecTIoncon= DriverManager.getConnecTIon (url);

//The following statement is a way of connecting to the database without data source

con= DriverManager.getConnecTIon (“ jdbc:odbc :driver={MicrosoftAccessDriver(*.mdb)};

DBQ=d:\\ xsgl.mdb ”)

3. Get the statement object

It can be seen that the three member methods of previous serialization create statement object, Preparedstatement object and callablestatement object

4. Execute SQL statements

All statements have the following three ways to execute SQL statements

(1) Execute (): you can execute any SQL statement

(2) ExecuteQuery (): executes the query statement and returns the resultset object

(3) Executeupdate(): execute the operation of adding, deleting and modifying

5. Get the result and combine the resultset object to perform a series of operations.

give an example:

packagecom.bjpowernode.java_ learning;

importjava.sql.Statement ;

importjava.sql.Connection ;

importjava.sql.ResultSet ;

importjava.sql.SQLException ;

publicclassD134_ 1_ JDBCConnection{






//1. Register the driver of database

Class.forName (“ com.hxtt.sql . access.AccessDriver ”);

//2. Get database connection through drivermanager

conn= DriverManager.getConnection (“ jbdc:Access :///e: xsgl.mdb ”);

//3. Get the statement object through the connection object

stmt= conn.createStatement ();

//4. Use statement to execute SQL statement


rs= stmt.executeQuery (sql);

//5. Operate resultset result set

System.out.println (“studentID|studentName|studentSEX”);

while( rs.next ()){

intid= rs.getInt (“studentID”); / / get the value of the specified field through the column name

Stringname= rs.getString (“studentName”);

Stringpsw= rs.getString (“studentSEX”);

System.out.println (id+“|”+name+“|”+psw);





//6. Recycle database resources

if(rs! =null){


rs.close ();






if(stmt! =null){


stmt.close ();






if(conn! =null){


conn.close ();









3. Points for attention

The version after jdk1.7 no longer contains access bridge driver, so it no longer supports jdbc-odbc bridge mode, so it is necessary to download the access driver jar package (access)_ JDK 1.1 to JDK 1.6 all come with their own jar packages, which do not need to be downloaded.

After downloading, download access_ The jdbc30.jar package is put into the Lib folder of JDK, and then the environment variable classpath is modified to add the jar package. The path is the absolute path of the jar package, for example: C: / / program files / Java / jre1.8.0_ 65\lib\Access_ JDBC30.jar。 If there are other values in classpath, you can add the package at last. Then set it in the project, so that you can connect to the database normally, but the name of the driver is not sun.jdbc.odbc . jdbcodbcdriver, but com.hxtt.sql . access.AccessDriver , the database path can also be directly connected, and the URL can be set to jdbc:Access :///d: MYDB.accdb .

2、 Source code:

D134_ 1_ JDBCConnection.java

https://github.com/ruigege66/Java/blob/master/D134_ 1_ JDBCConnection.java

That’s how Java uses JDBC to connect to a database.

Editor: hfy

Leave a Reply

Your email address will not be published. Required fields are marked *