JDBC MENU DRIVEN

1 package jdbcapplication;
 2
 3 import java.io.*;
 4
 5 public class DbConcept {
 6
 7     public static void main(String args[]) throws IOException {
 8         InputStreamReader in = new InputStreamReader(System.in);
 9         BufferedReader br = new BufferedReader(in);
10         int option, eid, esal;
11         String ename, ejob;
12         System.out.println("1. Select Records");
13         System.out.println("2. Insert Records");
14         System.out.println("3. Delete Records");
15         System.out.println("4. Update Records");
16         System.out.println("Enter Above Mention OPtion : ");
17         option = Integer.parseInt(br.readLine());
18         switch (option) {
19             case 1:
20                 System.out.println("EMPLOYEE RECORDS SELECTION");
21                 Select obj = new Select();
22                 break;
23             case 2:
24                 System.out.println("EMPLOYEE RECORDS INSERTION");
25                 System.out.println("Enter Employee id : ");
26                 eid = Integer.parseInt(br.readLine());
27                 System.out.println("Enter Employee Name : ");
28                 ename = br.readLine();
29                 System.out.println("Enter Employee Salary : ");
30                 esal = Integer.parseInt(br.readLine());
31                 System.out.println("Enter Employee Job : ");
32                 ejob = br.readLine();
33                 Insert inobj = new Insert(eid, ename, esal, ejob);
34                 break;
35             case 3:
36                 System.out.println("EMPLOYEE RECORDS DELETION");
37                 System.out.println("Enter Employee id : ");
38                 eid = Integer.parseInt(br.readLine());
39                 Delet demo = new Delet(eid);
40                 break;
41             case 4:
42                 System.out.println("EMPLOYEE RECORDS UPDATION");
43                 System.out.println("Enter Employee Name u hav to upadte : ");
44                 ename = br.readLine();
45                 System.out.println("Enter Employee Salary u hav to upadte: ");
46                 esal = Integer.parseInt(br.readLine());
47                 System.out.println("Enter Employee Job u hav to upadte: ");
48                 ejob = br.readLine();
49                 System.out.println("Enter confm Employee id that u hav to change : ");
50                 eid = Integer.parseInt(br.readLine());
51                 Update up = new Update(ename, esal, ejob, eid);
52                 break;
53             default:
54                 System.out.println("Invalid Option");
55
56
57         }
58     }
59 }

CASE 1: SELECT RECORDS
1 package jdbcapplication;
 2 import java.sql.*;
 3 public class Select
 4 {
 5     Connection cnt;
 6     PreparedStatement pstm;
 7     String driver,url;
 8     ResultSet rs;
 9     String qry="select * from emp";
10     Select()
11     {
12     driver="com.mysql.jdbc.Driver";
13     url="jdbc:mysql://localhost/bhupesh";
14     try
15     {
16     cnt=DriverManager.getConnection(url,"root","ashok");
17     pstm=cnt.prepareStatement(qry);
18     rs=pstm.executeQuery();
19         while(rs.next())
20         {
21             int id=rs.getInt(1);
22             String name=rs.getString(2);
23             int sal=rs.getInt(3);
24             String job=rs.getString(4);
25            
26             System.out.println(" | "+id+" | "+name+" | "+sal+" | "+job);
27         }
28     System.out.println("Sucessfull Records Done");
29     rs.close();
30     cnt.close();
31    
32     }
33         catch (SQLException ex) {
34             System.out.println("Error : "+ex.getMessage());
35         }
36     }
37 }

CASE 2: INSERT RECORDS
1 package jdbcapplication;
 2 import java.sql.*;
 3 public class Insert
 4 {
 5     Connection cnt;
 6     PreparedStatement pstm;
 7     String driver,url;
 8     String qry="insert into emp(eid,ename,esal,ejob) values(?,?,?,?)";
 9     Insert(int id,String name,int sal,String job)
10     {
11     driver="com.mysql.jdbc.Driver";
12     url="jdbc:mysql://localhost/bhupesh";
13     try
14     {
15     cnt=DriverManager.getConnection(url,"root","ashok");
16     pstm=cnt.prepareStatement(qry);
17     pstm.setInt(1,id);
18     pstm.setString(2,name);
19     pstm.setInt(3,sal);
20     pstm.setString(4,job);
21     pstm.execute();
22     System.out.println("Sucessfull");
23     cnt.close();
24     }
25         catch (SQLException ex) {
26             System.out.println("Error : "+ex.getMessage());
27         }
28     }
29 }

CASE 3: DELETE RECORDS
1 package jdbcapplication;
 2 import java.sql.*;
 3 public class Delet
 4 {
 5     Connection cnt;
 6     PreparedStatement pstm;
 7     String driver,url;
 8     String qry="delete from emp where eid=?";
 9     Delet(int eid)
10     {
11     driver="com.mysql.jdbc.Driver";
12     url="jdbc:mysql://localhost/bhupesh";
13     try
14     {
15     cnt=DriverManager.getConnection(url,"root","ashok");
16     pstm=cnt.prepareStatement(qry);
17     pstm.setInt(1,eid);
18     pstm.execute();
19     System.out.println("Deletion Done");
20     pstm.close();
21     cnt.close();
22     }
23         catch (SQLException ex) {
24             System.out.println("Error : "+ex.getMessage());
25         }
26     }
27
28 }

CASE 4: UPDATE  RECORDS
1 package jdbcapplication;
 2 import java.sql.*;
 3 public class Update
 4 {
 5     Connection cnt;
 6     PreparedStatement pstm;
 7     String driver,url;
 8     String qry="update emp set ename=?,esal=?,ejob=? where eid=?";
 9     Update(String ename,int esal,String ejob,int eid)
10     {
11     driver="com.mysql.jdbc.Driver";
12     url="jdbc:mysql://localhost/bhupesh";
13     try
14     {
15     cnt=DriverManager.getConnection(url,"root","ashok");
16     pstm=cnt.prepareStatement(qry);
17    
18     pstm.setString(1,ename);
19     pstm.setInt(2,esal);
20     pstm.setString(3,ejob);
21     pstm.setInt(4,eid);
22  
23     pstm.executeUpdate();
24     System.out.println("Updation Sucessfull Done");
25     cnt.close();
26     }
27         catch (SQLException ex) {
28             System.out.println("Error : "+ex.getMessage());
29         }
30     }
31
32 }



No comments:

Post a Comment

Python Code Loop Program-1

 #Chapter -7 Qno - 2 num=4; while(num<10):     print("Number is %d"%num);     num=num+1; Output Number is 4 Number is 5 Number ...