Before running this program, create access database and add a table named 'emp' with the following fields
eno - Number
ename - Text
salary - Number
Cofingure ODBC in your system using Control Panel -> Administrative Tools -> ODBC
import java.io.*;
import java.sql.*;
public class jdbcprg {
static void myLine(){
for (int i=1;i<=80;i++)
{
System.out.print("*");
}
System.out.println();
}
public static void main(String[] args) {
Connection con;
Statement st;
BufferedReader bin;
ResultSet rs;
ResultSetMetaData rm;
String eno, ename, salary;
int ch, nof;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:karthik");
st = con.createStatement();
bin = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("Choose Option");
System.out.println("1. Select");
System.out.println("2. Insert");
System.out.println("3. Update");
System.out.println("4. Delete");
System.out.println("0. Exit");
ch = Integer.parseInt(bin.readLine());
if (ch==1)
{
rs = st.executeQuery("select * from emp");
rm = rs.getMetaData();
nof = rm.getColumnCount();
myLine();
for(int i=1; i<=nof; i++)
{
System.out.print(rm.getColumnName(i)+"\t\t");
}
System.out.println();
myLine();
while(rs.next())
{
for(int i=1; i<=nof; i++)
{
System.out.print(rs.getString(i) +"\t\t");
}
System.out.println();
}
myLine();
}
else if(ch==2)
{
do
{
System.out.println("Enter E.No");
eno = bin.readLine();
System.out.println("Enter Name");
ename = bin.readLine();
System.out.println("Enter Salary");
salary = bin.readLine();
st.execute("insert into emp values("+eno+",'"+ename+"',"+salary+")");
System.out.println("1 Record inserted");
System.out.println("Continue ? [y/n]");
eno = bin.readLine();
}while(eno.equalsIgnoreCase("y"));
}
else if(ch==3)
{
System.out.println("Enter E.No. to Edit :");
eno = bin.readLine();
System.out.println("Enter Name");
ename = bin.readLine();
System.out.println("Enter Salary");
salary = bin.readLine();
st.execute("update emp set ename='"+ename+"', salary="+salary+" where eno ="+eno);
System.out.println("1 Record Updated");
}
else if(ch==4)
{
System.out.println("Enter E.No. to Delete :");
eno = bin.readLine();
st.execute("delete from emp where eno ="+eno);
System.out.println("Record Deleted");
}
else if(ch==0)
{
bin.close();
con.close();
System.exit(0);
}
}
}
catch(Exception e)
{
System.out.println("Error :"+e.getMessage());
}
}
}