2016.5.23 Mybatis Framework (ii) a summary of the original ecological JDBC Program

Source: Internet
Author: User
Tags what sql

JDBC Program

1. Create a Java project to join the JAR package

Database driver package (MySQL)

In fact, only the top of the drive package, because the use of MySQL, the general version is not a problem, this machine is MySQL 5.6

Below is the Oracle driver package

2. Create a Jdbctest project

1  PackageCn.itcast.mybatis.jdbc;2 3 Importjava.sql.Connection;4 ImportJava.sql.DriverManager;5 Importjava.sql.PreparedStatement;6 ImportJava.sql.ResultSet;7 Importjava.sql.SQLException;8 9 Ten  Public classJdbctest { One      Public Static voidMain (string[] args)throwssqlexception{ A         //Database Links -Connection Connection =NULL; -         //Precompiled statement, using precompilation to improve database performance thePreparedStatement PreparedStatement =NULL; -         //result set -ResultSet resultset=NULL; -          +         Try{ -             //Load Database driver +Class.forName ("Com.mysql.jdbc.Driver"); A              atConnection = Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/test?characterencoding=utf-8", "Root", "" "); -String sql = "SELECT * from user where username =?" and id =? "; -PreparedStatement =connection.preparestatement (SQL); -              -Preparedstatement.setstring (1, "Harry"); -Preparedstatement.setstring (2, "1"); in              -ResultSet =preparedstatement.executequery (); to              +              while(Resultset.next ()) { -System.out.println (resultset.getstring ("id") +resultset.getstring ("username"))); the             } *}Catch(Exception e) { $ e.printstacktrace ();Panax Notoginseng}finally{ - resultset.close (); the preparedstatement.close (); + Connection.close ();  A         } the     } +}

3. JDBC uses drivers

What is JDBC full name?

Java DataBase Connectivity

That is, the Java database connection is established

Then the 3 areas required

1> established link object Connection Connection

2> The precompiled Statement Declaration object established PreparedStatement PreparedStatement

3> query to the result set object ResultSet ResultSet

Next is the process of how to use JDBC for database linking and manipulation

1> Load Driver Class.forName (com.mysql.jdbc.Driver);

Locate the Driver.class file in the driver package, copy QualifiedName.

2> assigning url+ ports to link connection

Conneciton = Drivermanager.getconnection ("Jdbc:mysql://127.0.0.1:3306/test", "Test", "");

Format JDBC: Database type name://Address: Port/Database name username UserPassword

3> defines what SQL statement to execute

String sql = "SELECT * from user where username =?" and id=? "

Position symbol "?" The role of a reserved field is saved by the committed value of the pending

Preparedstatement.setstring (Index, "value");

The first one is the question mark, the second one is the value.

Actually replaced? The form ' value ' so obviously cannot replace some property names such as column

4> executes this precompiled return result set

ResultSet = Preparedstatement.executequerry ();

Query values can be obtained by entering the corresponding column property in the result set

Resultset.getstring ("id")

5> Releasing Resources

Order Upside Down: Resultset.close () Preparedstatement.close () Connection.close ()

4. Original Eco-JDBC Problem:

1> database connection, when used to create non-use immediate release, frequent connection and shutdown of the database, resulting in data resource waste affecting database performance

Harry--Database connection pool

2> SQL statements in Java code, this is a hard-coded way, if the SQL statement is modified, you need to recompile the Java code,

Set parameters in PreparedStatement, also hard coded in Java code

Harry---SQL statements, placeholders and other variables are placed in the XML file to be configured

3> result set ResultSet still hard-coded to get table field

Harry automatically map a query's result set to a Java object

2016.5.23 Mybatis Framework (ii) a summary of the original ecological JDBC Program

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.