Detailed description of the basic process for jdbc operations on the database _ MySQL

Source: Internet
Author: User
Detailed explanation of the basic process for jdbc database operations All JDBC applications have the following basic procedures:
1. load the database driver and establish a connection to the database.
2. execute SQL statements.
3. processing result.
4. disconnect from the database to release resources.

Next we will take a closer look at each step:
In fact, each stage can be used as an independent class method file. A total of other applications are called.

1. load the database driver and establish a connection to the database:

String driverName = "com. mysql. jdbc. Driver ";
String connectiionString = "jdbc: mysql: // 3306/test? "+" User = root & password = chen & characterEncoding = utf-8 ";
Connection connection = null;
Try {
Class. forName (driverName); // Here is the so-called database driver loading
Connection = (Connection) DriverManager. getConnection (connectiionString); // The database connection is established here.
System. out. println ("database connection successful ");
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return connection;

2. execute the SQL statement:
When executing SQL statements, there are two common types of statement objects:
Statement: it provides a method to directly execute SQL statements in the database. It is sufficient for queries, deletions, or fixed SQL statements that only run once.

Statement statement = (Statement) dUtil. getConnection (). createStatement ();

String SQL = "delete from diary where title =" + "'" + title + "'";

Int count=statement.exe cuteUpdate (SQL );

System. out. println ("deleted successfully ");

Preparedstatement: This statement object is used for SQL statements that need to be executed multiple times and only have different data values each time. It also provides some methods to indicate the input parameters used by the statement.

String SQL = "insert into diary (title, content, authorname, time) values (?,?,?, Now ())";
Try {
PreparedStatement preparedStatement = (PreparedStatement) dUtil. getConnection (). prepareStatement (SQL );
String title = diary. getTitle ();
String content = diary. getContent ();
String authorname = diary. getAuthorName ();
PreparedStatement. setString (1, title );
PreparedStatement. setString (2, content );
PreparedStatement. setString (3, authorname );

3. processing result:

ResultSet resultSet=statement.exe cuteQuery (SQL );
While (resultSet. next ()){
Diary diary = new Diary ();
Diary. setAuthorName (resultSet. getString ("authorname "));
Diary. setContent (resultSet. getString ("content "));
Diary. setTitle (resultSet. getString ("title "));
Diary. setId (resultSet. getInt ("id "));
Date time = resultSet. getDate ("time ");

Here, we should know that the Statement method for executing SQL statements: the insert, Update, and delete statements are executed using the executeUpdate method of Statement, the returned results are the number of inserts, updates, and deletions. The execution of select statements is especially executed using the executeQuery method of Statement. The returned results are stored in the resultset result set. we can call the next () method to move them to the next record in the result set. The result set consists of rows and columns. The data of each column can be obtained through a series of get methods (such as getString, getInt, and getDate) of the corresponding database type.

4. disconnect from the database to release resources:
After the result set, statements, and connection objects are used up, we must close them correctly. The close () method is used to connect objects, result set objects, and all statement objects. by calling this method, we can ensure that all resources related to specific database systems are correctly released.

Public static void closeConnection (ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) throws SQLException {

If (resultSet! = Null) resultSet. close ();
If (preparedStatement! = Null) preparedStatement. close ();
If (connection! = Null & connection. isClosed () = false) connection. close ();
System. out. println ("database closed ");


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: 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.