0% found this document useful (0 votes)
4 views2 pages

JDBC

The document is a Java program that connects to an Oracle database and creates a table named 'emp92' with columns for id, name, and age. It inserts a record into the table and retrieves all records to display them. The program handles exceptions and prints error messages if any occur during execution.

Uploaded by

udayx4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

JDBC

The document is a Java program that connects to an Oracle database and creates a table named 'emp92' with columns for id, name, and age. It inserts a record into the table and retrieves all records to display them. The program handles exceptions and prints error messages if any occur during execution.

Uploaded by

udayx4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.sql.

Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class OracleDB {


public static void main(String[] args)
{
try {

Class.forName("oracle.jdbc.driver.OracleD
river");
String url
="jdbc:oracle:thin:@localhost:1521/xe";
String username = "System";
String password ="root";
Connection connection =
DriverManager.getConnection(url,username,
password);
Statement statement =
connection.createStatement();
String createTableQuery =
"CREATE TABLE emp92(" +
"id NUMBER(10)," +
"name VARCHAR2(40)," +
"age NUMBER(3)),";

statement.executeUpdate(createTableQuery)
;
System.out.println("Table
'emp92' created successfully!");
int result =
statement.executeUpdate(
"insert into
emp92(id,name,age)
values('1','rachel','45')");
String selectQuery = "Select *
FROM emp92";
ResultSet resultSet =
statement.executeQuery(selectQuery);
while(resultSet.next()) {
int id =
resultSet.getInt(1);
String name =
resultSet.getString(2);
String age =
resultSet.getString(3);
System.out.println(id +" "+
name+ " "+ age);
}
connection.close();
} catch(Exception e) {
System.out.println("Erorr: " +
e.getMessage());
}
}
}

You might also like