当前位置:网站首页>JDBC interface summary

JDBC interface summary

2022-06-12 19:40:00 Jiaran is hungry three times a day

JDBC Interface summary

What is? JDBC?

JDBC(Java DataBase Connectivity) be called Java Database connection , It is an application for database access API, By a group Java Language written classes and interfaces , With JDBC You can use the same syntax to access multiple relational databases , Don't worry about the difference of its database operation language . With JDBC, There's no need to visit Mysql The database specially writes a program , For the visit Oracle Another program is specially written, and so on .

In short , Is a set of standardized interfaces .

establish JDBC Application steps

  1. Registration drive
  2. Get the connection
  3. Get database operation object
  4. perform SQL sentence
  5. Process query result set (DQL)
  6. Close the connection
public class JDBCTest {
  public static void main(String[] args) {
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    
    try{
      ResourceBundle rb = ResourceBundle.getBundle("jdbc");
      String driver = rb.getString("driver");
      String url = rb.getString("url");
      String user = rb.getString("user");
      String password = rb.getString("password");
      Class.forName(driver);
      conn = DriverManager.getConnection(url,user,password);
      stmt = conn.createStatement();
      rs = stmt.executeQuery("select empno,ename,sal from emp");
  
      while(rs.next()){
    
        int empno = rs.getInt("empno");
        String ename = rs.getString("ename");
        double sal = rs.getDouble("sal");
        System.out.println(empno + "," + ename + "," + (sal + 200));
      }
    } catch(Exception e){
      e.printStackTrace();
    }finally{
      if(rs != null){
        try{
          rs.close();
        } catch (Exception e){
          e.printStackTrace();
        }
      }
      if(stmt != null){
        try{
          stmt.close();
        } catch (Exception e){
          e.printStackTrace();
        }
      }
      if(conn != null){
        try{
          conn.close();
        } catch (Exception e){
          e.printStackTrace();
        }
      }
    }
  }
}

JDBC Control the transaction

1. Business : A business operation with multiple steps . If the business operation is managed by a transaction , Then these multiple steps will either succeed at the same time , Or fail at the same time .

  1. operation :

​ 1. Open transaction

​ 2. Commit transaction

​ 3. Roll back the transaction

  1. Use Connection Object to manage affairs

* Open transaction :setAutoCommit(boolean autoCommit) : Call this method to set the parameter to false, That is, start the transaction

* In execution sql Before opening the transaction

* Commit transaction :commit()

* When all sql All commit transactions are completed

* Roll back the transaction :rollback()

* stay catch Rollback transaction

 public class JDBCDemo {
 
            public static void main(String[] args) {
                Connection conn = null;
                PreparedStatement pstmt1 = null;
                PreparedStatement pstmt2 = null;
        
                try {
                    //1. Get the connection 
                    conn = JDBCUtils.getConnection();
                    // Open transaction 
                    conn.setAutoCommit(false);
        
                    //2. Definition sql
                    //2.1  Zhang San  - 500
                    String sql1 = "update account set balance = balance - ? where id = ?";
                    //2.2  Li Si  + 500
                    String sql2 = "update account set balance = balance + ? where id = ?";
                    //3. Access to perform sql object 
                    pstmt1 = conn.prepareStatement(sql1);
                    pstmt2 = conn.prepareStatement(sql2);
                    //4.  Set parameters 
                    pstmt1.setDouble(1,500);
                    pstmt1.setInt(2,1);
        
                    pstmt2.setDouble(1,500);
                    pstmt2.setInt(2,2);
                    //5. perform sql
                    pstmt1.executeUpdate();
                    //  Manual manufacturing abnormality 
                    int i = 3/0;
        
                    pstmt2.executeUpdate();
                    // Commit transaction 
                    conn.commit();
                } catch (Exception e) {
                    // Transaction rollback 
                    try {
                        if(conn != null) {
                            conn.rollback();
                        }
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                    e.printStackTrace();
                }finally {
                    JDBCUtils.close(pstmt1,conn);
                    JDBCUtils.close(pstmt2,null);
                }
            }
        }

SQL Inject

JDBCUtils.close(pstmt1,conn);
JDBCUtils.close(pstmt2,null);
}
}
}








### SQL Inject 

 When entering the account and password , Add at the end  "or"  And any statements that are true , thus , If it's true, it's true , In this way, you can log in successfully . This is called  sql Inject , It is deceiving the server to execute malicious SQL command 
原网站

版权声明
本文为[Jiaran is hungry three times a day]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121930516011.html