如何使用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://127.0.0.1/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+ 次浏览

启动您的职业生涯

通过完成课程获得认证

开始
广告