JDBC中Statement接口實現修改數據、刪除數據
大家好,我是Java進階者,今天給大家繼續分享JDBC技術。
一、前言
一般來說,一個應用程序通常會與某個數據庫進行連接,并使用SQL語句和該數據庫中的表進行交互信息,例如修改數據、刪除數據等操作。本文給大家介紹的是如何使用Statement接口實現查詢修改數據、刪除數據,接下來,小編帶大家一起來學習!
二、操作數據庫
1.在Java語言中,使用Statement對象的executeUpdate()方法,來完成數據庫的數據處理操作。executeUpdate()方法是用于執行給定的SQL語句,如INSERT、UPDATE或DELETE語句,該方法的返回值是一個整數,該整數代表的意思是數據庫受到影響的行數。
2.操作數據庫實現的基本步驟,如下所示:
1)首先導入拓展包“mysql-connector-java-5.1.7-bin.jar”,在Ecilpse編輯軟件的當前項目右鍵選擇“Bulid Path”,再選擇“Configure Build Path...”,選擇Libraies,在右邊有個“Add External JARs...”按鈕把這個拓展包加進來,然后點擊“OK”。具體操作如下圖所示:
2)使用Class.forName()方法來加載驅動程序。
3)成功加載驅動程序后,Class.forName()方法向DriverManager注冊自己,接著使用getConnection()方法和數據庫進行連接,返回一個Connection對象。
4)使用Connection對象的createStatement()方法創建一個Statement對象。
5)使用Statement對象調用相應的方法查詢數據庫表,把查詢的結果存儲在一個ResultSet對象。
6)使用ResultSet對象的next()方法,獲取表中的數據。
三、通過一個案例了解Statement接口修改數據的用法
在上面介紹了操作數據庫實現的基本步驟,接下來,小編帶大家來了解Statement接口修改數據的用法,student表中的數據和代碼如下所示:
student表中的數據:
代碼:
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Example32 {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- System.out.println("請輸入ID:");
- String oldId=sc.next();
- System.out.println("請輸入你要修改的ID:");
- String newId=sc.next();
- String driver="com.mysql.jdbc.Driver";
- try {
- //加載驅動
- Class.forName(driver);
- //數據庫地址,本機、端口號3306、數據庫名為test
- String url="jdbc:mysql://localhost:3306/test";
- //用戶名
- String user="root";
- //密碼
- String pwd="168168";
- //連接數據庫
- Connection conn=DriverManager.getConnection(url,user,pwd);
- //創建Statement對象
- Statement stmt=conn.createStatement();
- String sql="update student set id='"+newId+"' where id='"+oldId+"'";
- //執行SQL語句
- stmt.executeUpdate(sql);
- sql="select * from student where id='"+newId+"'";
- //執行SQL語句
- ResultSet rs=stmt.executeQuery(sql);
- //根據ID值獲取數據
- if(rs.next()){
- System.out.println("id:"+rs.getString("id")+" name:"+rs.getString("name")+" age:"+rs.getInt("age"));
- }
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
效果圖如下所示:
表中的數據:
四、通過一個案例了解Statement接口刪除數據的用法
在上面介紹了操作數據庫實現的基本步驟,接下來,小編帶大家來了解Statement接口修改數據的用法,student表中的數據和代碼如下所示:
student表中的數據:
代碼:
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Scanner;
- public class Example33 {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- System.out.println("請輸入你要刪除的ID:");
- String del_id=sc.next();
- String driver="com.mysql.jdbc.Driver";
- try {
- //加載驅動
- Class.forName(driver);
- //數據庫地址,本機、端口號3306、數據庫名為test
- String url="jdbc:mysql://localhost:3306/test";
- //用戶名
- String user="root";
- //密碼
- String pwd="168168";
- //連接數據庫
- Connection conn=DriverManager.getConnection(url,user,pwd);
- //創建Statement對象
- Statement stmt=conn.createStatement();
- String sql="delete from student where id='"+del_id+"'";
- //執行SQL語句
- stmt.executeUpdate(sql);
- sql="select * from student";
- //執行SQL語句
- ResultSet rs=stmt.executeQuery(sql);
- //獲取表中所有數據
- while(rs.next()){
- System.out.println("id:"+rs.getString("id")+" name:"+rs.getString("name")+" age:"+rs.getInt("age"));
- }
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
效果圖如下所示:
表中的數據:
五、總結
1.本文介紹了Statement接口實現修改數據、刪除數據。
2.在Java中,使用Statement對象的executeUpdate()方法,來完成數據庫的數據處理操作。文本介紹了操作數據庫實現的基本步驟,重點在于使用getConnection()方法來連接數據庫,創建Statement對象,調用Connection對象的createStatement()方法創建這個MySQL語句對象,之后該對象調用executeUpdate方法來進行具體的數據處理。
3.根據文中的操作數據庫實現的基本步驟方法,通過一個案例來幫助大家了解Statement接口修改和刪除數據的用法。
4.希望大家通過本文的學習,對你有所幫助!