jdbc program .dot
jdbc program .dot
Syntax
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);
Example
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTable_Oracle
{
public static void main(String args[]) throws SQLException
{
//Registering the Driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connection established......");
Output
Connection established......
Table Created......
In oracle you can get the list of tables using the
query/command −
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.
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 −
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;
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
System.out.println("Connection established......");
pstmt.setString(1, "Key-Board");
pstmt.setString(2, "Raja");
pstmt.setInt(5, 7000);
pstmt.setString(6, "Hyderabad");
pstmt.execute();
pstmt.setString(1, "Earphones");
pstmt.setString(2, "Roja");
pstmt.setInt(5, 2000);
pstmt.setString(6, "Vishakhapatnam");
pstmt.execute();
pstmt.setString(1, "Mouse");
pstmt.setString(2, "Puja");
pstmt.setInt(5, 3000);
pstmt.setString(6, "Vijayawada");
pstmt.execute();
pstmt.setString(1, "Mobile");
pstmt.setString(2, "Vanaja");
pstmt.setInt(5, 9000);
pstmt.setString(6, "Chennai");
pstmt.execute();
pstmt.setString(1, "Headset");
pstmt.setString(2, "Jalaja");
pstmt.setInt(5, 6000);
pstmt.setString(6, "Goa");
pstmt.execute();
System.out.println("Records inserted......");
Output
Connection established......
Records inserted......
1 row created.
1 row created.
1 row created.
1 row created.
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Readdata
{
public static void main(String args[]) throws SQLException
{
int cnt=0;
//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",
"system");
System.out.println("Connection established......");
Update Query
To update the contents of a record in a table 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.
Execute the Query: Execute the query using the executeUpdate() method of the Statement
interface.
Example
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();
}
}
}
Output
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:5