JDBC中Statement接口實現(xiàn)查詢數(shù)據(jù)、添加數(shù)據(jù)
大家好,我是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ù):
代碼:
- 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 Example30 {
- public static void main(String[] args) {
- System.out.println("請輸入你要查詢的ID:");
- Scanner sc=new Scanner(System.in);
- String input=sc.next();
- String driver="com.mysql.jdbc.Driver";
- try {
- //加載驅動
- Class.forName(driver);
- //數(shù)據(jù)庫地址,本機、端口號3306、數(shù)據(jù)庫名為test
- String url="jdbc:mysql://localhost:3306/test";
- //用戶名
- String user="root";
- //密碼
- String pwd="168168";
- //連接數(shù)據(jù)庫
- Connection conn=DriverManager.getConnection(url,user,pwd);
- //創(chuàng)建Statement對象
- Statement stmt=conn.createStatement();
- String sql="select * from student where id='"+input+"'";
- //執(zhí)行SQL語句
- 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"));
- }else{
- System.out.println("你輸入的ID不存在!");
- }
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
效果圖如下所示:
在上面代碼中,首先是加載驅動程序,之后使用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ù)的用法,代碼如下所示:
- 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 Example31 {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- System.out.println("請輸入你要添加的ID:");
- String id=sc.next();
- System.out.println("請輸入你要添加的Name:");
- String name=sc.next();
- System.out.println("請輸入你要添加的Age:");
- int age=sc.nextInt();
- String driver="com.mysql.jdbc.Driver";
- try {
- //加載驅動
- Class.forName(driver);
- //數(shù)據(jù)庫地址,本機、端口號3306、數(shù)據(jù)庫名為test
- String url="jdbc:mysql://localhost:3306/test";
- //用戶名
- String user="root";
- //密碼
- String pwd="168168";
- //連接數(shù)據(jù)庫
- Connection conn=DriverManager.getConnection(url,user,pwd);
- //創(chuàng)建Statement對象
- Statement stmt=conn.createStatement();
- String sql="insert into student values('"+id+"','"+name+"',"+age+")";
- //執(zhí)行SQL語句
- stmt.executeUpdate(sql);
- sql="select * from student where id='"+id+"'";
- //執(zhí)行SQL語句
- 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 block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
效果圖如下所示:
表中的數(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進階者,希望大家通過本文的學習,對你有所幫助!