How to drop a table from Oracle database using JDBC API?

JDBCJava 8Object Oriented ProgrammingProgramming

<p>You can insert records into a table using the INSERT query.</p><h2>Syntax</h2><pre class="result notranslate">INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN); Or, INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN);</pre><p>To insert a record into a table in a database using JDBC API you need to &minus;</p><p><strong>Register the Driver:</strong> Register the driver class using the<strong>&nbsp;registerDriver()</strong> method of the <strong>DriverManager&nbsp;</strong>class. Pass the driver class name to it, as parameter.</p><p><strong>Establish a connection:</strong> Connect to the database using the <strong>getConnection()</strong> method of the <strong>DriverManager&nbsp;</strong>class. Passing URL (String), username (String), password (String) as parameters to it.</p><p><strong>Create Statement:</strong> Create a Statement object using the <strong>createStatement()</strong> method of the <strong>Connection&nbsp;</strong>interface.</p><p><strong>Execute the Query:</strong> Execute the query using the executeUpdate() method of the Statement interface.</p><p>Let us create a table with name <strong>dispatches&nbsp;</strong>in Oracle database using CREATE statement as shown below&nbsp;&minus;</p><pre class="result notranslate">CREATE TABLE dispatches( &nbsp; &nbsp;PRODUCTNAME VARCHAR2(20), &nbsp; &nbsp;CUSTOMERNAME VARCHAR2(20), &nbsp; &nbsp;DISPATCHDATE DATE, &nbsp; &nbsp;DELIVERYTIME TIMESTAMP(6), &nbsp; &nbsp;PRICE NUMBER(38), &nbsp; &nbsp;LOCATION VARCHAR2(20) );</pre><p>Following JDBC program establishes connection with the Oracle database and inserts 5 records in the Dispatches table &minus;</p><h2>Example</h2><pre class="prettyprint notranslate">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 { &nbsp; &nbsp;public static void main(String args[]) throws SQLException { &nbsp; &nbsp; &nbsp; //Registering the Driver &nbsp; &nbsp; &nbsp; DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); &nbsp; &nbsp; &nbsp; //Getting the connection &nbsp; &nbsp; &nbsp; String oracleUrl = &quot;jdbc:oracle:thin:@localhost:1521/xe&quot;; &nbsp; &nbsp; &nbsp; Connection con = DriverManager.getConnection(oracleUrl, &quot;system&quot;, &quot;password&quot;); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Connection established......&quot;); &nbsp; &nbsp; &nbsp; //Inserting values to the table &nbsp; &nbsp; &nbsp; String query = &quot;INSERT INTO dispatches VALUES (?, ?, ?, ?, ?, ?)&quot;; &nbsp; &nbsp; &nbsp; PreparedStatement pstmt = con.prepareStatement(query); &nbsp; &nbsp; &nbsp; pstmt.setString(1, &quot;Key-Board&quot;); &nbsp; &nbsp; &nbsp; pstmt.setString(2, &quot;Raja&quot;); &nbsp; &nbsp; &nbsp; pstmt.setDate(3, new Date(1567315800000L)); &nbsp; &nbsp; &nbsp; pstmt.setTime(4, new Time(1567315800000L)); &nbsp; &nbsp; &nbsp; pstmt.setInt(5, 7000); &nbsp; &nbsp; &nbsp; pstmt.setString(6, &quot;Hyderabad&quot;); &nbsp; &nbsp; &nbsp; pstmt.execute(); &nbsp; &nbsp; &nbsp; pstmt.setString(1, &quot;Earphones&quot;); &nbsp; &nbsp; &nbsp; pstmt.setString(2, &quot;Roja&quot;); &nbsp; &nbsp; &nbsp; pstmt.setDate(3, new Date(1556688600000L)); &nbsp; &nbsp; &nbsp; pstmt.setTime(4, new Time(1556688600000L)); &nbsp; &nbsp; &nbsp; pstmt.setInt(5, 2000); &nbsp; &nbsp; &nbsp; pstmt.setString(6, &quot;Vishakhapatnam&quot;); &nbsp; &nbsp; &nbsp; pstmt.execute(); &nbsp; &nbsp; &nbsp; pstmt.setString(1, &quot;Mouse&quot;); &nbsp; &nbsp; &nbsp; pstmt.setString(2, &quot;Puja&quot;); &nbsp; &nbsp; &nbsp; pstmt.setDate(3, new Date(1551418199000L)); &nbsp; &nbsp; &nbsp; pstmt.setTime(4, new Time(1551418199000L)); &nbsp; &nbsp; &nbsp; pstmt.setInt(5, 3000); &nbsp; &nbsp; &nbsp; pstmt.setString(6, &quot;Vijayawada&quot;); &nbsp; &nbsp; &nbsp; pstmt.execute(); &nbsp; &nbsp; &nbsp; pstmt.setString(1, &quot;Mobile&quot;); &nbsp; &nbsp; &nbsp; pstmt.setString(2, &quot;Vanaja&quot;); &nbsp; &nbsp; &nbsp; pstmt.setDate(3, new Date(1551415252000L)); &nbsp; &nbsp; &nbsp; pstmt.setTime(4, new Time(1551415252000L)); &nbsp; &nbsp; &nbsp; pstmt.setInt(5, 9000); &nbsp; &nbsp; &nbsp; pstmt.setString(6, &quot;Chennai&quot;); &nbsp; &nbsp; &nbsp; pstmt.execute(); &nbsp; &nbsp; &nbsp; pstmt.setString(1, &quot;Headset&quot;); &nbsp; &nbsp; &nbsp; pstmt.setString(2, &quot;Jalaja&quot;); &nbsp; &nbsp; &nbsp; pstmt.setDate(3, new Date(1554529139000L)); &nbsp; &nbsp; &nbsp; pstmt.setTime(4, new Time(1554529139000L)); &nbsp; &nbsp; &nbsp; pstmt.setInt(5, 6000); &nbsp; &nbsp; &nbsp; pstmt.setString(6, &quot;Goa&quot;); &nbsp; &nbsp; &nbsp; pstmt.execute(); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Records inserted......&quot;); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Connection established...... Records inserted......</pre><p>If you verify the contents of the dispatches table using select statement, you can find the inserted records in it as &minus;</p><pre class="result notranslate">SQL&gt; 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</pre>
raja
Updated on 30-Jul-2019 22:30:26

Advertisements