一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java的JDBC中Statement與CallableStatement對象實例

Java的JDBC中Statement與CallableStatement對象實例

2020-03-13 13:40goldensun JAVA教程

這篇文章主要介紹了Java的JDBC中Statement與CallableStatement對象實例,JDBC是Java編程中用于操作數據庫的API,需要的朋友可以參考下

JDBC Statement對象實例
以下是利用以下三種查詢以及打開和關閉說明的例子:

boolean execute(String SQL) : 返回一個布爾值true,如果ResultSet對象可以被檢索,否則返回false。使用這個方法來執行SQL DDL語句,或當需要使用真正的動態SQL。

int executeUpdate(String SQL) : 返回受影響的SQL語句執行的行數。使用此方法來執行,而希望得到一些受影響的行的SQL語句 - 例如,INSERT,UPDATE或DELETE語句。

ResultSet executeQuery(String SQL) : 返回ResultSet對象。當希望得到一個結果集使用此方法,就像使用一個SELECT語句。

基于對環境和數據庫安裝在前面的章節中做此示例代碼已被寫入。

復制下面的例子中JDBCExample.java,編譯并運行,如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//STEP 1. Import required packages
import java.sql.*;
 
public class JDBCExample {
  // JDBC driver name and database URL
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL = "jdbc:mysql://localhost/EMP";
 
  // Database credentials
  static final String USER = "username";
  static final String PASS = "password";
  
  public static void main(String[] args) {
  Connection conn = null;
  Statement stmt = null;
  try{
   //STEP 2: Register JDBC driver
   Class.forName("com.mysql.jdbc.Driver");
 
   //STEP 3: Open a connection
   System.out.println("Connecting to database...");
   conn = DriverManager.getConnection(DB_URL,USER,PASS);
 
   //STEP 4: Execute a query
   System.out.println("Creating statement...");
   stmt = conn.createStatement();
   String sql = "UPDATE Employees set age=30 WHERE id=103";
   
   // Let us check if it returns a true Result Set or not.
   Boolean ret = stmt.execute(sql);
   System.out.println("Return value is : " + ret.toString() );
 
   // Let us update age of the record with ID = 103;
   int rows = stmt.executeUpdate(sql);
   System.out.println("Rows impacted : " + rows );
 
   // Let us select all the records and display them.
   sql = "SELECT id, first, last, age FROM Employees";
   ResultSet rs = stmt.executeQuery(sql);
 
   //STEP 5: Extract data from result set
   while(rs.next()){
     //Retrieve by column name
     int id = rs.getInt("id");
     int age = rs.getInt("age");
     String first = rs.getString("first");
     String last = rs.getString("last");
 
     //Display values
     System.out.print("ID: " + id);
     System.out.print(", Age: " + age);
     System.out.print(", First: " + first);
     System.out.println(", Last: " + last);
   }
   //STEP 6: Clean-up environment
   rs.close();
   stmt.close();
   conn.close();
  }catch(SQLException se){
   //Handle errors for JDBC
   se.printStackTrace();
  }catch(Exception e){
   //Handle errors for Class.forName
   e.printStackTrace();
  }finally{
   //finally block used to close resources
   try{
     if(stmt!=null)
      stmt.close();
   }catch(SQLException se2){
   }// nothing we can do
   try{
     if(conn!=null)
      conn.close();
   }catch(SQLException se){
     se.printStackTrace();
   }//end finally try
  }//end try
  System.out.println("Goodbye!");
}//end main
}//end JDBCExample

現在編譯上面的例子如下:

?
1
C:>javac JDBCExample.java

當運行JDBCExample,它會產生以下結果:

?
1
C:>java JDBCExample
?
1
2
3
4
5
6
7
8
9
Connecting to database...
Creating statement...
Return value is : false
Rows impacted : 1
ID: 100, Age: 18, First: Zara, Last: Ali
ID: 101, Age: 25, First: Mahnaz, Last: Fatma
ID: 102, Age: 30, First: Zaid, Last: Khan
ID: 103, Age: 30, First: Sumit, Last: Mittal
Goodbye!


JDBC CallableStatement對象實例
下面是利用CallableStatement連同下列getEmpName()的MySQL存儲過程的例子:

請確定已經在EMP數據庫中創建該存儲過程。可以使用MySQL查詢瀏覽器來完成它。

?
1
2
3
4
5
6
7
8
9
10
11
12
DELIMITER $$
 
DROP PROCEDURE IF EXISTS `EMP`.`getEmpName` $$
CREATE PROCEDURE `EMP`.`getEmpName`
  (IN EMP_ID INT, OUT EMP_FIRST VARCHAR(255))
BEGIN
  SELECT first INTO EMP_FIRST
  FROM Employees
  WHERE ID = EMP_ID;
END $$
 
DELIMITER ;

基于對環境和數據庫安裝在前面的章節中進行,這個范例程式碼已被寫入。

復制下面的例子中JDBCExample.java,編譯并運行,如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//STEP 1. Import required packages
import java.sql.*;
 
public class JDBCExample {
  // JDBC driver name and database URL
  static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL = "jdbc:mysql://localhost/EMP";
 
  // Database credentials
  static final String USER = "username";
  static final String PASS = "password";
  
  public static void main(String[] args) {
  Connection conn = null;
  CallableStatement stmt = null;
  try{
   //STEP 2: Register JDBC driver
   Class.forName("com.mysql.jdbc.Driver");
 
   //STEP 3: Open a connection
   System.out.println("Connecting to database...");
   conn = DriverManager.getConnection(DB_URL,USER,PASS);
 
   //STEP 4: Execute a query
   System.out.println("Creating statement...");
   String sql = "{call getEmpName (?, ?)}";
   stmt = conn.prepareCall(sql);
   
   //Bind IN parameter first, then bind OUT parameter
   int empID = 102;
   stmt.setInt(1, empID); // This would set ID as 102
   // Because second parameter is OUT so register it
   stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
   
   //Use execute method to run stored procedure.
   System.out.println("Executing stored procedure..." );
   stmt.execute();
 
   //Retrieve employee name with getXXX method
   String empName = stmt.getString(2);
   System.out.println("Emp Name with ID:" +
        empID + " is " + empName);
   stmt.close();
   conn.close();
  }catch(SQLException se){
   //Handle errors for JDBC
   se.printStackTrace();
  }catch(Exception e){
   //Handle errors for Class.forName
   e.printStackTrace();
  }finally{
   //finally block used to close resources
   try{
     if(stmt!=null)
      stmt.close();
   }catch(SQLException se2){
   }// nothing we can do
   try{
     if(conn!=null)
      conn.close();
   }catch(SQLException se){
     se.printStackTrace();
   }//end finally try
  }//end try
  System.out.println("Goodbye!");
}//end main
}//end JDBCExample

現在編譯上面的例子如下:

?
1
C:>javac JDBCExample.java

當運行JDBCExample,它會產生以下結果:

?
1
C:>java JDBCExample
?
1
2
3
4
5
Connecting to database...
Creating statement...
Executing stored procedure...
Emp Name with ID:102 is Zaid
Goodbye!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日产免费自线一二区 | 掰开逼操| 高清视频在线播放 | 久久中文字幕综合不卡一二区 | 日本老妇乱子伦中文视频 | 91入口免费网站大全 | 99久久免费国内精品 | 四虎官网 | 久久中文电影 | 久久久精品国产免费A片胖妇女 | 精品国产中文字幕在线视频 | 免费日本在线视频 | 古装全套 毛片 | 免费一级日本c片完整版 | 日本精品一二三区 | gogo人体模特啪啪季玥图片 | 韩国www | 99视频都是精品热在线播放 | 免费高清视频免费观看 | 深夜免费在线观看 | 国产成人一区二区三区影院免费 | 欧美日韩一本 | 91东航翘臀女神在线播放 | 国产视频99 | 久久免费看少妇高潮A片2012 | 色老板在线 | 精品国产欧美一区二区五十路 | 国产自拍专区 | 欧美女人p| 冰雪奇缘1完整版免费观看 变形金刚第一部 | 99久久精品免费看国产情侣 | 国产亚洲精品线观看77 | 男女视频在线观看网站 | 美女的让男人桶爽30分钟的 | 国产国语videosex另类 | 久久精品一区二区三区资源网 | 欧美久久综合网 | 成年人免费在线看的惊悚动作片 | 国产成人精品曰本亚洲78 | 欧美成人aa久久狼窝动画 | 免费精品视频在线 |