如何使用 JDBC API 來更新資料庫表中記錄的內容?
A. 可使用 UPDATE 查詢來更新/修改表中記錄的現有內容。使用此功能可更新表的所有記錄或特定記錄。
語法
UPDATE table_name SET column1 = value1, column2 = value2...., columnN = valueN WHERE [condition];
要使用 JDBC API 更新表中記錄的內容,需要
註冊驅動程式:使用 DriverManager 類的 registerDriver() 方法註冊驅動程式類。將其中的驅動程式類名作為引數傳遞。
建立連線:使用 DriverManager 類中的 getConnection() 方法連線資料庫。將其中的 URL(字串)、使用者名稱(字串)、密碼(字串)作為引數傳遞。
建立語句:使用 Connection 介面中的 createStatement() 方法建立一個 Statement 物件。
執行查詢:使用 Statement 介面中的 executeUpdate() 方法執行查詢。
示例
假設我們在 MySQL 中名為 mydatabase 的資料庫中有一張名為 customers 的表,其中有 12 條記錄:
+----+-----------+------+---------+----------------+ | ID | NAME | AGE | SALARY | ADDRESS | +----+-----------+------+---------+----------------+ | 1 | Amit | 25 | 3000.00 | Hyderabad | | 2 | Kalyan | 27 | 4000.00 | Vishakhapatnam | | 3 | Renuka | 30 | 5000.00 | Delhi | | 4 | Archana | 24 | 1500.00 | Mumbai | | 5 | Koushik | 30 | 9000.00 | Kota | | 6 | Hardik | 45 | 6400.00 | Bhopal | | 7 | Trupthi | 33 | 4360.00 | Ahmedabad | | 8 | Mithili | 26 | 4100.00 | Vijayawada | | 9 | Maneesh | 39 | 4000.00 | Hyderabad | | 10 | Rajaneesh | 30 | 6400.00 | Delhi | | 11 | Komal | 29 | 8000.00 | Ahmedabad | | 12 | Manyata | 25 | 5000.00 | Vijayawada | +----+-----------+------+---------+----------------+
以下 JDBC 程式建立與 MySQL 的連線,並將 id 為 4、5、6 和 7 的客戶地址更新為德里,並在更新後檢索並顯示錶的內容。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UpdateRecordsExample {
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:///MyDatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating the Statement
Statement stmt = con.createStatement();
//Query to update records
String query = "update customers set address = 'Delhi' where ID in (4, 5, 6, 7 )";
//Executing the query
int i = stmt.executeUpdate(query);
System.out.println("Rows updated: "+i);
System.out.println("Contents of the customers table after updating the records: ");
//Retrieving data
ResultSet rs = stmt.executeQuery("Select * from customers");
while(rs.next()) {
System.out.print("ID: "+rs.getInt("ID")+", ");
System.out.print("Name: "+rs.getString("Name")+", ");
System.out.print("Age: "+rs.getInt("Age")+", ");
System.out.print("Salary: "+rs.getInt("Salary")+", ");
System.out.print("Address: "+rs.getString("Address"));
System.out.println();
}
}
}輸出
Connection established...... Rows updated: 4 Contents of the customers table after updating the records: ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Delhi ID: 5, Name: Koushik, Age: 30, Salary: 9000, Address: Delhi ID: 6, Name: Hardik, Age: 45, Salary: 6400, Address: Delhi ID: 7, Name: Trupthi, Age: 33, Salary: 4360, Address: Delhi ID: 8, Name: Mithili, Age: 26, Salary: 4100, Address: Vijayawada ID: 9, Name: Maneesh, Age: 39, Salary: 4000, Address: Hyderabad ID: 10, Name: Rajaneesh, Age: 30, Salary: 6400, Address: Delhi ID: 11, Name: Komal, Age: 8000, Salary: 29, Address: Ahmedabad ID: 12, Name: Manyata, Age: 5000, Salary: 25, Address: Vijayawada
廣告
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP