Mybatis learn a

JDBC

JDBC (Java DataBase Connectivity) is a bridge between Java and database, is a norm rather than an implementation can execute SQL statements. It consists of a set with the Java language classes and interfaces written. Various different types of database has a corresponding implementation, the code is herein implemented using MySQL database.

 

 

 

 JDBC programming as follows

A: introducing special jar package (different databases require different jar package)

II: drive initialization

 

 

 Three: the establishment of links for Connection

 

 

 Four: Create a Statement or PreparedStatement interface to execute SQL (recommended PreparedStatement interface)

 

 

PreparedStatement advantages:

① high efficiency.

When using PreparedStatement to execute SQL commands, the command will be parsed and compiled the database, and put the command buffer. After each execution of a PreparedStatement object with a precompiled commands can be reused

② code readability and maintainability

③ good security.

Use PreparedStatement can prevent SQL injection.

Five: release of resources

In the process of coding JDBC we create Connection, ResultSet and other resources, which finished after use is sure to be closed

 

 

 

code show as below

Connection connection=null;
PreparedStatement preparedStatement = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
connection=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl",
"scott",
"scott");
preparedStatement=connection.prepareStatement("insert into Student values (?,?,?)");
preparedStatement.setInt(1,1000);
preparedStatement.setString(2,"wll");
preparedStatement.setInt(3,500);
preparedStatement.executeUpdate();

} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
preparedStatement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

 

Guess you like

Origin www.cnblogs.com/lovetq520/p/11698522.html