Statement database operation:
CRUD: using the executeUpdate ();
query: Use the executeQuery ();
The ResultSet: * Save result set from XXX SELECT
Next (): cursor downward, determines whether the next data; to true / to false
Previous (): to true / to false
getXxx (field names | position): Get a specific field value of
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JDBCDemo { private final String URL="jdbc:mysql://localhost:3306/java?useSSL=false"; private final String USERNAME = "root"; private final String PASSWORD = "123456"; public void update(){//实现增删改 Connection connection = null; The Statement stmt = null ; the try { // A driving introduced. The Class.forName ( "com.mysql.jdbc.Driver" ); // B establish a connection to a database. Connection = the DriverManager.getConnection (the URL, USERNAME, PASSWORD); // . C transmission sql, additions and deletions performed, check stmt = Connection.createStatement (); // String SQL = "INSERT INTO Book (name,. price) values ( 'novel', 50)"; // String = SQL " update book set name = 'scientific' the WHERE the above mentioned id = 17 "; String SQL =" Book the Delete from the WHERE the above mentioned id = 19 " ; //d.执行sql语句 stmt.executeUpdate(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try{ if(stmt!=null) stmt.close(); if(connection!=null) connection.close(); }catch(SQLException e){ e.printStackTrace(); } } } public void query(){//实现查询 Connection = Connection null ; the Statement stmt = null ; the ResultSet RS = null ; the try { // A driving introduced. The Class.forName ( "com.mysql.jdbc.Driver" ); // . B establish a connection to a database Connection = the DriverManager. getConnection (the URL of, USERNAME, PASSWORD); // . c send sql, additions and deletions to perform, check stmt = Connection.createStatement (); String SQL = "Book from the SELECT *" ; //SQL execution [CRUD executeUpdate (), query the executeQuery ()] RS = stmt.executeQuery (SQL); // process result the while (rs.next ()) { // determines whether there is a next line data String na = rs.getString ( "name" ); String PRI = rs.getString ( ". price" ); System.out.println (Na + "-" + PRI); } } the catch (Exception E) { // the TODO Auto Block the catch-Generated e.printStackTrace (); } the finally { the try { IF(rs!=null) rs.close(); if(stmt!=null) stmt.close(); if(connection!=null) connection.close(); }catch(SQLException e){ e.printStackTrace(); } } } public static void main(String[] args) { JDBCDemo j = new JDBCDemo(); //j.update(); j.query(); } }
PreparedStatement operation of the database:
public interface PreparedStatement extends Statement
Thus
CRUD: executeUpdate ()
Query: the executeQuery ();
- furthermore
assignment setXxx ();
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCPreparedStatement { private final String URL="jdbc:mysql://localhost:3306/java?useSSL=false"; private final String USERNAME = "root"; private final String PASSWORD = "123456"; public void update(){//实现增删改 Connection connection = null ; the PreparedStatement pstmt = null ; the try { // A driving introduced. the Class.forName ( "com.mysql.jdbc.Driver" ); // B establish a connection to a database. Connection = the DriverManager.getConnection (the URL, USERNAME, PASSWORD) ; // . c send sql, additions and deletions to perform, check String SQL = "INSERT INTO Book values (,,???)" ; pstmt = Connection.prepareStatement (SQL); pstmt.setString ( 1, "next door" ); pstmt.setString ( 2, "56 is" ); pstmt.setInt (3, 21); //String sql = "insert into book(name,price) values('小说',50)"; //String sql = "update book set name='科学' where id=17"; pstmt.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try{ if(pstmt!=null) pstmt.close(); if(connection!=null) connection.close(); }catch(SQLException E) { e.printStackTrace (); } } } public void Query () { Connection Connection = null ; the PreparedStatement pstmt = null ; the ResultSet RS = null ; the try { // A driving introduced. The Class.forName ( "COM. mysql.jdbc.Driver " ); // . B establish a connection to a database connection = the DriverManager.getConnection (the URL, USERNAME, PASSWORD); // . C transmission sql, additions and deletions performed, search SQL = String "from the SELECT * Book the WHERE the above mentioned id =?" ; Pstmt = Connection.prepareStatement (SQL); pstmt.setInt ( 1, 21 ); // execute sql [CRUD executeUpdate (), query executeQuery ()] rs = pstmt.executeQuery (); // process result the while (rs.next ()) { // determines whether there is a next line data String = Na rs.getString ( "name" ); String PRI = rs.getString ( ". price" ) ; System.out.println (Na + "-" + PRI); } } the catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try{ if(rs!=null) rs.close(); if(pstmt!=null) pstmt.close(); if(connection!=null) connection.close(); }catch(SQLException e){ e.printStackTrace(); } } } public static void main(String[] args) { JDBCPreparedStatement j = new JDBCPreparedStatement(); /*j.update();*/ j.query(); } }
PreparedStatement and Statement of differences in the use of:
Of Statement:
1, defined SQL
String sql =" insert into book(name,price) values('小说',50) " ;
2、stmt.executeUpdate(sql);
PreparedStatement:
1, sql defined
String sql =" insert into book(name,price) values(?,?,?) " ;
pstmt = connection.prepareStatement(sql);//预编译SQL
pstmt.setString(1, "隔壁");
pstmt.setString(2, "56");
pstmt.setInt(3, 21);
2、 pstmt.executeUpdate();