Write a Menu
driven program in Java for the following.
- Insert Record into the Table.
- Update The Existing Record.
- Display all the Records from the Table.
import java.io.*;
import java.sql.*;
class Menu
{
static Connection cn;
static Statement st;
static ResultSet rs;
static BufferedReader
br=new BufferedReader(new InputStreamReader(System.in));
public static void
main(String args[])
{
int e,k,ch,sal;
String en;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn=DriverManager.getConnection("jdbc:odbc:Ass","","");
st=cn.createStatement();
do
{
System.out.println("1.
Insert ");
System.out.println("2.
Update ");
System.out.println("3.
Delete ");
System.out.println("4.
Display ");
System.out.println("5.
Exit ");
System.out.println("Enter
Ur Choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case
1:
System.out.println("Enter
the Eno");
e=Integer.parseInt(br.readLine());
System.out.println("Enter
the Ename");
en=br.readLine();
System.out.println("Enter
Salary");
sal=Integer.parseInt(br.readLine());
String
str="insert into emp values(" + e + ",'"+ en +
"'," + sal +")";
k=st.executeUpdate(str);
if(k>0)
{
System.out.println("Record
Is Added");
}
break;
case
2:
System.out.println("Enter
the Eno");
e=Integer.parseInt(br.readLine());
System.out.println("Enter
the Ename");
en=br.readLine();
String
ss="update emp set ename='" + en + "' where eno="+e;
k=st.executeUpdate(ss);
if(k>0)
{
System.out.println("Record
Is Updated");
}
break;
case
3:
System.out.println("Enter
the Eno");
e=Integer.parseInt(br.readLine());
String
sd="delete from emp where eno=" +e;
k=st.executeUpdate(sd);
if(k>0)
{
System.out.println("Record
Is Deleted");
}
break;
case
4:
rs=st.executeQuery("select
* from emp");
while(rs.next())
{
System.out.println(rs.getInt("eno")
+ "\t" +
rs.getString("ename")+"\t"+rs.getInt("sal"));
}
break;
case
5:
System.exit(0);
}
}
while(ch!=5);
}
catch(Exception
et)
{
System.out.println("Error");
}
}
}
No comments:
Post a Comment