Several steps of the JDBC link database _java

Source: Internet
Author: User
Tags connection pooling finally block

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

Jdbc:java access to the database solution.

Several steps: 1. Load drive class;

2. Establish a connection with the database;

3. Execute SQL statement

4. Processing result Sets

5. Close the connection

1. The first step : Load the driver class:

Note: Different databases, the reference string is different, Oracle's connection is: Class.forName ("Oracle.jdbc.driver.OracleDriver"); After this step is executed, the program may be thrown: classnotfoundexception, for general reasons:

A. The driver jar package for the database is not imported into the environment variable

B. The string in Class.forName is not spelled correctly

2. Step two : Establish a connection with the database through DriverManager:

Its static method getconnection is used to get the connection. You typically need to pass in three parameters

Parameter 1: The address and Port of the database (different database string contents are not identical)

Oracle's address: Jdbc:oracle:thin: @host:p Ort:sid

Parameter 2: User name of the database

Parameter 3: password for database corresponding user name

Connection conn = Drivermanager.getconnect
("Jdbc:oracle:thin: @host:p ort:oracle", "User", "PSD");

3. Step three : java.sql.Statement Execute SQL statements and get results

Statement state = Conn.createstatement ();

String sql= "* * Here is the SQL statement */";

Statement provides different methods of execution for different SQL statements:

ResultSet executequery (String sql)

* This method is specifically used to execute the DQL statement, and the returned resultset represents the result set of the query

int executeupdate (String sql)
* This method is specifically used to execute a DML statement, and the number returned indicates how many of the data in the table are affected by executing the statement

Boolean execute (String sql)
* The method can theoretically execute any statement, but since DQL,DML has a specific method of execution, the method is often used to execute DDL statements

ResultSet rs = state.executequery (SQL);
Output query results: while (Rs.next ())
{OUTPUT Statement}
ResultSet provides methods for traversing a result set:

Boolean Next ()

* This method has two effects, first when we query the result set after the RS pointer to the first data, so we need to call the next () to move its pointer to the first data and represent the data.
The second function is to look at the return value, if the pointer moves down and finds no data, returns FALSE, and returns true if there is one, so we get the value of each field in the current record only if the method returns True. RS also provides several getxxx (String FieldName) Method:

* This series of methods is used to get the value of the given field in the current record represented by Rs. Different fields need to invoke the corresponding method because of different types

Step 4th : Close the connection, write in the finally block

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

Put the database connection in a tool class, to achieve the effect of reuse

Since access to the database is an often-used operation, in engineering, usually write a tool class to access the database, all access to the database after the operation, from the tool class to get the connection, implementation of the tool class in two ways:

1. Write the data configuration directly in the tool class Dbutil

2. Write the database configuration in a Properties property file, the tool class reads the attribute file, gets the database parameter line by row (generally uses the second kind)

If using the first method, in the later need to modify the database used or to modify the host, port, database connection name, password, etc., you need to modify the source code inside the data, not easy to maintain the system, It is common to use the second method of database Connection tool class Dbutil.java and the main steps of connection pooling:

 properties prop = new properties (); Prop.load (New FileInputStream ("Config.properties")
);
Initializes a String drivername = Prop.getproperty ("DriverName") based on a configuration item;
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 wait time int maxwait = Integer.parseint (Prop.getproperty ("maxwait"));
Initialize Connection pool CP = new Basicdatasource ();
Quite with the content of the 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 content is for JDBC link database steps to explain, hope to be able to help everybody!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.