JavaEE學習中,PreparedStatement 的簡單使用 和 介紹(java 學習中的小記錄),preparedstatement

來源:互聯網
上載者:User

JavaEE學習中,PreparedStatement 的簡單使用 和 介紹(java 學習中的小記錄),preparedstatement

JavaEE學習中,PreparedStatement的簡單使用和介紹(java 學習中的小記錄)作者:王可利(Star·星星)

 

PreparedStatement

它是 Statement 的子類,分支。PreparedStatement 繼承於 Statement

 

資料庫情況

帳號:liubei

密碼:123

先看代碼示範問題所在:

 

 1 package TwoDay; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 import java.util.Scanner; 8  9 public class StarOne {10     public static void main(String[] args){11         Connection conn = null;12         Statement stmt = null;13         ResultSet rs  = null;14         15         //根據控制台提示輸入使用者名稱和密碼16         Scanner input = new Scanner(System.in);17         18         System.out.println("\t寵物主人登入");19         System.out.println("請輸入使用者名稱:");20         String name =input.next();21         System.out.println("請輸入密碼:");22         String password =input.next();23         24         try {25             Class.forName("com.mysql.jdbc.Driver");26             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/starstudy", "root", "123456");27             stmt = conn.createStatement();28             String sql = "SELECT *FROM `master` WHERE `name`= '"+name+"' AND `password`='"+password+"'";//加入參數的時候是:'"+變數+"'29             System.out.println(sql);30             //發現問題,把輸入的密碼注入到這個 sql語句裡面去了,如:我輸入的密碼是:123'or'1'='1'   31             //sql的語句就變成  :   SELECT *FROM `master` WHERE `name`= 'liubei' AND `password`='123'or'1'='1'32             //這種現象我們都叫做 使用Statment安全性差,存在SQL注入隱患(原因他用的是拼裝的語句)33             34             rs = stmt.executeQuery(sql);35             if (rs.next()) {36                 System.out.println("登陸成功!");37             }else{38                 System.out.println("登入失敗!");39             }40             41         } catch (Exception e) {42             // TODO: handle exception43         }finally{44             try {45                 if (null!=rs) {46                     rs.close();47                 }48                 if (null!=stmt) {49                     stmt.close();50                 }51                 if (null!=conn) {52                     conn.close();53                 }54             } catch (Exception e2) {55                 // TODO: handle exception56             }57         }58     }59 }

 

 

出錯總結:

如:當我輸入的密碼是:123'or'1'='1'   ,它把輸入的密碼注入到這個 sql語句裡面去了

於是  SQL的語句就變成  :   SELECT *FROM `master` WHERE `name`= 'liubei' AND `password`='123'or'1'='1'

程式運行成功,'1'='1' 是預設一定成立的。

這種現象我們都叫做 使用 Statment 安全性差,存在SQL注入隱患(原因它用的是拼裝的語句)

於是就有了 PreparedStatement 用來解決這個問題:

 

 

PreparedStatement 的使用步驟執行個體:

 

代碼的使用如下:

 1 package TwoDay; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6  7 public class StarTwo { 8     public static void main(String[] args){ 9         Connection conn = null;10         PreparedStatement pstmt= null;11         12         String sql = "UPDATE pet SET health=?,love=? WHERE id=?";//不知道的都給他預留位置 問號?13         14         try {15             Class.forName("com.mysql.jdbc.Driver");16             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/starstudy", "root", "123456");17             pstmt = conn.prepareStatement(sql);//執行SQL語句,先行編譯18             19             pstmt.setInt(1,1234); //這裡第幾個問號 就是 第幾個索引(第一個參數)20             pstmt.setInt(2, 88);21             pstmt.setInt(3, 2);22             23             pstmt.executeUpdate();//修改的方法24             25         } catch (Exception e) {26             27         }finally{28             try {29                 if (null!=pstmt) {30                     pstmt.close();31                 }32                 if (null!=conn) {33                     conn.close();34                 }35             } catch (Exception e2) {                36             }37         }38     }39 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.