java之jdbc_採用PreparedStatement根據條件查詢

動力節點筆記java

 

  
  
  
  
  1. import java.sql.*;  
  2.  
  3. //採用PreparedStatement根據條件查詢  
  4. public class QueryTest05 {  
  5.     public static void main(String[] args) {  
  6.         if (args.length == 0) {  
  7.             throw new IllegalArgumentException("參數非法,正確使用爲: Java QueryTest05 + 參數");  
  8.         }  
  9.           
  10.         Connection conn = null;  
  11.         PreparedStatement pstmt = null;  
  12.         ResultSet rs = null;  
  13.         try {  
  14.             //第一步,加載數據庫驅動,不一樣的數據庫驅動程序不同  
  15.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  16.             //第二部,獲得數據庫鏈接  
  17.             String dburl = "jdbc:oracle:thin:@localhost:1521:orcl";  
  18.             //String dburl = "jdbc:oracle:thin:@192.168.21.1:1521:orcl";  
  19.             //String dburl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
  20.             String userName = "system";  
  21.             String password = "wanwan";  
  22.             conn = DriverManager.getConnection(dburl, userName, password);  
  23.             //System.out.println(conn);  
  24.               
  25.             //第三步,獲得PreparedStatement,執行sql  
  26.             //採用PreparedStatement不建議採用拼串的方式  
  27.             //由於拼串每次的SQL語句不同,這樣就沒有利用PreparedStatement的優勢  
  28.             //仍然每次都編譯,和Statement沒什麼差異  
  29.             //String sql = "select * from tb_student where sex like '" + args[0] + "'";  
  30.             //pstmt = conn.prepareStatement(sql);  
  31.               
  32.             //建議採用佔位符的方式  
  33.             //這樣每次SQL語句是同樣的,數據庫就不會再編譯  
  34.             //這樣就利用了PreparedStatement的優勢,顯著地提升性能  
  35.             //瞭解Statement和PreparedStatement的區別  
  36.             String sql = "select * from tb_student where sex like ?";  
  37.             pstmt = conn.prepareStatement(sql);  
  38.             //注意:此處不要加單引號  
  39.             pstmt.setString(1, args[0]);  
  40.             //第四部,取得結果集  
  41.             //將條件作爲sql語句的一部分進行拼接  
  42.             //注意字符串必須採用單引號引發來  
  43.             rs = pstmt.executeQuery();  
  44.             //System.out.println("sql" + sql);  
  45.             while (rs.next()) {  
  46.                 int id = rs.getInt("id");  
  47.                 String name = rs.getString("name");  
  48.                 System.out.println(id + " , " + name);  
  49.             }   
  50.               
  51.               
  52.         } catch (ClassNotFoundException e) {  
  53.             e.printStackTrace();  
  54.         } catch (SQLException e) {  
  55.             e.printStackTrace();  
  56.         } finally {  
  57.             //注意關閉原則:從裏到外  
  58.               
  59.               
  60.             try {  
  61.                 if (rs != null) {  
  62.                 rs.close();   
  63.                 }  
  64.                 if (pstmt != null) {  
  65.                     pstmt.close();    
  66.                 }  
  67.                 if (conn != null) {  
  68.                     conn.close();  
  69.                 }  
  70.             } catch(SQLException e) {  
  71.                           
  72.             }  
  73.               
  74.         }  
  75.     }     
  76. }  
相關文章
相關標籤/搜索