Quick Start with JDBC

Source: Internet
Author: User

1. perform the following five steps to connect to the database through JDBC.
The most common operations for JDBC to connect to a database are query operations, which are also complicated to add, delete, and modify operations in JDBC. Therefore, the five steps mentioned by the author are based on data query operations. When learning JDBC, most people may feel that JDBC is hard to learn. But these steps are hard to remember. In fact, the main reason is whether you have learned it carefully or not. I believe that the steps for connecting JDBC to the database can be summarized as CCPRC.
1. Drive registration (C-Class)
Class. forName ("oracle. jdbc. driver. OracleDriver ");
2. Obtain the Connection (C-Connection)
String str = "jdbc: oracle: thin :@ localhost: 1521: oracle ";
Connection conn = DriverManager. getConnection (str, "hr", "hr ");
3. Declare a pre-compiled Statement (P-PreparedStatement)
String SQL = "select * from person ";
PreparedStatement ps = conn. prepareStatement (SQL );
4. Execute the statement (R-ResultSet)
ResultSet rs = ps.exe cuteQuery ();
5. release resources (C-close)
Ps. close ();
Conn. close ();
Remember CCPRC and understand the meaning of each letter. I believe you can do it even if you are a cainiao.
 
2. Write a standard DBhelper class to reuse code and improve connection efficiency.
 
If you have completed the preceding steps, you should consider further optimization. There are many connections between a software and a database. We cannot repeatedly write the same code every time, and there is no need to repeatedly register the database driver, therefore, we should put things with little variability and reusable things together, that is, put them in a class for everyone to use.
 
Define a DBhelper class.
Public final class DbHelper {}
The final keyword is used for modification because we do not want this class to be inherited.
 
Attributes that are basically unchanged, such as database connection strings, driver strings, database login names, and database logon passwords, can be declared as constants in DBhelper.
 
Private final static String DB_URL = "jdbc: oracle: thin :@localhost: 1521: oracle ";
Private final static String DB_DRIVER = "oracle. jdbc. driver. OracleDriver ";
Private final static String DB_USERNAME = "hr ";
Private final static String DB_PASSWORD = "hr ";
The database driver does not need to be registered multiple times. We put the registration of the database driver in the static {} code block, only once when the DBhelper class is loaded.
Static {
Try {
Class. forName (DB_DRIVER );
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
}
}
This is a static method for establishing a connection with a database.
Public static Connection getConnection (){
Try {
Conn = DriverManager
. GetConnection (DB_URL, DB_USERNAME, DB_PASSWORD );
} Catch (Exception ex ){
Ex. printStackTrace ();
}
Return conn;
}
Resource shutdown is required after each interaction with the database, which may involve the shutdown of objects such as PreparedStatement, Connection, ResultSet, and Statement, after a period of time, your system may crash because the system resources have been used up and are not returned. That is, when all the resources on our planet are used up, our planet will be destroyed.
The closure of resources is accompanied by the interaction with the database, where database interaction requires the closure of database resources, so the reuse of resource close code is also necessary, otherwise, you must repeatedly write the following code every time you interact with the database.
 
Finally {
Try {
If (ps! = Null ){
Ps. close ();
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Try {
If (conn! = Null ){
Conn. close ();
}
} Catch (SQLException e ){
E. printStackTrace ();
}
 
}
 
}
Write the code for resource shutdown in DBhelper as follows:
// Disable the ResultSet Resource
Public static void close (ResultSet rs ){
If (rs! = Null ){
Try {
Rs. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
 
}
 
}
 
// Close the Statement Resource
Public static void close (Statement sta ){
If (sta! = Null ){
Try {
Sta. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
 
}
 
}
 
// Close the Connection Resource
Public static void close (Connection conn ){
If (conn! = Null ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
 
}
}
For the complete code of the DBhelper class, see the attachment.


Author: "extreme scholar"

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.