自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JDBC中Statement接口實現(xiàn)查詢數(shù)據(jù)、添加數(shù)據(jù)

開發(fā) 后端
本文介紹了Statement接口實現(xiàn)查詢數(shù)據(jù)、添加數(shù)據(jù)。在JDBC的基本應用中,介紹了使用Statement接口查詢和添加數(shù)據(jù)的步驟。重點在于使用getConnection()方法來連接數(shù)據(jù)庫,創(chuàng)建Statement對象,調用Connection對象的createStatement()方法創(chuàng)建這個MySQL語句對象。

[[400871]]

大家好,我是Java進階者,今天給大家介紹的是如何使用Statement接口實現(xiàn)查詢查詢數(shù)據(jù)、添加數(shù)據(jù)。

一、前言

在JDBC技術中,不同的數(shù)據(jù)庫需要不同的驅動程序,先加載驅動程序,接著數(shù)據(jù)庫的連接后,再使用SQL語句來執(zhí)行數(shù)據(jù)庫。本文給大家介紹的是如何使用Statement接口實現(xiàn)查詢查詢數(shù)據(jù)、添加數(shù)據(jù),接下來,小編帶大家一起來學習!

二、JDBC的基本應用

1.在程序中和某個數(shù)據(jù)庫進行連接之后,可以使用SQL語句和該數(shù)據(jù)庫中的表進行交互信息,例如,通過增、刪、改、查的方式來操作表中的記錄。這些交互是通過JDBC的一個API接口實現(xiàn)的。JDBC提供的Statement接口向數(shù)據(jù)庫發(fā)送SQL語句,執(zhí)行SQL語句返回一個結果保存在一個ResultSet對象中,調用該對象的next()方法來獲取數(shù)據(jù)。

2.使用Statement接口查詢數(shù)據(jù)的步驟如下所示:

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()方法和數(shù)據(jù)庫進行連接,返回一個Connection對象。

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

5)使用Statement對象調用executeQuery()方法查詢數(shù)據(jù)庫表,把查詢的結果存儲在一個ResultSet對象。

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

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

1.在上面介紹了Statement接口查詢數(shù)據(jù)的具體步驟,接下來,小編帶著大家一起來了解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 Example30 { 
  9.  
  10.     public static void main(String[] args) { 
  11.         System.out.println("請輸入你要查詢的ID:"); 
  12.         Scanner sc=new Scanner(System.in); 
  13.         String input=sc.next(); 
  14.         String driver="com.mysql.jdbc.Driver"
  15.         try { 
  16.             //加載驅動 
  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="select * from student where id='"+input+"'"
  29.             //執(zhí)行SQL語句 
  30.             ResultSet rs=stmt.executeQuery(sql); 
  31.             //根據(jù)用戶輸入的ID值獲取數(shù)據(jù) 
  32.             if(rs.next()){ 
  33.                 System.out.println("id:"+rs.getString("id")+"  name:"+rs.getString("name")+"  age:"+rs.getInt("age")); 
  34.             }else
  35.                 System.out.println("你輸入的ID不存在!"); 
  36.             } 
  37.         } catch (ClassNotFoundException e) { 
  38.             // TODO Auto-generated catch block 
  39.             e.printStackTrace(); 
  40.         } catch (SQLException e) { 
  41.             // TODO Auto-generated catch block 
  42.             e.printStackTrace(); 
  43.         } 
  44.     } 

效果圖如下所示:

在上面代碼中,首先是加載驅動程序,之后使用getConnection()方法來連接數(shù)據(jù)庫,創(chuàng)建Statement對象,調用Connection對象的createStatement()方法創(chuàng)建這個MySQL語句對象,在這個對象調用executeQuery()方法來處理查詢的結果。

四、Statement接口添加數(shù)據(jù)實現(xiàn)步驟

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()方法和數(shù)據(jù)庫進行連接,返回一個Connection對象。

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

5.使用Statement對象調用executeUpdate()方法查詢數(shù)據(jù)庫表,把查詢的結果存儲在一個ResultSet對象。

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

五、通過一個案例了解Statement接口添加數(shù)據(jù)的用法

1.在上面介紹了Statement接口添加數(shù)據(jù)的實現(xiàn)步驟,接下來,小編帶著大家一起來了解Statement接口添加數(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 Example31 { 
  9.  
  10.     public static void main(String[] args) { 
  11.         Scanner sc=new Scanner(System.in); 
  12.         System.out.println("請輸入你要添加的ID:"); 
  13.         String id=sc.next(); 
  14.         System.out.println("請輸入你要添加的Name:"); 
  15.         String name=sc.next(); 
  16.         System.out.println("請輸入你要添加的Age:"); 
  17.         int age=sc.nextInt(); 
  18.         String driver="com.mysql.jdbc.Driver"
  19.         try { 
  20.             //加載驅動 
  21.             Class.forName(driver); 
  22.             //數(shù)據(jù)庫地址,本機、端口號3306、數(shù)據(jù)庫名為test 
  23.             String url="jdbc:mysql://localhost:3306/test"
  24.             //用戶名 
  25.             String user="root"
  26.             //密碼 
  27.             String pwd="168168"
  28.             //連接數(shù)據(jù)庫 
  29.             Connection conn=DriverManager.getConnection(url,user,pwd); 
  30.             //創(chuàng)建Statement對象 
  31.             Statement stmt=conn.createStatement(); 
  32.             String sql="insert into student values('"+id+"','"+name+"',"+age+")"
  33.             //執(zhí)行SQL語句 
  34.             stmt.executeUpdate(sql); 
  35.             sql="select * from student where id='"+id+"'"
  36.             //執(zhí)行SQL語句 
  37.             ResultSet rs=stmt.executeQuery(sql); 
  38.             //根據(jù)ID值獲取數(shù)據(jù) 
  39.             if(rs.next()){ 
  40.                 System.out.println("id:"+rs.getString("id")+"  name:"+rs.getString("name")+"  age:"+rs.getInt("age")); 
  41.             } 
  42.         } catch (ClassNotFoundException e) { 
  43.             // TODO Auto-generated catch block 
  44.             e.printStackTrace(); 
  45.         } catch (SQLException e) { 
  46.             // TODO Auto-generated catch block 
  47.             e.printStackTrace(); 
  48.         } 
  49.     } 

效果圖如下所示:

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

在上面代碼中,首先是加載驅動程序,之后使用getConnection()方法來連接數(shù)據(jù)庫,創(chuàng)建Statement對象,調用Connection對象的createStatement()方法創(chuàng)建這個MySQL語句對象,在這個對象調用executeUpdate方法來處理。

六、總結

1.本文介紹了Statement接口實現(xiàn)查詢數(shù)據(jù)、添加數(shù)據(jù)。

2.在JDBC的基本應用中,介紹了使用Statement接口查詢和添加數(shù)據(jù)的步驟。重點在于使用getConnection()方法來連接數(shù)據(jù)庫,創(chuàng)建Statement對象,調用Connection對象的createStatement()方法創(chuàng)建這個MySQL語句對象。

3.針對接口查詢數(shù)據(jù),在這個對象調用executeQuery()方法來處理查詢的結果;針對接口添加數(shù)據(jù),在這個對象調用executeUpdate方法來處理。并通過一個具體的案例來幫助大家了解它的用法。

4.希望大家通過本文的學習,對你有所幫助!

最后需要拓展包的小伙伴,可以在公眾號后臺回復“拓展包”關鍵字進行獲取。

我是Java進階者,希望大家通過本文的學習,對你有所幫助!

 

責任編輯:姜華 來源: Java進階學習交流
相關推薦

2021-06-03 10:01:28

JDBCStatement接口

2021-06-22 10:24:22

Statement接口修改數(shù)據(jù)刪除數(shù)據(jù)

2009-07-06 17:36:06

ResultSetJDBC Connec

2021-05-13 07:58:05

JDBC接口PreparedSta

2010-06-18 15:33:19

UML接口

2017-08-22 16:40:22

前端JavaScript接口

2009-07-16 14:46:48

jdbc statem

2010-04-19 08:51:30

2010-07-08 10:28:51

UML接口

2021-06-28 10:25:47

MySQL語句接口

2021-06-28 10:00:32

JDBC數(shù)據(jù)庫MySQL

2009-08-25 14:18:13

C#如何連接數(shù)據(jù)庫

2021-05-14 06:15:48

SpringAware接口

2010-07-02 09:28:18

SQL Server

2010-01-26 09:50:30

C++接口

2009-11-09 16:57:05

WCF托管特性

2010-07-01 16:45:15

SQL Server

2010-07-21 16:20:45

SQL Server

2020-11-18 07:51:15

MySQL數(shù)據(jù)查詢

2010-09-25 16:53:39

SQL語句
點贊
收藏

51CTO技術棧公眾號