Summary of the original JDBC (using JDBC development alone) problem

Source: Internet
Author: User

Before writing this program you need to create a Java project, which is the premise

Then add the MySQL driver package, I use the database is version 5.1, so the driver package is also 5.1 version, JDK is 1.7

Create DATABASE bit mybatis, table fame user


Package Com.mybatis.jdbc;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import java.sql.resultset;/** * JDBC Operations database * @author Administrator * */public class Jdbctest {p Ublic static void Main (string[] args) {//Database link Connection connection=null;//precompiled statement, can improve database performance PreparedStatement preparedstatement=null;//result set ResultSet resultset=null;try {//Load Database driver Class.forName ("Com.mysql.jdbc.Driver");// Get the database link by driving the management class Connection=drivermanager.getconnection ("jdbc:mysql://localhost:3306/mybatis?characterencoding= Utf-8 "," root "," 123456 ");//define SQL statements,? Represents a placeholder string sql= "select * from user where id=?"; /Get preprocessed statementpreparedstatement=connection.preparestatement (SQL);//Set the SQL parameter, the first parameter is the ordinal of the parameter in SQL (starting at 1), The body parameters are set to the value Preparedstatement.setint (1, 1);//Send SQL request to database, query result set resultset=preparedstatement.executequery ();// Iterate through the query result set while (Resultset.next ()) {System.out.println (resultset.getstring (2));}} catch (Exception e) {e.printstacktrace ();} Finally{try {//close connection resultset.close ();p RepareDstatement.close (); Connection.close ();} catch (Exception E2) {e2.printstacktrace ();}}}}


Based on the above coding steps, we have the following conclusions:

1. Load Database Driver

2. Create and get database links

3. Create a Jdbcstatement object

4. Set the SQL statement

5. Set parameters in SQL statement (using PreparedStatement)

6. Execute SQL via statement and get results

7. Parse the results of SQL execution

8. Releasing resources (ResultSet, PreparedStatement, connection)


The above is a simple example of using JDBC to manipulate the database, so it is easy to say that the operation of the database will have some problems.

1, the database connection, the use of the creation, not applicable on the release, the database is frequently opened and closed operations, which is a waste of resources, affecting database performance.

Imagine: Use a database connection pool to manage database connections.

2, hard code SQL statements into the Java code, if the SQL statement modification, the need to recompile Java code, not conducive to system maintenance.

Imagine: Configuring SQL statements in an XML configuration file, even if SQL changes, does not require modifications to the Java code to recompile.

3, set the parameters to the PreparedStatement, the placeholder position parameter value, hard coding in Java, not conducive to system maintenance.

Imagine: All SQL statements and placeholders and parameters are configured in XML.

4, when traversing the result set data from the resultset, there is hard coding, and the field of getting table is hard-coded, which is not conducive to system maintenance.

Imagine: Map a result set to a Java object.

Summary of the original JDBC (using JDBC development alone) problem

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.