動力節點筆記java
- import java.sql.*;
- //採用Statement刪除數據
- public class DeleteTest01 {
- public static void main(String[] args) {
- if (args.length != 1) {
- throw new IllegalArgumentException("參數非法,正確使用爲InsertTest01 值");
- }
- Connection conn = null;
- Statement stmt = null;
- try {
- //第一步,加載數據庫驅動,不一樣的數據庫驅動程序不同
- Class.forName("oracle.jdbc.driver.OracleDriver");
- //第二部,獲得數據庫鏈接
- String dburl = "jdbc:oracle:thin:@localhost:1521:orcl";
- //String dburl = "jdbc:oracle:thin:@192.168.21.1:1521:orcl";
- //String dburl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
- String userName = "system";
- String password = "wanwan";
- conn = DriverManager.getConnection(dburl, userName, password);
- //System.out.println(conn);
- //取得輸入參數
- int empno = Integer.parseInt(args[0]);
- //第三步,建立Statement,執行SQL語句
- //拼串
- //最好將比較複雜的sql調通,再將sql語句和程序相結合,進行連調
- String sql = "delete from enp where empno = " + empno;
- System.out.println("sql = " + sql);
- stmt = conn.createStatement();
- stmt.executeUpdate(sql);
- System.out.println("刪除員工成功, 員工代碼:【" + empno +"】");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- //注意關閉原則:從裏到外
- try {
- if (stmt != null) {
- stmt.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch(SQLException e) {
- }
- }
- }
- }
- /*
- 注意在pl/sql developer中,修改數據後要commit後,在其餘終端下才能對相應的記錄操做
- */