標籤:資料 gen 語句 nbsp pack port main except rac
1:刪除資料庫中資料表中的資料同樣也是一個非常用的技術,使用executeUpdate()方法執行用來做刪除SQL的語句可以刪除資料庫表中的資料
2:本案例使用Statement介面中的executeUpdate()方法,刪除資料庫中users表中id為1的使用者資訊
1 package com.ningmeng; 2 3 import java.sql.*; 4 /** 5 * 6 * @author biexiansheng 7 * 8 */ 9 public class Test06 {10 11 public static void main(String[] args) {12 // TODO Auto-generated method stub13 try {14 Class.forName("com.mysql.jdbc.Driver");//載入資料庫驅動15 System.out.println("載入資料庫驅動成功");16 String url="jdbc:mysql://localhost:3306/test";//聲明自己的資料庫test的url17 String user="root";//聲明自己的資料庫帳號18 String password="123456";//聲明自己的資料庫密碼19 //建立資料庫連接,獲得連線物件conn20 Connection conn=DriverManager.getConnection(url,user,password);21 System.out.println("串連資料庫成功");22 String sql="delete from users where id=1";//產生一條sql語句23 Statement stmt=conn.createStatement();//建立Statement對象24 stmt.executeUpdate(sql);//執行sql語句25 System.out.println("資料庫刪除成功");26 conn.close();27 System.out.println("資料庫關閉成功");//關閉資料庫的串連28 } catch (ClassNotFoundException e) {29 // TODO Auto-generated catch block30 e.printStackTrace();31 } catch (SQLException e) {32 // TODO Auto-generated catch block33 e.printStackTrace();34 }35 36 37 }38 39 }
3:大量刪除操作
1 package com.ningmeng; 2 3 import java.sql.*; 4 /** 5 * 6 * @author biexiansheng 7 * 8 */ 9 public class Test06 {10 11 public static void main(String[] args) {12 // TODO Auto-generated method stub13 try {14 Class.forName("com.mysql.jdbc.Driver");//載入資料庫驅動15 System.out.println("載入資料庫驅動成功");16 String url="jdbc:mysql://localhost:3306/test";//聲明自己的資料庫test的url17 String user="root";//聲明自己的資料庫帳號18 String password="123456";//聲明自己的資料庫密碼19 //建立資料庫連接,獲得連線物件conn20 Connection conn=DriverManager.getConnection(url,user,password);21 System.out.println("串連資料庫成功");22 String sql="delete from users where sex=2";//產生一條sql語句23 Statement stmt=conn.createStatement();//建立Statement對象24 stmt.executeUpdate(sql);//執行sql語句25 System.out.println("資料庫刪除成功");26 conn.close();27 System.out.println("資料庫關閉成功");//關閉資料庫的串連28 } catch (ClassNotFoundException e) {29 // TODO Auto-generated catch block30 e.printStackTrace();31 } catch (SQLException e) {32 // TODO Auto-generated catch block33 e.printStackTrace();34 }35 36 37 }38 39 }
至此,java中使用jdbc操作資料庫的增刪改查全部操作完畢,參考者可以在上下篇隨筆中參考,熟悉練習和使用jdbc操作資料庫,理清操作思路,為以後學習更深打好基礎
Java刪除資料庫中的資料