如何使用JDBC在PreparedStatement的IN子句中設定引數列表的值?


MySQL資料庫中的IN子句用於在查詢中指定引數列表。

例如,如果您需要使用特定的ID檢索表的內容,您可以使用SELECT語句以及IN子句,如下所示:

mysql> SELECT * from sales where ID IN (1001, 1003, 1005);
+------+-------------+--------------+--------------+--------------+-------+------------+
| ID   | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location   |
+------+-------------+--------------+--------------+--------------+-------+------------+
| 1001 | Key-Board   | Raja         | 2019-09-01   | 11:00:00     | 8500  | Hyderabad  |
| 1003 | Mouse       | Puja         | 2019-03-01   | 10:59:59     | 4500  | Vijayawada |
| 1005 | Headset     | Jalaja       | 2019-04-06   | 11:08:59     | 7500  | Goa        |
+------+-------------+--------------+--------------+--------------+-------+------------+
3 rows in set (0.03 sec)

當在預處理語句中使用IN子句時,您可以為引數列表使用繫結變數(每個引數一個),並使用PreparedStatement介面的setter方法稍後設定這些變數的值,並且在為語句中的所有繫結變數設定值後,您可以使用execute()方法執行該語句。

String query = "UPDATE sales SET price = price+1500 WHERE ProductName IN (?, ?, ? )";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Key-Board");
pstmt.setString(2, "Mouse");
pstmt.setString(3, "Headset");
pstmt.execute();

示例

讓我們在MySQL資料庫中建立一個名為sales的表,其中一列是自動遞增的,使用CREATE語句如下所示:

CREATE TABLE Sales(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   ProductName VARCHAR (20),
   CustomerName VARCHAR (20),
   DispatchDate date,
   DeliveryTime time,
   Price INT,
   Location VARCHAR(20)
);

現在,我們將使用INSERT語句在sales表中插入5條記錄:

insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'India');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) values('Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');

下面的JDBC程式建立與資料庫的連線,並使用IN子句將產品鍵盤、滑鼠和耳機的價格分別增加1500。

示例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class PreparedStatement_IN_clause {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///sample_database";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Inserting values to a table
      String query = "UPDATE sales SET price = price+1500 WHERE ProductName IN (?, ?, ? )";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "Key-Board");
      pstmt.setString(2, "Mouse");
      pstmt.setString(3, "Headset");
      pstmt.execute();
      System.out.println("Price values updated ......");
      System.out.println("Contents of the Sales table after the update: ");
      //Retrieving data
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from sales");
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rs.getString("CustomerName")+", ");
         System.out.print("Dispatch Date: "+rs.getDate("DispatchDate")+", ");
         System.out.print("Delivery Time: "+rs.getTime("DeliveryTime")+", ");
         System.out.print("Price: "+rs.getInt("Price")+", ");
         System.out.print("Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

輸出

Connection established......
Price values updated ......
Contents of the Sales table after the update:
Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 11:00:00, Price: 8500, Location: Hyderabad
Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 11:00:00, Price: 2000, Location: Vishakhapatnam
Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 10:59:59, Price: 4500, Location: Vijayawada
Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 10:10:52, Price: 9000, Location: Chennai
Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 11:08:59, Price: 7500, Location: Goa

更新於:2019年7月30日

4K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.