JDBC中Statement接口實(shí)現(xiàn)修改數(shù)據(jù)、刪除數(shù)據(jù)
后臺(tái)回復(fù)“Java”即可獲贈(zèng)Java學(xué)習(xí)資料
大家好,我是Java進(jìn)階者,今天給大家繼續(xù)分享JDBC技術(shù)。
一、前言
一般來(lái)說(shuō),一個(gè)應(yīng)用程序通常會(huì)與某個(gè)數(shù)據(jù)庫(kù)進(jìn)行連接,并使用SQL語(yǔ)句和該數(shù)據(jù)庫(kù)中的表進(jìn)行交互信息,例如修改數(shù)據(jù)、刪除數(shù)據(jù)等操作。本文給大家介紹的是如何使用Statement接口實(shí)現(xiàn)查詢修改數(shù)據(jù)、刪除數(shù)據(jù),接下來(lái),小編帶大家一起來(lái)學(xué)習(xí)!
二、操作數(shù)據(jù)庫(kù)
1.在Java語(yǔ)言中,使用Statement對(duì)象的executeUpdate()方法,來(lái)完成數(shù)據(jù)庫(kù)的數(shù)據(jù)處理操作。executeUpdate()方法是用于執(zhí)行給定的SQL語(yǔ)句,如INSERT、UPDATE或DELETE語(yǔ)句,該方法的返回值是一個(gè)整數(shù),該整數(shù)代表的意思是數(shù)據(jù)庫(kù)受到影響的行數(shù)。
2.操作數(shù)據(jù)庫(kù)實(shí)現(xiàn)的基本步驟,如下所示:
1)首先導(dǎo)入拓展包“mysql-connector-java-5.1.7-bin.jar”,在Ecilpse編輯軟件的當(dāng)前項(xiàng)目右鍵選擇“Bulid Path”,再選擇“Configure Build Path...”,選擇Libraies,在右邊有個(gè)“Add External JARs...”按鈕把這個(gè)拓展包加進(jìn)來(lái),然后點(diǎn)擊“OK”。具體操作如下圖所示:


2)使用Class.forName()方法來(lái)加載驅(qū)動(dòng)程序。
3)成功加載驅(qū)動(dòng)程序后,Class.forName()方法向DriverManager注冊(cè)自己,接著使用getConnection()方法和數(shù)據(jù)庫(kù)進(jìn)行連接,返回一個(gè)Connection對(duì)象。
4)使用Connection對(duì)象的createStatement()方法創(chuàng)建一個(gè)Statement對(duì)象。
5)使用Statement對(duì)象調(diào)用相應(yīng)的方法查詢數(shù)據(jù)庫(kù)表,把查詢的結(jié)果存儲(chǔ)在一個(gè)ResultSet對(duì)象。
6)使用ResultSet對(duì)象的next()方法,獲取表中的數(shù)據(jù)。
三、通過(guò)一個(gè)案例了解Statement接口修改數(shù)據(jù)的用法
在上面介紹了操作數(shù)據(jù)庫(kù)實(shí)現(xiàn)的基本步驟,接下來(lái),小編帶大家來(lái)了解Statement接口修改數(shù)據(jù)的用法,student表中的數(shù)據(jù)和代碼如下所示:
student表中的數(shù)據(jù):

代碼:
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("請(qǐng)輸入ID:");String oldId=sc.next();System.out.println("請(qǐng)輸入你要修改的ID:");String newId=sc.next();String driver="com.mysql.jdbc.Driver";try {//加載驅(qū)動(dòng)Class.forName(driver);//數(shù)據(jù)庫(kù)地址,本機(jī)、端口號(hào)3306、數(shù)據(jù)庫(kù)名為testString url="jdbc:mysql://localhost:3306/test";//用戶名String user="root";//密碼String pwd="168168";//連接數(shù)據(jù)庫(kù)Connection conn=DriverManager.getConnection(url,user,pwd);//創(chuàng)建Statement對(duì)象Statement stmt=conn.createStatement();String sql="update student set id='"+newId+"' where id='"+oldId+"'";//執(zhí)行SQL語(yǔ)句stmt.executeUpdate(sql);sql="select * from student where id='"+newId+"'";//執(zhí)行SQL語(yǔ)句ResultSet rs=stmt.executeQuery(sql);//根據(jù)ID值獲取數(shù)據(jù)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 blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
效果圖如下所示:

表中的數(shù)據(jù):

四、通過(guò)一個(gè)案例了解Statement接口刪除數(shù)據(jù)的用法
在上面介紹了操作數(shù)據(jù)庫(kù)實(shí)現(xiàn)的基本步驟,接下來(lái),小編帶大家來(lái)了解Statement接口修改數(shù)據(jù)的用法,student表中的數(shù)據(jù)和代碼如下所示:
student表中的數(shù)據(jù):

代碼:
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("請(qǐng)輸入你要?jiǎng)h除的ID:");String del_id=sc.next();String driver="com.mysql.jdbc.Driver";try {//加載驅(qū)動(dòng)Class.forName(driver);//數(shù)據(jù)庫(kù)地址,本機(jī)、端口號(hào)3306、數(shù)據(jù)庫(kù)名為testString url="jdbc:mysql://localhost:3306/test";//用戶名String user="root";//密碼String pwd="168168";//連接數(shù)據(jù)庫(kù)Connection conn=DriverManager.getConnection(url,user,pwd);//創(chuàng)建Statement對(duì)象Statement stmt=conn.createStatement();String sql="delete from student where id='"+del_id+"'";//執(zhí)行SQL語(yǔ)句stmt.executeUpdate(sql);sql="select * from student";//執(zhí)行SQL語(yǔ)句ResultSet rs=stmt.executeQuery(sql);//獲取表中所有數(shù)據(jù)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 blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
效果圖如下所示:

表中的數(shù)據(jù):

五、總結(jié)
1.本文介紹了Statement接口實(shí)現(xiàn)修改數(shù)據(jù)、刪除數(shù)據(jù)。
2.在Java中,使用Statement對(duì)象的executeUpdate()方法,來(lái)完成數(shù)據(jù)庫(kù)的數(shù)據(jù)處理操作。文本介紹了操作數(shù)據(jù)庫(kù)實(shí)現(xiàn)的基本步驟,重點(diǎn)在于使用getConnection()方法來(lái)連接數(shù)據(jù)庫(kù),創(chuàng)建Statement對(duì)象,調(diào)用Connection對(duì)象的createStatement()方法創(chuàng)建這個(gè)MySQL語(yǔ)句對(duì)象,之后該對(duì)象調(diào)用executeUpdate方法來(lái)進(jìn)行具體的數(shù)據(jù)處理。
3.根據(jù)文中的操作數(shù)據(jù)庫(kù)實(shí)現(xiàn)的基本步驟方法,通過(guò)一個(gè)案例來(lái)幫助大家了解Statement接口修改和刪除數(shù)據(jù)的用法。
4.希望大家通過(guò)本文的學(xué)習(xí),對(duì)你有所幫助!
最后需要拓展包的小伙伴,可以在公眾號(hào)后臺(tái)回復(fù)“拓展包”關(guān)鍵字進(jìn)行獲取。
我是Java進(jìn)階者,希望大家通過(guò)本文的學(xué)習(xí),對(duì)你有所幫助!歡迎大家加我微信,有問(wèn)題可以隨時(shí)幫大家解決噢,交個(gè)朋友也好哇~
------------------- End -------------------
往期精彩文章推薦:

歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
想加入Java學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群】
萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行
