如何使用JDBC API从Oracle数据库中现有表中删除记录?


您可以使用DELETE查询从数据库中的表中删除特定记录。

语法

DELETE FROM table_name
WHERE [condition];

要使用JDBC API从表中删除记录,您需要:

注册驱动程序: 使用**DriverManager**类的**registerDriver()**方法注册驱动程序类。将驱动程序类名作为参数传递。

建立连接: 使用**DriverManager**类的**getConnection()**方法连接到数据库。将URL(字符串)、用户名(字符串)、密码(字符串)作为参数传递。

创建语句: 使用**Connection**接口的**createStatement()**方法创建一个Statement对象。

执行查询: 使用Statement接口的executeUpdate()方法执行查询。

让我们使用CREATE语句在Oracle数据库中创建一个名为**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数据库的连接,并从Dispatches表中删除名为Jalaja的客户的记录。

示例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DeleteRecordsOracle {
   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 delete records
      String query = "Delete from dispatches where CUSTOMERNAME = 'Jalaja'";
      int i = stmt.executeUpdate(query);
      System.out.println("Rows deleted: "+i);
      //Retrieving data
      ResultSet rs = stmt.executeQuery("Select * from dispatches");
      System.out.println("Contents of the table after deleting the records: ");
      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 deleted: 1
Contents of the table after deleting 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

更新于:2019年7月30日

497 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告