class mydatabse
{
Connection cn;
ResultSet rs;
Statement st;
PreparedStatement pst;
mydatabse() //constructor
{
try{
class.forName("oracle.jdbc.driver.OracleDriver");
cn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","<password>");
System.out.println("Connection established");
}catch(Exception e) { }
}//const end
void addrecord()
{
System.out.print("welcome to insert module:");
try {
pst=cn.prepareStatement("insert into emp values (?,?)");
Scanner sc= new Scanner (System.in);
System.out.println("Enter emp no");
int eno=sc.nextInt();
System.out.println("Enter emp name:");
String name=sc.next();
pst.setInt(1,eno);
pst.set(2,name);
int x=pst.executeUpdtae();
if(x>0)
System.out.println("inserted");
} catch(Exception e)
{
System.out.println("Already available");
}
}
void deleterecord()
{
System.out.print("welcome to delete module:");
try {
pst=cn.prepareStatement("delete from emp where ename=?");
System.out.println("Enter emp name:");
String name=sc.next();
pst.setString(1,name);
int x=pst.executeUpdtae();
if(x>0)
System.out.println("deleted");
} catch(Exception e)
{
e.printStsackTrace();
}
}
public static void main (String s[]) throws Exception
{
int ch;
mydatabase ms=new mydatabse();
do
{
System.out.println("[1].Insert");
Thread.sleep(1000);
System.out.println("[2].Delete");
Thread.sleep(1000);
System.out.println("Enter your choice");
ch=sc.nextInt();
switch (ch)
{
case1:
ms.addrecord();
break;
case2:
ms.deleterecord();
break;
default:Sytsem.out.println("Wrong choice");
}
}while(ch!=2);
{
Connection cn;
ResultSet rs;
Statement st;
PreparedStatement pst;
mydatabse() //constructor
{
try{
class.forName("oracle.jdbc.driver.OracleDriver");
cn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","<password>");
System.out.println("Connection established");
}catch(Exception e) { }
}//const end
void addrecord()
{
System.out.print("welcome to insert module:");
try {
pst=cn.prepareStatement("insert into emp values (?,?)");
Scanner sc= new Scanner (System.in);
System.out.println("Enter emp no");
int eno=sc.nextInt();
System.out.println("Enter emp name:");
String name=sc.next();
pst.setInt(1,eno);
pst.set(2,name);
int x=pst.executeUpdtae();
if(x>0)
System.out.println("inserted");
} catch(Exception e)
{
System.out.println("Already available");
}
}
void deleterecord()
{
System.out.print("welcome to delete module:");
try {
pst=cn.prepareStatement("delete from emp where ename=?");
System.out.println("Enter emp name:");
String name=sc.next();
pst.setString(1,name);
int x=pst.executeUpdtae();
if(x>0)
System.out.println("deleted");
} catch(Exception e)
{
e.printStsackTrace();
}
}
public static void main (String s[]) throws Exception
{
int ch;
mydatabase ms=new mydatabse();
do
{
System.out.println("[1].Insert");
Thread.sleep(1000);
System.out.println("[2].Delete");
Thread.sleep(1000);
System.out.println("Enter your choice");
ch=sc.nextInt();
switch (ch)
{
case1:
ms.addrecord();
break;
case2:
ms.deleterecord();
break;
default:Sytsem.out.println("Wrong choice");
}
}while(ch!=2);
No comments:
Post a Comment