0% found this document useful (0 votes)
30 views

JDBC Program Creation

This document discusses how to connect to an Oracle database and execute queries using JDBC. It first explains how to set the classpath and load the Oracle JDBC driver. It then shows an example oracleDBConnection program that establishes a connection, creates a statement, executes a sample query, and prints the results. Finally, it demonstrates using a prepared statement to insert rows into a table and then retrieve and print the results.

Uploaded by

Harsha Vardhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

JDBC Program Creation

This document discusses how to connect to an Oracle database and execute queries using JDBC. It first explains how to set the classpath and load the Oracle JDBC driver. It then shows an example oracleDBConnection program that establishes a connection, creates a statement, executes a sample query, and prints the results. Finally, it demonstrates using a prepared statement to insert rows into a table and then retrieve and print the results.

Uploaded by

Harsha Vardhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

JDBC program development

using type 4 thin oracle driver


Loading the driver files by Setting the
Environment Variables
Add the paths of ojdbc14.jar and
ojdbc14_g.jar files of oracleXE
Add the jdk’s lib path also.
C:\oraclexe\app\oracle\product\10.2.0\ser
ver\jdbc\lib\ojdbc14.jar;.;C:\oraclexe\ap
p\oracle\product\10.2.0\server\jdbc\lib\
ojdbc14_g.jar;.;C:\Program
Files\Java\jdk1.7.0_07\lib;.;
Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 2
oracleDBConnection.java program

Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 3


oracleDBConnection.java program
import java.sql.*;
import java.io.*;

1. public class oracleDBConnection{


2. public static void main(String args[]) throws Exception {
3. try {
4. Class.forName("oracle.jdbc.driver.OracleDriver"); //step1 register driver class
5. Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system
","123"); //step2 create the connection object
6. Statement stmt=con.createStatement(); //step3 create the statement object
7. ResultSet rs=stmt.executeQuery("select * from emp"); //step4 execute query
8. while(rs.next()) //step 5: process the results
9. System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
10. con.close(); //step5 close the connection object
11. }catch(Exception e){ System.out.println(e);}
12. }
13. } Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 4
Compile and Execute

Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 5


Using Prepared Statement to insert the rows in the existing table
import java.io.*;
import java.sql.*;

public class usingPreparedStatement {


public static void main(String[] args) throws SQLException {
try {
System.out.println("Hello World! Using PreparedStatement interface...,
this is used when parameterized sql queries are used like insert, retrieve on
conditions,delete,..");

Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con=
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","
123");

Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 6


PreparedStatement ps=con.prepareStatement("insert into emp values(?,?,?,?)");
int empno,deptno;
double salary;
String ename;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));


empno=Integer.parseInt(br.readLine());
ename=br.readLine();
deptno=Integer.parseInt(br.readLine());
salary=Double.parseDouble(br.readLine());

ps.setInt(1,empno);
ps.setString(2,ename);
ps.setInt(3,deptno);
ps.setDouble(4,salary);
int inserted=ps.executeUpdate();

System.out.println(inserted+" records inserted.");

Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 7


ps.close();

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
ResultSetMetaData rsmd=rs.getMetaData();

while (rs.next())
{
System.out.println();
for(int i=1;i<=rsmd.getColumnCount();i++)
System.out.print(" "+rs.getString(i));
}
con.close();
st.close();
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}

} Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 8


Using Prepared Statement to insert the rows in the existing table

Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 9


Outputs

Results shown in the browser 

Dr. K. Shirisha, Dept. of CSE, SNIST, Hyd 10

You might also like