/
Dynamic_Get.java
161 lines (120 loc) · 4.45 KB
/
Dynamic_Get.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package Com.Practice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
import java.util.Scanner;
public class Dynamic_Get {
static Scanner s = new Scanner(System.in);
static Scanner s1 = new Scanner(System.in);
static Statement st = null;
static Connection con;
static ResultSet rs = null;
static int id = 0; static Double sa; static String na;
public static void add() {
// System.out.print("\n !!! Add Employee !!!\n");
// System.out.print("\n Enter Employee ID : ");
// id=s.nextInt();
System.out.print(" Enter Employee Name : ");
na =s1.nextLine();
System.out.print(" Enter Employee salary : ");
sa = s.nextDouble();
try {
st.executeUpdate("insert into employeed value('"+id+"','"+na+"','"+sa+"')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(" \nValues Inserted Succesfully....");
}
public static void check() {
System.out.print("\n !!! Add Employee !!!\n");
System.out.print("\n Enter Employee ID : ");
id=s.nextInt();
try {
while(rs.next()) {
int g = rs.getInt("ID");
if(id==g) {
System.out.println("Employee Already Exist !!! \n Enter Valid ID");
check();
}
else {
add();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
int j;
do {
System.out.println("___________________________________________________________________________________");
System.out.print("\n 1. Add Employee \n 2. Delete Employee\n 3. View All Employee \n 99. Exit \n\n" );
System.out.print("Enter Your Choice : ");
j =s.nextInt();
System.out.println("___________________________________________________________________________________");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con =DriverManager.getConnection("jdbc:mysql://localhost:3306/employee","root","gvirus");
// System.out.println("Connection Established..");
st=con.createStatement();
rs = st.executeQuery("select * from employeed");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
if(j==1) {
System.out.print("\n !!! Add Employee !!!\n");
System.out.print("\n Enter Employee ID : ");
id=s.nextInt();
while(rs.next()){
int g = rs.getInt("ID");
if(g==id) {
System.out.println("Employee Already Exist !!! \n Enter Valid ID");
check();
}
else {
// System.out.print("GOOD");
}
}
System.out.print(" Enter Employee Name : ");
na =s1.nextLine();
System.out.print(" Enter Employee salary : ");
sa = s.nextDouble();
st.executeUpdate("insert into employeed value('"+id+"','"+na+"','"+sa+"')");
System.out.println(" \nValues Inserted Succesfully....");
// check();
}else if(j==2) {
System.out.print(" !!! Delete Employee !!!\n");
System.out.print(" Enter Employee ID : ");
int k = s.nextInt();
st.executeUpdate("delete from employeed where ID = '"+k+"'");
System.out.print("\n !!! Successfully Deleted !!! ");
}else if(j==3) {
System.out.println(" !!! All Employees !!! \n");
System.out.println(" ID Name Salary");
while(rs.next()) {
int id1 = rs.getInt("ID");
String n1 =rs.getString("Name");
Double d1 = rs.getDouble("Salary");
System.out.println(" "+id1+" "+n1+" "+d1);
// f = new Formatter();
// System.out.println(f.format("%10d", 9878));
// System.out.println(f.format("%10d", 9878));
}
}
else if(j==99) {
System.out.print("\n Thank you... ");
System.exit(0);
}
else
{
System.out.println(" \n Invalid Choice !, Please Select Correct Option ");
}
}while(j!=99);
}
}