Java刪除數據庫中的數據


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 stub
13         try {
14             Class.forName("com.mysql.jdbc.Driver");//加載數據庫驅動
15             System.out.println("加載數據庫驅動成功");
16             String url="jdbc:mysql://localhost:3306/test";//聲明自己的數據庫test的url
17             String user="root";//聲明自己的數據庫賬號
18             String password="123456";//聲明自己的數據庫密碼
19             //建立數據庫連接,獲得連接對象conn
20             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 block
30             e.printStackTrace();
31         } catch (SQLException e) {
32             // TODO Auto-generated catch block
33             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 stub
13         try {
14             Class.forName("com.mysql.jdbc.Driver");//加載數據庫驅動
15             System.out.println("加載數據庫驅動成功");
16             String url="jdbc:mysql://localhost:3306/test";//聲明自己的數據庫test的url
17             String user="root";//聲明自己的數據庫賬號
18             String password="123456";//聲明自己的數據庫密碼
19             //建立數據庫連接,獲得連接對象conn
20             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 block
30             e.printStackTrace();
31         } catch (SQLException e) {
32             // TODO Auto-generated catch block
33             e.printStackTrace();
34         }
35         
36         
37     }
38 
39 }

 

 

 

至此,java中使用jdbc操作數據庫的增刪改查全部操作完畢,參考者可以在上下篇隨筆中參考,熟悉練習和使用jdbc操作數據庫,理清操作思路,為以后學習更深打好基礎

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM