|
| 1 | +package experimentThree; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.DriverManager; |
| 5 | +import java.sql.PreparedStatement; |
| 6 | +import java.sql.ResultSet; |
| 7 | +import java.util.Scanner; |
| 8 | +import java.sql.Statement; |
| 9 | + |
| 10 | + |
| 11 | +public class jdbc { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + try{ |
| 15 | + |
| 16 | + Class.forName("com.mysql.jdbc.Driver"); |
| 17 | + |
| 18 | + Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3308/employee","root","Akash@123"); |
| 19 | + |
| 20 | + Statement stmt=con.createStatement(); |
| 21 | + |
| 22 | + int ans=1; |
| 23 | + |
| 24 | + do { |
| 25 | + |
| 26 | + System.out.println("1. Insert a record "); |
| 27 | + |
| 28 | + System.out.println("2. Delete a record "); |
| 29 | + |
| 30 | + System.out.println("3. Modify/Edit a record "); |
| 31 | + |
| 32 | + System.out.println("4. Display list of records "); |
| 33 | + |
| 34 | + Scanner sc = new Scanner(System.in); |
| 35 | + |
| 36 | + System.out.println("Enter your choice:"); |
| 37 | + |
| 38 | + int ch = sc.nextInt(); |
| 39 | + |
| 40 | + String ename; |
| 41 | + |
| 42 | + int eno,age; |
| 43 | + |
| 44 | + String query=""; |
| 45 | + |
| 46 | + switch(ch) { |
| 47 | + |
| 48 | + case 1: |
| 49 | + |
| 50 | + System.out.println("Enter employee number:"); |
| 51 | + |
| 52 | + eno = sc.nextInt(); |
| 53 | + |
| 54 | + System.out.println("Enter employee name:"); |
| 55 | + |
| 56 | + ename = sc.next(); |
| 57 | + |
| 58 | + System.out.println("Enter employee age:"); |
| 59 | + |
| 60 | + age = sc.nextInt(); |
| 61 | + |
| 62 | + query = "INSERT INTO emp " + "VALUES (" + eno+ ",'" + ename+"',"+ age+")"; |
| 63 | + |
| 64 | + stmt.executeUpdate(query); |
| 65 | + |
| 66 | + break; |
| 67 | + |
| 68 | + case 2: |
| 69 | + |
| 70 | + System.out.println("Enter employee number:"); |
| 71 | + |
| 72 | + eno = sc.nextInt(); |
| 73 | + |
| 74 | + query = "delete from emp where eno='"+eno+"'"; |
| 75 | + |
| 76 | + stmt.executeUpdate(query); |
| 77 | + |
| 78 | + System.out.println("Record is deleted from the table successfully.................."); |
| 79 | + |
| 80 | + break; |
| 81 | + |
| 82 | + case 3: |
| 83 | + |
| 84 | + PreparedStatement ps = null; |
| 85 | + |
| 86 | + query = "update emp set name=? where eno=? "; |
| 87 | + |
| 88 | + ps = con.prepareStatement(query); |
| 89 | + |
| 90 | + System.out.println("Enter employee number:"); |
| 91 | + |
| 92 | + eno = sc.nextInt(); |
| 93 | + |
| 94 | + System.out.println("Enter employee name:"); |
| 95 | + |
| 96 | + ename = sc.next(); |
| 97 | + |
| 98 | + ps.setString(1, ename); |
| 99 | + |
| 100 | + ps.setInt(2, eno); |
| 101 | + |
| 102 | + ps.executeUpdate(); |
| 103 | + |
| 104 | + System.out.println("Record is updated successfully......"); |
| 105 | + |
| 106 | + break; |
| 107 | + |
| 108 | + case 4: |
| 109 | + |
| 110 | + ResultSet rs=stmt.executeQuery("select * from emp"); |
| 111 | + |
| 112 | + while(rs.next()) |
| 113 | + |
| 114 | + System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3)); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + System.out.println("Enter another(1/0)"); |
| 119 | + |
| 120 | + ans = sc.nextInt(); |
| 121 | + |
| 122 | + }while(ans==1); |
| 123 | + |
| 124 | + con.close(); |
| 125 | + |
| 126 | + }catch(Exception e){ |
| 127 | + System.out.println(e); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments