Several steps for JDBC linking a database

  • 2020-04-01 04:00:03
  • OfStack

This article lists the 4 steps of JDBC linking database for your reference:

JDBC: a JAVA solution for accessing databases.

Several steps: 1. Load the driver class; 2.

2. Establish a connection with the database;

                      3. Execute the SQL statement

      4. Process result sets

      Close the connection

1. The first step : load driver class:

Note: different databases, with reference to the string, ORACLE connection is: the Class. The class.forname ("... The ORACLE JDBC driver OracleDriver "); After this step, the program might throw a: ClassNotFoundException, usually because:

A. The driver jar for the database was not imported into the environment variables

The string in b. class. forName is spelled incorrectly

2. The second step : connection to database via DriverManager:

Its static method getConnection is used to get the connection. Three parameters are usually passed in

Parameter 1: database address and port (different database string contents are different)

Oracle address: JDBC :oracle:thin:@host:port:sid

Parameter 2: the user name of the database

Parameter 3: the password for the username of the database

The Connection conn = DriverManager. GetConnect
(" JDBC: oracle: thin: @ host: port: oracle ", "user", "PSD");

3. The third step : java.sql.Statement executes the SQL Statement and gets the result

The Statement state = conn. CreateStatement ();

String SQL = "  ";

Statement provides different execution methods for different SQL statements:

The ResultSet executeQuery (String SQL)

* this method is specialized for executing DQL statements, and the returned ResultSet represents the ResultSet from the query

Int executeUpdate (String SQL)
* this method is specifically used to execute a DML statement, and the number returned indicates how many pieces of data in the table were affected by the execution of the statement

Boolean execute (String SQL)
* this method can theoretically execute any statement, but since DQL and DML have specialized methods to execute, this method is usually used to execute DDL statements

The ResultSet rs = state. The executeQuery (SQL);
Output query result: while(rs.next())
{  Output statement   }
A ResultSet provides methods for traversing a ResultSet:

Boolean next ()

* this method has two functions. First, when we query the result set, rs pointer points to the first data, so we need to call next() once to move its pointer to the first data and represent the first data.  
The second function is to see the return value. If the pointer moves down and there is no data, it will return false, and if there is no data, it will return true. Therefore, we only get the corresponding value of each field of the current record under the condition that this method returns true.

* this set of methods is used to get the value of a given field in the current record represented by RS. Different fields need to call corresponding methods due to different types

Step 4 : close the connection and write ina finally block


finally{
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Place database connections in a utility class for reuse

Because access to the database is often used in the operation, so in the project, usually write a tool class to access the database, after all the operations to access the database, are from the tool class to obtain the connection, implement the two ways of the tool class:

1. Write the data configuration directly in the tooling class DBUtil

2. Write the database configuration in a properties file, the utility class reads the properties file, and gets the database parameters line by line (usually the second)

If you use the first method, in the later need to modify the use of the database or modify host, port, database connection name, password, etc., you need to modify the data in the source code, not easy to maintain the system, so generally use the second method database connection tool class dbutil.java and the main steps of connection pool:


Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
//Initialize according to the configuration item
String driverName = prop.getProperty("driverName");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
//Maximum number of connections
int maxActive = Integer.parseInt(prop.getProperty("maxActive"));
//Maximum waiting time
int maxWait = Integer.parseInt(prop.getProperty("maxWait"));
//Initialize the connection pool
cp = new BasicDataSource();
//The equivalent is class.forname ()
cp.setDriverClassName(driverName);
cp.setUrl(url);
cp.setUsername(username);
cp.setPassword(password);
cp.setMaxActive(maxActive);
cp.setMaxWait(maxWait);
public static Connection getConnection() throws Exception{
return cp.getConnection();
}
The above is for the JDBC link database steps to explain, I hope to help you!

Related articles: