java之jdbc_採用Statement刪除數據

動力節點筆記java

 

  
  
  
  
  1. import java.sql.*;  
  2. //採用Statement刪除數據  
  3. public class DeleteTest01 {  
  4.     public static void main(String[] args) {  
  5.         if (args.length != 1) {  
  6.             throw new IllegalArgumentException("參數非法,正確使用爲InsertTest01 值");   
  7.         }  
  8.           
  9.         Connection conn = null;  
  10.         Statement stmt = null;  
  11.         try {  
  12.             //第一步,加載數據庫驅動,不一樣的數據庫驅動程序不同  
  13.             Class.forName("oracle.jdbc.driver.OracleDriver");  
  14.             //第二部,獲得數據庫鏈接  
  15.             String dburl = "jdbc:oracle:thin:@localhost:1521:orcl";  
  16.             //String dburl = "jdbc:oracle:thin:@192.168.21.1:1521:orcl";  
  17.             //String dburl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
  18.             String userName = "system";  
  19.             String password = "wanwan";  
  20.             conn = DriverManager.getConnection(dburl, userName, password);  
  21.             //System.out.println(conn);  
  22.               
  23.             //取得輸入參數  
  24.             int empno = Integer.parseInt(args[0]);  
  25.               
  26.             //第三步,建立Statement,執行SQL語句  
  27.             //拼串  
  28.             //最好將比較複雜的sql調通,再將sql語句和程序相結合,進行連調  
  29.             String sql = "delete from enp where empno = " + empno;  
  30.               
  31.             System.out.println("sql = " + sql);  
  32.             stmt = conn.createStatement();  
  33.             stmt.executeUpdate(sql);  
  34.             System.out.println("刪除員工成功, 員工代碼:【" + empno +"】");        
  35.         } catch (ClassNotFoundException e) {  
  36.             e.printStackTrace();  
  37.         } catch (SQLException e) {  
  38.             e.printStackTrace();  
  39.         } finally {  
  40.             //注意關閉原則:從裏到外  
  41.             try {  
  42.                 if (stmt != null) {  
  43.                     stmt.close();     
  44.                 }  
  45.                 if (conn != null) {  
  46.                     conn.close();  
  47.                 }  
  48.             } catch(SQLException e) {  
  49.                           
  50.             }  
  51.               
  52.         }  
  53.     }     
  54. }  
  55.  
  56.  
  57. /*  
  58.     注意在pl/sql developer中,修改數據後要commit後,在其餘終端下才能對相應的記錄操做  
  59. */
相關文章
相關標籤/搜索