Java How to Connect to a Database Using JDBC

  • 2021-08-31 07:50:56
  • OfStack

Directory 1. Connecting to the database using JDBC
1. Connecting to the database using the JDBC-ODBC bridge driver
2. Here's a code demonstration
3. Pay attention
2. Source:

1. Connecting to the database using JDBC

1. Connecting to the database using the JDBC-ODBC bridge driver

Basic steps:
(1) Load and register the database driver
(2) Obtain database connection through DriverManager
(3) Obtaining the Statement object through the Connection object
(4) Execute SQL statements using the Statement interface
(5) Operating on the ResultSet result set
(6) Close the connection and free the resources

2. Here's a code demonstration

1. The syntax format for registering the database driver is as follows:


DriverManager.registerDriver(Driver driver)

Or


Class.forName("DriverName");

2. Create a database connection


String url = "jdbc:odbc:student";
//student Is the name of the data source created in Data Source Manager 
Connection con = DriverManager.getConnection(url);
//1 The following statement uses the 1 A way to connect to database without data source 
con=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};
DBQ=d:\\xsgl.mdb")

3. Get the Statement object
It can be seen that the three member methods serialized before create Statement objects, PreparedStatement objects and CallableStatement objects

4. Execute the SQL statement
All Statement have the following three methods to execute SQL statements
(1) execute (): You can execute any SQL statement
(2) executeQuery (): Executes the query statement and returns the ResultSet object
(3) executeUpate (): Adding, deleting and modifying

5. Obtain the result and combine with ResultSet object, and perform 1 series of operations.
Examples:


package com.bjpowernode.java_learning;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class D134_1_JDBCConnection {
	public static void main(String[] args) {
		Statement stmt = null;
		ResultSet rs = null;
		Connection conn = null;
		
		try {
			//1. Register the driver of the database 
			Class.forName("com.hxtt.sql.access.AccessDriver");
			//2. Pass DriverManager Get a database connection 
			conn = DriverManager.getConnection("jbdc:Access:///e:xsgl.mdb");
			//3. Pass Connection Object acquisition Statement Object 
			stmt = conn.createStatement();
			//4. Use Statement Execute SQL Statement 
			String sql = "select * from studentInfo";
			rs = stmt.executeQuery(sql);
			//5. Operation ResultSet Result set 
			System.out.println("studentID | studentName | studentSEX");
			while(rs.next()) {
				int id = rs.getInt("studentID");// Gets the value of the specified field by column name 
				String name = rs.getString("studentName");
				String psw = rs.getString("studentSEX");
				System.out.println(id + " | " + name + " | " + psw);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			//6. Recycling database resources 
			if (rs != null) {
				try {
					rs.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				rs = null;
			}
			if(stmt != null) {
				try {
					stmt.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				stmt = null;
			}
			if (conn != null) {
				try {
					conn.close();
				}catch(SQLException e) {
					e.printStackTrace();
				}
				conn = null;
			}
		}
	}
}

3. Pay attention

Access bridging driver is no longer included in JDK 1.7, so JDBC-ODBC bridging mode is no longer supported. jar package of Access driver needs to be downloaded (Access_JDBC30. jar), while JDK 1.1 to JDK1.6 all come with jar package, which does not need to be downloaded. After downloading, put the Access_JDBC30. jar package into the lib folder of JDK, and then modify the environment variable CLASSPATH to add this jar package. The path is the absolute path of jar package, for example: C:\ ProgramFiles\ Java\ jre1.8.0_65\ lib\ Access_JDBC30. jar. If you already have other values in CLASSPATH, you can add the package at last. Then set it up in the project, so that the database can be connected normally, but the name of the driver is not sun. jdbc. odbc. com. hxtt. sql. access. AccessDriver. The database path can also be directly connected, and URL can be set to jdbc: Access://d: MYDB. accdb.

2. Source:

D134_1_JDBCConnection.java
https://github.com/ruigege66/Java/blob/master/D134_1_JDBCConnection.java

The above is Java how to use JDBC database connection details, more about Java using JDBC database connection information please pay attention to other related articles on this site!


Related articles: