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)
在prepared语句中使用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();
让我们使用CREATE语句在MySQL数据库中创建一个名称为sales的表,其中一列自动递增,如下所示-
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子句将产品的键盘,鼠标和Headset的价格分别提高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 {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获得连接
String mysqlUrl = "jdbc:mysql://localhost/sample_database";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("建立连接...-");
//将值插入表
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("价格值已更新......-");
System.out.println("更新后Sales表的内容: ");
//检索数据
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from sales");
while(rs.next()) {
System.out.print("Name: "+rs.getString("ProductName")+", ");
System.out.print("客户名称: "+rs.getString("CustomerName")+", ");
System.out.print("发货日期: "+rs.getDate("DispatchDate")+", ");
System.out.print("交货时间: "+rs.getTime("DeliveryTime")+", ");
System.out.print("Price: "+rs.getInt("Price")+", ");
System.out.print("Location: "+rs.getString("Location"));
System.out.println();
}
}
}输出结果
建立连接...- 价格值已更新......- 更新后Sales表的内容: Name: Key-Board, 客户名称: Raja, 发货日期: 2019-09-01, 交货时间: 11:00:00, Price: 8500, Location: Hyderabad Name: Earphones, 客户名称: Roja, 发货日期: 2019-05-01, 交货时间: 11:00:00, Price: 2000, Location: Vishakhapatnam Name: Mouse, 客户名称: Puja, 发货日期: 2019-03-01, 交货时间: 10:59:59, Price: 4500, Location: Vijayawada Name: Mobile, 客户名称: Vanaja, 发货日期: 2019-03-01, 交货时间: 10:10:52, Price: 9000, Location: Chennai Name: Headset, 客户名称: Jalaja, 发货日期: 2019-04-06, 交货时间: 11:08:59, Price: 7500, Location: Goa