Java JDBC programming Summary

Source: Internet
Author: User

JDBC is a basic technology for Java database operations and is not the only technology.

Hibernate, toplink, and other or mapping technology for database operations are built on the JDBC technology. In fact, their performance is very different from that of JDBC. However, if JDBC is not used properly, not as good as hibernate. For the time being, I will not mention the advantages and disadvantages of Java. I will summarize the basic technologies of Java to deepen my understanding.

I. Basic principles of JDBC is the Technical Specification for Java database operations.

It actually defines a set of standard database operation interfaces. To allow Java to operate databases, you must have classes that implement the JDBC interfaces. In order for different database vendors to allow Java to operate their own databases, provides the Implementation of The JDBC interface-these classes that implement the JDBC interface are packaged into a jar package, which is the database driver we usually see. Since different database data operation mechanisms are different, the specific implementation of JDBC is very different. However, as a Java programmer, you only need to submit it to the Java JDBC interface, they don't have to worry about how they do it! Now I know what the JDBC driver is. Of course, these classes can be written by yourself-if you are awesome!

Ii. JDBC programming steps to illustrate this step

Suppose you want to execute an SQL query using a Java program, you need to follow the steps below:

1. the URL for creating the specified database is actually a unified resource locator, which contains some information about connecting to the database: database Type, port, driver type, connection method, IP address (or name) of the database server, and database name (or alias. The format is as follows:

JDBC: subprotocol: subname: // dbserverip: Port/databasename for example, a MySQL URL: JDBC: mysql: // 192.168.3.143: 3306/zfvims

2. There are two methods to load the driver class to the JVM memory area: one is to load the specified driver using the class. forname () method.

One is to add the driver to the JDBC. Drivers attribute of Java. Lang. system.

The last note is that sometimes the classpath environment variable added to the system does not work because the JVM has not loaded the driver before using the JDBC interface to operate the database.

Class. forname ("com. MySQL. JDBC. Driver ")

3. Use the drivermanager class to manage drivers and create database connections.

The drivermanager class acts between programmers and JDBC drivers and provides methods such as registering management drivers to establish connection details. All its members are static. The getconnection method creates a JDBC connection object.

Connection conn = drivermanager. registerdriver ("JDBC: mysql: // 192.168.3.143: 3306/zfvims", "lavasoft", "password ");

4. Connection class -- the connection class mentioned in database connection is actually a class that implements the JDBC connection interface. This class is generally implemented by the JDBC driver. The connection class indicates a database connection. You can obtain detailed information about database objects such as databases and tables through its objects. However, this connection is used to send SQL statements to the database for execution.

Database Connection establishment is resource-and time-consuming. Therefore, you must close the connection through the close () method to release system resources without connection.

5. Statement class -- send and execute (static) SQL statements. You can create a statement object through the createstatement () method of the connection object. You can use this object method to send the statement) execute a static SQL statement. If you want to execute a dynamic SQL statement (with parameters in the SQL string), use the preparedstatement class. Its usage is similar to statement.

Statement stmt = con. createstatement ()

6. resultset class-result set when you execute an SQL query, a query result is generated. Resultset indicates the data table of the database result set. It is usually generated by executing the statement to query the database. The resultset object has a pointer to its current data row. You can use the resultset object to obtain not only the result set data, but also the column name and Data Type of the result set table.

Resultset rs1_stmt.exe cutequery (SQL)

7. Close the database connection. After the SQL operation is complete, close the database connection to avoid consuming system resources because the connection is not closed, multiple operations will establish multiple connections, and eventually the database connection will reach the maximum limit, or the system resources will be exhausted, resulting in application crash. Therefore, pay attention to disabling resources, especially database connections.

III. Basic example of JDBC Programming

Next, we will execute a simple MySQL query to show the preceding JDBC programming methods and steps.

Public class testjdbc (){

Public static connection getconnectionbyjdbc (){

Connection conn = NULL;

Try {

// Load the Driver Class

Class. forname ("com. MySQL. JDBC. Driver ");

} Catch (classnotfoundexception e ){

System. Out. println ("An error occurred while loading the driver! ");

E. printstacktrace ();

}

Try {

// Establish a JDBC connection

Conn = drivermanager. getconnection ("JDBC: mysql: // 192.168.3.143: 3306/zfvims", "lavasoft", "password ");

} Catch (sqlexception e ){

System. Out. println ("An error occurred while connecting to the database! ");

E. printstacktrace ();

}

Return conn;

}

Public static string test (){

String sqlx = "select T. Code, T. name from province t order by T. Code ASC ";

Try {

// Create a JDBC Statement

Statement stmt = conn. createstatement (resultset. type_forward_only, resultset. concur_read_only );

// Execute the query

Resultset = stmt.exe cutequery (sqlx );

While (Rs. Next ()){

String code = Rs. getstring ("Code ");

String name = Rs. getstring ("name ");

System. Out. println (code + name );

}

} Catch (sqlexception e ){

System. Out. println (E. getmessage ());

E. printstacktrace ();

} Finally {

// Closes the connection proactively (to avoid the connection being closed in the try statement block when an exception occurs)

Try {

If (Conn! = NULL) Conn. Close ();

} Catch (sqlexception e ){

System. Out. println (E. getmessage ());

E. printstacktrace ();

}

}

}

Public static void main (string ARGs []) {

New testjdbc (). Test ();

}

}

Iv. JDBC traps

1. the conn must be closed in the finally statement block.

2. Statement and resultset should narrow down the variable scope as much as possible.

3. statement can be used multiple times to define multiple. A statement object is associated with only one resultset object and is the last query.

4. The resultset is automatically disabled after the connection and resultset are disabled.

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.