Write a java program to accept the details of Employee (Eno,Ename,Sal) from user and store it into the database, display it on the screen.
import java.sql.*; import java.io.*; import javax.sql.*; class slip13 { public static void main(String args[]) { Connection con; Statement state; ResultSet rs; int ch; boolean flag=true; String decision; int eno; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:my1dsn.dsn"); System.out.println("Statement object created"); do { System.out.println("\n"); System.out.println("Menu:"); System.out.println("1.Insert Record into the table"); System.out.println("2.Display all the records from the table"); System.out.println("3.Exit"); System.out.println("Enter your choice:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: System.out.println("Enter Employee No:"); eno=Integer.parseInt(br.readLine()); System.out.println("Enter Employee Name:"); String ename=br.readLine(); System.out.println("Enter Employee Salary:"); int sal=Integer.parseInt(br.readLine()); String sql="Insert into employee values(?,?,?)"; PreparedStatement p=con.prepareStatement(sql); p.setInt(1,eno); p.setString(2,ename); p.setInt(3,sal); p.executeUpdate(); System.out.println("Record Added"); break; case 2: state=con.createStatement(); sql="Select * from employee"; rs=state.executeQuery(sql); while(rs.next()) { System.out.println("\n"); System.out.println("\t"+rs.getInt(1)); System.out.println("\t"+rs.getString(2)); System.out.println("\t"+rs.getInt(3)); } break; case 3: System.exit(0); default: System.out.println("Invalid Choice"); break; } } hile (ch!=3); } catch(Exception e) { System.out.println(e); }}}
Write a java program to accept the details of Employee (Eno,Ename,Sal) from user and store it into the database, display it on the screen.
Reviewed by
on
April 27, 2015
Rating: