Using net bean write the following programs
Program 1:
Write a contest java program to establish connection with oracle by using net beans
import java.sql.*;
public class conntest {
public static void main(String[] args)throws Exception {
//create jdbc driver class object
sun.jdbc.odbc.JdbcOdbcDriver od= new sun.jdbc.odbc.JdbcOdbcDriver();
//register driver with drivermanager
DriverManager.registerDriver(od);
//establish connection with s/w
Connection con=DriverManager.getConnection("jdbc:odbc:oradsn","scott","tiger");
if(con==null)
System.out.println("connection is not establish");
else
System.out.println("connection is established");
}
}
Output:
Right Click and select run file (or) shift+F6 to run the program
run:
connection is established
BUILD SUCCESSFUL (total time: 0 seconds)
Refreshing SQL command
Check the student table in sql plus other wise creates it with Create, Insert commands and check it with select command you can see like this
Create command
Eg:
Create table student (sno number, sname varchar2 (20), m1 number, m2 number, m3 number);
Insert Command
Eg:
Insert into student values (101,’raja’, 20, 30, 49);
Select command
Eg:
Select * from student;
Program 2
Type the following code in NetBeans
import java.sql.*;
public class selecttest {
public static void main(String[] args) {
try{
//load driver class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//establish the connection with db s/w
Connection con=DriverManager.getConnection("Jdbc:Odbc:oradsn","scott","tiger");
//create statment object
Statement st=con.createStatement();
//send query to db s/w make query executed
ResultSet rs=st.executeQuery("select * from student");
//display the result
while(rs.next())
{
int no=rs.getInt("sno");
String name=rs.getString("sname");
System.out.println(no+""+name);
}
//close the connection
rs.close();
st.close();
con.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(ClassNotFoundException cnf)
{
cnf.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
run:
101raja
116rani
121rahul
131rajesh
BUILD SUCCESSFUL (total time: 0 seconds)
Program 3
Refer sql plus
Now we will delete 116 sno from student table
import java.sql.*;
import java.io.*;
public class Deletetest {
public static void main(String[] args) {
Connection con=null;
Statement st=null;
BufferedReader br=null;
try{
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter student number");
String s1=br.readLine().trim();
int no= Integer.parseInt(s1);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:oradsn","scott","tiger");
con.setAutoCommit(false);
st=con.createStatement();
String qry="delete from student where sno="+no;
st.execute(qry);
int result=st.getUpdateCount();
con.commit();
if(result!=0)
System.out.println(result+"number of record are deleted");
else
System.out.println("number records found to delete");
st.close();
con.close();
}catch(ClassNotFoundException cnf)
{
cnf.printStackTrace();
}
catch(SQLException se)
{
se.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
run:
enter student number
116
1number of record are deleted
BUILD SUCCESSFUL (total time: 10 seconds)
Check the sql plus ie.,
Program 4
Now we are try to insert the rani details into student table
The code is
import java.sql.*;
import java.io.*;
public class InsertTest {
public static void main(String[] args) {
Connection con=null;
Statement st=null;
BufferedReader br=null;
try{
br=new BufferedReader(new InputStreamReader(System.in));
//read input values from keyboard
int no=0;
int m1 = 0;
int m2 = 0;
int m3 = 0;
String name=null;
if(br!=null)
{
System.out.println("enter student number");
no=Integer.parseInt(br.readLine());
System.out.println("enter student name");
name=br.readLine();
System.out.println("enter mark1");
m1=Integer.parseInt(br.readLine());
System.out.println("enter mark2");
m2=Integer.parseInt(br.readLine());
System.out.println("enter mark3");
m3=Integer.parseInt(br.readLine());
}
//create jdbc connection object
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:oradsn","scott","tiger");
if(con!=null)
st=con.createStatement();
if(st!=null)
{
String qry="insert into student values("+no+",'"+name+"','"+m1+"','"+m2+"','"+m3+"')";
int res=st.executeUpdate(qry);
if(res==0)
System.out.println("record is not inserted");
else
System.out.println("record is inserted");
}
}catch(SQLException se)
{
se.printStackTrace();
}
catch(ClassNotFoundException cnf)
{
cnf.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(br!=null)
br.close();
}catch(IOException ioe)
{
ioe.printStackTrace();
}
try
{
if(st!=null)
st.close();
}catch(SQLException se)
{
se.printStackTrace();
}
try
{
if(con!=null)
con.close();
}catch(SQLException se)
{
se.printStackTrace();
}
}
}
}
run:
enter student number
141
enter student name
rani
enter mark1
98
enter mark2
87
enter mark3
66
record is inserted
BUILD SUCCESSFUL (total time: 27 seconds)