如何使用JDBC API從Oracle資料庫中現有表中檢索記錄?


您可以使用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()方法執行查詢。

讓我們使用CREATE語句建立一個名為**dispatches**的表,如下所示:

CREATE TABLE Dispatches(
   PRODUCTNAME VARCHAR2(20),
   CUSTOMERNAME VARCHAR2(20),
   DISPATCHDATE DATE,
   DELIVERYTIME TIMESTAMP(6),
   PRICE NUMBER(38),
   LOCATION VARCHAR2(20)
);

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

insert into dispatches values('Key-Board', 'Raja', TO_DATE('2019-09-01', 'yyyy/mm/dd'), TO_DATE('11:00:00', 'hh:mi:ss'), 7000, 'India');
insert into dispatches values('Earphones', 'Roja', TO_DATE('2019-05-01', 'yyyy/mm/dd'), TO_DATE('11:00:00', 'hh:mi:ss'), 2000, 'Vishakhapatnam');
insert into dispatches values('Mouse', 'Puja', TO_DATE('2019-03-01', 'yyyy/mm/dd'), TO_DATE('10:59:59', 'hh:mi:ss'), 3000, 'Vijayawada');
insert into dispatches values('Mobile', 'Vanaja', TO_DATE('2019-03-01', 'yyyy/mm/dd'), TO_DATE('10:10:52', 'hh:mi:ss'), 9000, 'Chennai');
insert into dispatches values('Headset', 'Jalaja', TO_DATE('2019-04-06', 'yyyy/mm/dd'), TO_DATE('11:08:59', 'hh:mi:ss' ), 6000, 'Goa');

下面的JDBC程式與Oracle資料庫建立連線,並將每個產品的價格增加3000。

示例

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 oracle.jdbc.driver.OracleDriver());
      //Getting the connection
      String oracleUrl = "jdbc:oracle:thin:@localhost:1521/xe";
      Connection con = DriverManager.getConnection(oracleUrl, "system", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to update records, Increasing the price of all items by 3000
      String query = "Update dispatches set PRICE = PRICE+3000";
      //Executing the query
      int i = stmt.executeUpdate(query);
      System.out.println("Rows updated: "+i);
      System.out.println("Contents of the dispatches table after updating the records: ");
      //Retrieving data
      ResultSet rs = stmt.executeQuery("Select * from dispatches");
      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......
Rows updated: 5
Contents of the dispatches table after updating the records:
Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 11:00:00, Price: 10001, Location: Hyderabad
Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 11:00:00, Price: 5000, Location: Vishakhapatnam
Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 10:59:59, Price: 6000, Location: Vijayawada
Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 10:10:52, Price: 12001, Location: Chennai
Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 11:08:59, Price: 9000, Location: Goa

更新於:2019年7月30日

1K+ 瀏覽量

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告