JDBC entry technology

1 What is JDBC
  JDBC (Java DataBase Connectivity) is a Java database connection, and to put it bluntly is to use the Java language to operate the database. It turns out that we operate the database by using SQL statements in the console to operate the database, and JDBC is using the Java language to send SQL statements to the database.
2 JDBC principle
   Application ====>JDBC====>msql driver ==="mysql database
JDBC is the interface, and the JDBC driver is the implementation of the interface, without the driver can not complete the database connection! Each database vendor has its own driver for connecting to its own company's database.
3 JDBC core class (interface) Introduction
The core classes in JDBC are: DriverManager, Connection, Statement, and ResultSet!
DriverManger (Driver Manager) has two functions:
 Registering the driver: This allows JDBC to know which driver to use;
 Obtaining Connection: If the Connection can be obtained, it means that it has been connected to the database.
The Connection object represents the connection, and the communication with the database is carried out through this object:
The most important method of Connection is to obtain the Statement object;
Statement is used to send SQL statements to the database, so that the database will execute and send it SQL statement
 executevoidUpdate(String sql): execute update operation (insert, update, delete, etc.);
ResultSet executeQuery(String sql): Execute the query operation, the database will send the query result after executing the query, and the query result is the ResultSet; the
ResultSet object represents the query result set, and the result set will be generated only after the query operation is executed. The result set is a two-dimensional table with rows and columns. To operate the result set, you need to learn to move the "row cursor" inside the ResultSet and get the data on each column on the current row:
boolean next(): Move the "row cursor" to the next row, and return whether the moved row exists ;
XXX getXXX(int col): Get the value of the specified column in the current row, the parameter is the number of columns, and the number of columns starts from 1, not 0.
4 Normalized code
    The so-called normalized code is to close ResultSet, Statement, and Connection regardless of whether an exception occurs,
@Test
public void query() {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = getConnection ();
stmt = con.createStatement();
String sql = "select * from user";
rs = stmt.executeQuery(sql);
while(rs.next()) {
String username = rs.getString(1);
String password = rs.getString(2);
System.out.println(username + ", " + password);
}
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326164592&siteId=291194637