Anda di halaman 1dari 34

c : Write a JDBC Application to create a table

 

importjava.sql.*;
importjava.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(empnonumber,ename varchar2(10),esalnumber,dept
varchar2(10))");
if(x<0)
System.out.println("table is created"+x);
else
System.out.println("error"+x);
}
}






Ä 

c 
Write a JDBC Application to insert five rows in a table
  

importjava.sql.*;
importjava.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);
}
}


Ä 

c 
Write a JDBC Application to Update the salary whose salary is less than 7000 with bonus amount of
20% on basic
  

importjava.sql.*;
importjava.lang.String.*;
classTabup
{
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);
}
}
Ä 

c 
Write a JDBC Application to delete all rows in a table
  

importjava.sql.*;
importjava.lang.String.*;
classTabDel
{
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);
}
}
Ä 

c 
Write a JDBC Application to selecting all the rows in a table
  

importjava.sql.*;
importjava.lang.String.*;
import java.io.*;
classTabSel
{
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();
ResultSetrs=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));
}
}
}
Ä 


c 
Write a JDBC Application to insert values into a table using values read from keyboard
  

importjava.sql.*;
importjava.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);
BufferedReaderbr=new BufferedReader(is);
System.out.println("Enter the Empno,Ename,Esal and Dept");
inteno=Integer.parseInt(br.readLine());
String ename=br.readLine();
intesal=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);
}
}
Ä 

c 
Write a JDBC Application to update rows by reading the input
  

importjava.sql.*;
importjava.lang.String.*;
import java.io.*;
classUpd
{
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);
BufferedReaderbr=new BufferedReader(is);
System.out.println("Enter the Esal value to update");
intsal=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);
}
}
Ä 


c 
Write a JDBC Application to delete a table by reading input from the keyboard
  

importjava.sql.*;
importjava.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);
BufferedReaderbr=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);
}
}
Ä 



c 
Write a JDBC Application to insert values into a table
  

import java.io.*;
importjava.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(inti=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);
}
}






























Ä 



c 
Write a JDBC Application to illustrate about Prepared Statement
  

import java.io.*;
importjava.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");
PreparedStatementst=con.prepareStatement("insert into saile values(?,?)");
long l1=System.currentTimeMillis();
for(inti=0;i<100;i++)
{
st.setInt(1,i);
st.setInt(2,i);
st.executeUpdate();
}
long l2=System.currentTimeMillis();
System.out.println(l2-l1);
}
}
Ä 




c 
Write a JDBC Application to insert values into a table using Prepared Statement
  

importjava.sql.*;
importjava.lang.String.*;
import java.io.*;
classInsp
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
PreparedStatementps=con.prepareStatement("insert into sammy23 values(?,?,?,?)");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReaderbr=new BufferedReader(is);
while(true)
{
System.out.println("Enter the empno,ensame,esal,dept");
inteno=Integer.parseInt(br.readLine());
String ename=br.readLine();
intesal=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();
}
}
Ä 


c 
Write a JDBC Application to update department name of an employee using Prepared Statement
  

importjava.sql.*;
importjava.lang.String.*;
import java.io.*;
classUpdp
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
PreparedStatementps=con.prepareStatement("update sammy23 set dept=? whereempno=101");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReaderbr=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();
}
}
Ä 

c 
Write a JDBC Application to delete a row where salary greater than 13000 using Prepared Statement
  

importjava.sql.*;
importjava.lang.String.*;
import java.io.*;
classDelp
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
PreparedStatementps=con.prepareStatement("delete sammy23 where esal>13000");
InputStreamReader is=new InputStreamReader(System.in);
BufferedReaderbr=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();
}
}
Ä 

c 
Write a Stored Procedure to insert values into a table and write a JDBC application to call the stored
procedure
  

importjava.sql.*;
import java.io.*;
classProce
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
CallableStatementcs=con.prepareCall("{call pro_inserti()}");
cs.execute();
con.close();
}
}

   



Create procedure pro_inserti
as
begin insert into sammy23 values(102,¶sandeep¶,12564,¶ece¶)
endpro_inserti;
/

Ä 


c 
Write a Stored Procedure to delete a row in a table and write a JDBC application to call the stored
procedure
  

importjava.sql.*;
import java.io.*;
classPdel
{
public static void main(String args[]) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:07589","scott","tiger");
CallableStatementcs=con.prepareCall("{call proced_dele()}");
cs.execute();
con.close();
}
}

   



Create procedure proced_dele
as begin
delete sammy23 where empno=102;
endproced_dele;
/

Ä