Java刪除資料庫中的資料

來源:互聯網
上載者:User

標籤:資料   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刪除資料庫中的資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.