在之前的 JDBC 教程中,我们开发了一个Java 程序来使用 PreparedStatement 对象将记录插入到表中。在这篇文章中,我们将开发一个 Java 程序来使用 PreparedStatement 对象更新记录。
在本教程中,我们使用的是 Oracle 数据库,您可以使用任何具有所需信息的数据库。在我们的 Oracle 数据库中,我们有一个“产品”表。
SQL> desc product;
Name Null? Type
---------- -------- -------------
PID NOT NULL NUMBER(10)
PNAME VARCHAR2(15)
PRICE FLOAT(126)
QUANTITY NUMBER(10)
在产品表中,我们有一些记录,
SQL> select * from product;
PID PNAME PRICE QUANTITY
---- -------- ------ ------
102 Chair 500 5
2051 Fan 999 2
111 Table 1500 1
程序描述:-开发一个使用 PreparedStatement 对象更新产品表记录的 JDBC 程序。
要更新一行的记录,我们需要至少一个列值来选择该行,然后更新所需的值。必需的 SQL 查询,
SQL> update product set pname='Book',
price = 20, quantity = 5
where pid = 111;
没有输入值进入数据库软件的SQL查询,无论是否执行,在数据库软件中都变成解析或编译的SQL查询,称为预编译SQL查询。Java 应用程序的 PreparedStatement 对象表示数据库软件的这种预编译 SQL 查询。我们可以使用该对象为 SQL 查询设置输入值,执行 SQL 查询并获取 SQL 查询的输出一次或多次。PreparedStatement 对象非常适合使用输入值或输出值多次执行相同的 SQL 查询。
使用 PreparedStatement 对象更新记录的 Java 程序
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class SelectTest {
// SQL query
private static final String UPDATE_PRODUCT_QUERY =
"UPDATE PRODUCT SET PNAME = ?, PRICE = ?, QUANTITY = ?"
+ "WHERE PID = ? ";
public static void main(String[] args ) {
// declare variables
Scanner scan = null;
int pid = 0, qty = 0;
String name = null;
float price = 0.0f;
Connection con = null;
PreparedStatement ps = null;
int result = 0;
try {
// read input
scan = new Scanner(System.in);
if(scan != null) {
System.out.println("Enter the existing product ID"
+ " to update: ");
pid = scan.nextInt();
System.out.println("Enter new details,");
System.out.print("Name of the product: ");
name = scan.next();
System.out.print("Price: ");
price = scan.nextFloat();
System.out.print("Quantity: ");
qty = scan.nextInt();
}
// establish the connection
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:knowprogram",
"scott", "tiger");
// compile SQL query and
// store it in PreparedStatemet object
if(con != null)
ps = con.prepareStatement(UPDATE_PRODUCT_QUERY);
// set input values and execute query
if(ps != null) {
// set input values to query parameters
ps.setString(1, name);
ps.setFloat(2, price);
ps.setInt(3, qty);
ps.setInt(4, pid);
// execute the query
result = ps.executeUpdate();
}
// process the result
if(result == 0)
System.out.println("Records not updated");
else
System.out.println("Records updated"+
" successfully");
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} // end of try-catch block
finally {
// close JDBC objects
try {
if(ps != null) ps.close();
} catch(SQLException se) {
se.printStackTrace();
}
try {
if(con != null) con.close();
} catch(SQLException se) {
se.printStackTrace();
}
try {
if(scan != null) scan.close();
} catch(Exception e) {
e.printStackTrace();
}
}
} //end of main
} //end of class
输出:-
Enter the existing product ID to update:
102
Enter new details,
Name of the product: Bed
Price: 2000
Quantity: 1
Records updated successfully
Enter the existing product ID to update:
1000
Enter new details,
Name of the product: Book
Price: 50
Quantity: 2
Records not updated
产品 id 1000 在产品表中不可用,因此不会更新记录。
现在,让我们验证记录是否已更新。执行 SQL 查询以显示表的详细信息,
SQL> select * from product;
PID PNAME PRICE QUANTITY
---- ------ ------ --------
102 Bed 2000 1
2051 Fan 999 2
111 Table 1500 1
默认情况下,JDBC 程序以自动提交模式执行。因此,无需显式提交记录。