Drop Table from Oracle Database Using JDBC API



You can insert records into a table using the INSERT query.

Syntax

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Or,
INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN);

To insert a record into a table in a database using JDBC API you need to −

Register the Driver: Register the driver class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as parameter.

Establish a connection: Connect to the database using the getConnection() method of the DriverManager class. Passing URL (String), username (String), password (String) as parameters to it.

Create Statement: Create a Statement object using the createStatement() method of the Connection interface.

Execute the Query: Execute the query using the executeUpdate() method of the Statement interface.

Let us create a table with name dispatches in Oracle database using CREATE statement as shown below −

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

Following JDBC program establishes connection with the Oracle database and inserts 5 records in the Dispatches table −

Example

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
public class InsertData_Oracle {
   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......");
      //Inserting values to the table
      String query = "INSERT INTO dispatches VALUES (?, ?, ?, ?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "Key-Board");
      pstmt.setString(2, "Raja");
      pstmt.setDate(3, new Date(1567315800000L));
      pstmt.setTime(4, new Time(1567315800000L));
      pstmt.setInt(5, 7000);
      pstmt.setString(6, "Hyderabad");
      pstmt.execute();
      pstmt.setString(1, "Earphones");
      pstmt.setString(2, "Roja");
      pstmt.setDate(3, new Date(1556688600000L));
      pstmt.setTime(4, new Time(1556688600000L));
      pstmt.setInt(5, 2000);
      pstmt.setString(6, "Vishakhapatnam");
      pstmt.execute();
      pstmt.setString(1, "Mouse");
      pstmt.setString(2, "Puja");
      pstmt.setDate(3, new Date(1551418199000L));
      pstmt.setTime(4, new Time(1551418199000L));
      pstmt.setInt(5, 3000);
      pstmt.setString(6, "Vijayawada");
      pstmt.execute();
      pstmt.setString(1, "Mobile");
      pstmt.setString(2, "Vanaja");
      pstmt.setDate(3, new Date(1551415252000L));
      pstmt.setTime(4, new Time(1551415252000L));
      pstmt.setInt(5, 9000);
      pstmt.setString(6, "Chennai");
      pstmt.execute();
      pstmt.setString(1, "Headset");
      pstmt.setString(2, "Jalaja");
      pstmt.setDate(3, new Date(1554529139000L));
      pstmt.setTime(4, new Time(1554529139000L));
      pstmt.setInt(5, 6000);
      pstmt.setString(6, "Goa");
      pstmt.execute();
      System.out.println("Records inserted......");
   }
}

Output

Connection established......
Records inserted......

If you verify the contents of the dispatches table using select statement, you can find the inserted records in it as −

SQL> select * from dispatches;
PRODUCTNAME CUSTOMERNAME DISPATCHDATE DELIVERYTIME PRICE LOCATION
------------------------------------------------------------------------------------------
Key-Board Raja 01-SEP-19 01-SEP-19 11.00.00.000000 AM 7001 Hyderabad
Earphones Roja 01-MAY-19 01-MAY-19 11.00.00.000000 AM 2000 Vishakhapatnam
Mouse Puja 01-MAR-19 01-MAR-19 10.59.59.000000 AM 3000 Vijayawada
Mobile Vanaja 01-MAR-19 01-MAR-19 10.10.52.000000 AM 9001 Chennai
Headset Jalaja 06-APR-19 06-APR-19 11.08.59.000000 AM 6000 Goa
Updated on: 2019-07-30T22:30:26+05:30

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements