偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

JDBC中Statement接口實現(xiàn)修改數(shù)據(jù)、刪除數(shù)據(jù)

開發(fā) 后端 其他數(shù)據(jù)庫
文本介紹了操作數(shù)據(jù)庫實現(xiàn)的基本步驟,重點在于使用getConnection()方法來連接數(shù)據(jù)庫,創(chuàng)建Statement對象,調(diào)用Connection對象的createStatement()方法創(chuàng)建這個MySQL語句對象,之后該對象調(diào)用executeUpdate方法來進(jìn)行具體的數(shù)據(jù)處理。

[[406786]]

大家好,我是Java進(jìn)階者,今天給大家繼續(xù)分享JDBC技術(shù)。

一、前言

一般來說,一個應(yīng)用程序通常會與某個數(shù)據(jù)庫進(jìn)行連接,并使用SQL語句和該數(shù)據(jù)庫中的表進(jìn)行交互信息,例如修改數(shù)據(jù)、刪除數(shù)據(jù)等操作。本文給大家介紹的是如何使用Statement接口實現(xiàn)查詢修改數(shù)據(jù)、刪除數(shù)據(jù),接下來,小編帶大家一起來學(xué)習(xí)!

二、操作數(shù)據(jù)庫

1.在Java語言中,使用Statement對象的executeUpdate()方法,來完成數(shù)據(jù)庫的數(shù)據(jù)處理操作。executeUpdate()方法是用于執(zhí)行給定的SQL語句,如INSERT、UPDATE或DELETE語句,該方法的返回值是一個整數(shù),該整數(shù)代表的意思是數(shù)據(jù)庫受到影響的行數(shù)。

2.操作數(shù)據(jù)庫實現(xiàn)的基本步驟,如下所示:

1)首先導(dǎo)入拓展包“mysql-connector-java-5.1.7-bin.jar”,在Ecilpse編輯軟件的當(dāng)前項目右鍵選擇“Bulid Path”,再選擇“Configure Build Path...”,選擇Libraies,在右邊有個“Add External JARs...”按鈕把這個拓展包加進(jìn)來,然后點擊“OK”。具體操作如下圖所示:

2)使用Class.forName()方法來加載驅(qū)動程序。

3)成功加載驅(qū)動程序后,Class.forName()方法向DriverManager注冊自己,接著使用getConnection()方法和數(shù)據(jù)庫進(jìn)行連接,返回一個Connection對象。

4)使用Connection對象的createStatement()方法創(chuàng)建一個Statement對象。

5)使用Statement對象調(diào)用相應(yīng)的方法查詢數(shù)據(jù)庫表,把查詢的結(jié)果存儲在一個ResultSet對象。

6)使用ResultSet對象的next()方法,獲取表中的數(shù)據(jù)。

三、通過一個案例了解Statement接口修改數(shù)據(jù)的用法

在上面介紹了操作數(shù)據(jù)庫實現(xiàn)的基本步驟,接下來,小編帶大家來了解Statement接口修改數(shù)據(jù)的用法,student表中的數(shù)據(jù)和代碼如下所示:

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

代碼:

  1. import java.sql.Connection
  2. import java.sql.DriverManager; 
  3. import java.sql.ResultSet; 
  4. import java.sql.SQLException; 
  5. import java.sql.Statement; 
  6. import java.util.Scanner; 
  7.  
  8. public class Example32 { 
  9.  
  10.     public static void main(String[] args) { 
  11.         Scanner sc=new Scanner(System.in); 
  12.         System.out.println("請輸入ID:"); 
  13.         String oldId=sc.next(); 
  14.         System.out.println("請輸入你要修改的ID:"); 
  15.         String newId=sc.next(); 
  16.         String driver="com.mysql.jdbc.Driver"
  17.         try { 
  18.             //加載驅(qū)動 
  19.             Class.forName(driver); 
  20.             //數(shù)據(jù)庫地址,本機、端口號3306、數(shù)據(jù)庫名為test 
  21.             String url="jdbc:mysql://localhost:3306/test"
  22.             //用戶名 
  23.             String user="root"
  24.             //密碼 
  25.             String pwd="168168"
  26.             //連接數(shù)據(jù)庫 
  27.             Connection conn=DriverManager.getConnection(url,user,pwd); 
  28.             //創(chuàng)建Statement對象 
  29.             Statement stmt=conn.createStatement(); 
  30.             String sql="update student set id='"+newId+"' where id='"+oldId+"'"
  31.             //執(zhí)行SQL語句 
  32.             stmt.executeUpdate(sql); 
  33.             sql="select * from student where id='"+newId+"'"
  34.             //執(zhí)行SQL語句 
  35.             ResultSet rs=stmt.executeQuery(sql); 
  36.             //根據(jù)ID值獲取數(shù)據(jù) 
  37.             if(rs.next()){ 
  38.                 System.out.println("id:"+rs.getString("id")+"  name:"+rs.getString("name")+"  age:"+rs.getInt("age")); 
  39.             } 
  40.         } catch (ClassNotFoundException e) { 
  41.             // TODO Auto-generated catch block 
  42.             e.printStackTrace(); 
  43.         } catch (SQLException e) { 
  44.             // TODO Auto-generated catch block 
  45.             e.printStackTrace(); 
  46.         } 
  47.     } 

效果圖如下所示:

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

四、通過一個案例了解Statement接口刪除數(shù)據(jù)的用法

在上面介紹了操作數(shù)據(jù)庫實現(xiàn)的基本步驟,接下來,小編帶大家來了解Statement接口修改數(shù)據(jù)的用法,student表中的數(shù)據(jù)和代碼如下所示:

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

代碼:

  1. import java.sql.Connection
  2. import java.sql.DriverManager; 
  3. import java.sql.ResultSet; 
  4. import java.sql.SQLException; 
  5. import java.sql.Statement; 
  6. import java.util.Scanner; 
  7.  
  8. public class Example33 { 
  9.  
  10.     public static void main(String[] args) { 
  11.         Scanner sc=new Scanner(System.in); 
  12.         System.out.println("請輸入你要刪除的ID:"); 
  13.         String del_id=sc.next(); 
  14.         String driver="com.mysql.jdbc.Driver"
  15.         try { 
  16.             //加載驅(qū)動 
  17.             Class.forName(driver); 
  18.             //數(shù)據(jù)庫地址,本機、端口號3306、數(shù)據(jù)庫名為test 
  19.             String url="jdbc:mysql://localhost:3306/test"
  20.             //用戶名 
  21.             String user="root"
  22.             //密碼 
  23.             String pwd="168168"
  24.             //連接數(shù)據(jù)庫 
  25.             Connection conn=DriverManager.getConnection(url,user,pwd); 
  26.             //創(chuàng)建Statement對象 
  27.             Statement stmt=conn.createStatement(); 
  28.             String sql="delete from student where id='"+del_id+"'"
  29.             //執(zhí)行SQL語句 
  30.             stmt.executeUpdate(sql); 
  31.             sql="select * from student"
  32.             //執(zhí)行SQL語句 
  33.             ResultSet rs=stmt.executeQuery(sql); 
  34.             //獲取表中所有數(shù)據(jù) 
  35.             while(rs.next()){ 
  36.                 System.out.println("id:"+rs.getString("id")+"  name:"+rs.getString("name")+"  age:"+rs.getInt("age")); 
  37.             } 
  38.         } catch (ClassNotFoundException e) { 
  39.             // TODO Auto-generated catch block 
  40.             e.printStackTrace(); 
  41.         } catch (SQLException e) { 
  42.             // TODO Auto-generated catch block 
  43.             e.printStackTrace(); 
  44.         } 
  45.     } 

效果圖如下所示:

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

五、總結(jié)

1.本文介紹了Statement接口實現(xiàn)修改數(shù)據(jù)、刪除數(shù)據(jù)。

2.在Java中,使用Statement對象的executeUpdate()方法,來完成數(shù)據(jù)庫的數(shù)據(jù)處理操作。文本介紹了操作數(shù)據(jù)庫實現(xiàn)的基本步驟,重點在于使用getConnection()方法來連接數(shù)據(jù)庫,創(chuàng)建Statement對象,調(diào)用Connection對象的createStatement()方法創(chuàng)建這個MySQL語句對象,之后該對象調(diào)用executeUpdate方法來進(jìn)行具體的數(shù)據(jù)處理。

3.根據(jù)文中的操作數(shù)據(jù)庫實現(xiàn)的基本步驟方法,通過一個案例來幫助大家了解Statement接口修改和刪除數(shù)據(jù)的用法。

4.希望大家通過本文的學(xué)習(xí),對你有所幫助!

 

責(zé)任編輯:姜華 來源: Java進(jìn)階學(xué)習(xí)交流
相關(guān)推薦

2021-06-03 10:01:28

JDBCStatement接口

2021-05-21 10:01:01

JDBCJavaStatement接口

2011-05-18 15:08:03

mysql刪除修改數(shù)據(jù)

2009-07-06 17:36:06

ResultSetJDBC Connec

2021-05-13 07:58:05

JDBC接口PreparedSta

2010-10-22 16:40:27

SQL TRUNCAT

2009-09-14 10:45:33

LINQ刪除數(shù)據(jù)

2017-08-22 16:40:22

前端JavaScript接口

2010-06-18 15:33:19

UML接口

2009-08-17 08:33:00

Visual C#數(shù)據(jù)

2009-07-16 14:46:48

jdbc statem

2011-02-21 10:35:00

查詢刪除數(shù)據(jù)

2010-10-21 14:12:07

SQL Server游

2010-04-19 08:51:30

2010-07-08 10:28:51

UML接口

2011-07-20 13:18:01

SQLite數(shù)據(jù)庫修改和刪除數(shù)據(jù)

2011-05-26 12:54:31

數(shù)據(jù)庫數(shù)據(jù)庫名

2009-09-04 17:56:22

C#刪除數(shù)據(jù)

2019-12-20 14:56:50

批量刪除數(shù)據(jù)數(shù)據(jù)刪除

2009-11-13 11:18:22

ADO.NET修改數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號