A summary of the problems in 02mybatis_ native JDBC programming--which leads to the use of MyBatis

Source: Internet
Author: User

Let's first use JDBC to write an example:

First step: Build a table

The second step: Import the MySQL Jia package-mysql-connector-java-5.1.7.jar;

Step three: Write JAV programs

 PackageCn.itcast.mybatis.jdbc;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;ImportJava.sql.ResultSet;Importjava.sql.SQLException; Public classJDBC { Public Static voidMain (string[] args) {Connection Connection=NULL; PreparedStatement PreparedStatement=NULL; ResultSet ResultSet=NULL; Try {            //Load Database driverClass.forName ("Com.mysql.jdbc.Driver"); //get a database link by driving the management classConnection = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mybaits?characterencoding=utf-8", "Root", " Root); //Define SQL statements? represents a placeholderString sql = "SELECT * from user where username =?"; //Get preprocessing statementPreparedStatement =connection.preparestatement (SQL); //set the parameter, the first parameter is the ordinal of the parameter in the SQL statement (starting at 1), the second parameter is the parameter value setPreparedstatement.setstring (1, "Harry"); //issuing SQL execution queries to the database, querying the result setResultSet =Preparedstatement.executequery (); //traversing query result sets             while(Resultset.next ()) {System.out.println (resultset.getstring ("id") + "" +resultset.getstring ("username"))); }        } Catch(Exception e) {e.printstacktrace (); }finally{            //Freeing Resources            if(resultset!=NULL){                Try{resultset.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(preparedstatement!=NULL){                Try{preparedstatement.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }            if(connection!=NULL){                Try{connection.close (); } Catch(SQLException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }        }    }}

Run Result: correct.

Summary: The above JDBC programming. The following problem is found.

1, the database connection, the use of the creation, do not use immediate release, the database is frequently connected to open and close, resulting in a waste of database 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 recompiling the Java code.

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

Imagine: SQL statements and placeholder symbols and parameters are all configured in XML.

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

Imagine: Automatically map a query's result set to a Java object.

                                                                                                                                                                               

Because of the above problem, we have to learn mybatis to solve the above problem. So the next study we have to take questions to learn mybatis.

This collection is based on a podcast.

A summary of the problems in 02mybatis_ native JDBC programming--which leads to the use of MyBatis

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.