Sie sind auf Seite 1von 34

Aim: Write a JDBC Application to create a table

Source Code:
import java.sql.*;
import java.lang.String.*;
class First
{
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
Statement st=con.createStatement();
int x=st.executeUpdate("create table sammy23(empno number,ename varchar2(10),esal number,dept
varchar2(10))");
if(x<0)
System.out.println("table is created"+x);
else
System.out.println("error"+x);
}
}
Output:
Aim: Write a JDBC Application to insert five rows in a table
Source Code:
import java.sql.*;
import java.lang.String.*;
class Second
{
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
Statement st=con.createStatement();
int x=st.executeUpdate("insert into sammy23 values(101,'sai',10000,'Cse')");
st.executeUpdate("insert into sammy23 values(102,'sandeep',90000,'Cse')");
st.executeUpdate("insert into sammy23 values(103,'sindhu',70000,'Ece')");
st.executeUpdate("insert into sammy23 values(104,'snigdha',50000,'Civil')");
st.executeUpdate("insert into sammy23 values(105,'kiran',70000,'Eee')");
if(x>0)
System.out.println("a row is inserted"+x);
else
System.out.println("error"+x);
}
}
Output:
Aim: Write a JDBC Application to Update the salary whose salary is less than 7000 with bonus amount of
20% on basic
Source Code:
import java.sql.*;
import java.lang.String.*;
class Tabup
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
Statement st=con.createStatement();
int x=st.executeUpdate("update sammy23 set esal=(esal+(esal*0.2)) where esal<70000");
if(x>0)
System.out.println("Value has been updated"+x);
else
System.out.println("error"+x);
}
}
Output:
Aim: Write a JDBC Application to delete all rows in a table
Source Code:
import java.sql.*;
import java.lang.String.*;
class TabDel
{
public static void main(String[] args) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
Statement st=con.createStatement();
int x=st.executeUpdate("delete sammy23");
if(x>0)
System.out.println("table has been deleted"+x);
else
System.out.println("error"+x);
}
}
Output:
Aim: Write a JDBC Application to selecting all the rows in a table
Source Code:
import java.sql.*;
import java.lang.String.*;
import java.io.*;
class TabSel
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from sammy23");
while(rs.next())
{
System.out.print(rs.getInt(1)+" ");
System.out.print(rs.getString(2)+" ");
System.out.print(rs.getInt(3)+" ");
System.out.println(rs.getString(4));
}
}
}
Output:
Aim: Write a JDBC Application to insert values into a table using values read from keyboard
Source Code:
import java.sql.*;
import java.lang.String.*;
import java.io.*;
class Ins
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter the Empno,Ename,Esal and Dept");
int eno=Integer.parseInt(br.readLine());
String ename=br.readLine();
int esal=Integer.parseInt(br.readLine());
String dept=br.readLine();
Statement st=con.createStatement();
int x=st.executeUpdate("insert into sammy23 values("+eno+",'"+ename+"',"+esal+",'"+dept+"')");

if(x>0)
System.out.println("table has been updated"+x);
else
System.out.println("error"+x);
}
}
Output:

Aim: Write a JDBC Application to update rows by reading the input


Source Code:
import java.sql.*;
import java.lang.String.*;
import java.io.*;
class Upd
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter the Esal value to update");
int sal=Integer.parseInt(br.readLine());
Statement st=con.createStatement();
int x=st.executeUpdate("update sammy23 set esal="+sal+"where ename='sai'");
if(x>0)
System.out.println("table has been updated"+x);
else
System.out.println("error"+x);
}
}

Output:
Aim: Write a JDBC Application to delete a table by reading input from the keyboard
Source Code:
import java.sql.*;
import java.lang.String.*;
import java.io.*;
class Del
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter the tabelname to delete all rows");
String tablename=br.readLine();
Statement st=con.createStatement();
System.out.println(tablename);
int x=st.executeUpdate("delete "+tablename+"");
if(x>0)
System.out.println("table has been deleted"+x);
else
System.out.println("error"+x);
}
}

Output:
Aim: Write a JDBC Application to insert values into a table
Source Code:
import java.io.*;
import java.sql.*;
class Test
{
public static void main(String ar[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
Statement st=con.createStatement();
long l1=System.currentTimeMillis();
System.out.println(l1);
for(int i=0;i<100;i++)
{
st.executeUpdate("insert into saile values("+i+","+i+")");
}
long l2=System.currentTimeMillis();
System.out.println(l2);
System.out.println(l2-l1);
}
}
Output:
Aim: Write a JDBC Application to illustrate about Prepared Statement
Source Code:
import java.io.*;
import java.sql.*;
class Test1
{
public static void main(String ar[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
PreparedStatement st=con.prepareStatement("insert into saile values(?,?)");
long l1=System.currentTimeMillis();
for(int i=0;i<100;i++)
{
st.setInt(1,i);
st.setInt(2,i);
st.executeUpdate();
}
long l2=System.currentTimeMillis();
System.out.println(l2-l1);
}
}
Output:
Aim: Write a JDBC Application to insert values into a table using Prepared Statement
Source Code:
import java.sql.*;
import java.lang.String.*;
import java.io.*;
class Insp
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
PreparedStatement ps=con.prepareStatement("insert into sammy23 values(?,?,?,?)");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
while(true)
{
System.out.println("Enter the empno,ensame,esal,dept");
int eno=Integer.parseInt(br.readLine());
String ename=br.readLine();
int esal=Integer.parseInt(br.readLine());
String dept=br.readLine();
ps.setInt(1,eno);
ps.setString(2,ename);
ps.setInt(3,esal);
ps.setString(4,dept);
int x=ps.executeUpdate();
if(x>0)
System.out.println("a row has been inserted"+x);
else
System.out.println("error"+x);
System.out.println("Do U want to insert another record ? YES/NO");
String ch=br.readLine();
if(!ch.equalsIgnoreCase("YES"))
break;
}
con.close();
}
}
Output:
Aim: Write a JDBC Application to update department name of an employee using Prepared Statement
Source Code:
import java.sql.*;
import java.lang.String.*;
import java.io.*;
class Updp
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
PreparedStatement ps=con.prepareStatement("update sammy23 set dept=? where empno=101");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
System.out.println("Enter the dept name to be updated for the eno=101");
String dept=br.readLine();
ps.setString(1,dept);
int x=ps.executeUpdate();
if(x>0)
System.out.println("a row has been updated"+x);
else
System.out.println("error"+x);

con.close();
}
}
Output:
Aim: Write a JDBC Application to delete a row where salary greater than 13000 using Prepared Statement
Source Code:
import java.sql.*;
import java.lang.String.*;
import java.io.*;
class Delp
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
PreparedStatement ps=con.prepareStatement("delete sammy23 where esal>13000");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
int x=ps.executeUpdate();
if(x>0)
System.out.println("a row has been deleted"+x);
else
System.out.println("error"+x);
con.close();
}
}
Output:
Aim: Write a Stored Procedure to insert values into a table and write a JDBC application to call the stored
procedure
Source Code:
import java.sql.*;
import java.io.*;
class Proce
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
CallableStatement cs=con.prepareCall("{call pro_inserti()}");
cs.execute();
con.close();
}
}

Stored Procedure:
Create procedure pro_inserti
as
begin insert into sammy23 values(102,’sandeep’,12564,’ece’)
end pro_inserti;
/
Output:
Aim: Write a Stored Procedure to delete a row in a table and write a JDBC application to call the stored
procedure
Source Code:
import java.sql.*;
import java.io.*;
class Pdel
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
CallableStatement cs=con.prepareCall("{call proced_dele()}");
cs.execute();
con.close();
}
}

Stored Procedure:
Create procedure proced_dele
as begin
delete sammy23 where empno=102;
end proced_dele;
/
Output:

Das könnte Ihnen auch gefallen