-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeDB.java
More file actions
47 lines (44 loc) · 1.75 KB
/
EmployeeDB.java
File metadata and controls
47 lines (44 loc) · 1.75 KB
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
import java.sql.*;
import java.util.Scanner;
public class EmployeeDB {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root", "");
Statement stmt = con.createStatement();
stmt.execute("CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER, FName VARCHAR(16), LName VARCHAR(16), Project VARCHAR(16), Salary INTEGER)");
Scanner in = new Scanner(System.in);
PreparedStatement ps = con.prepareStatement("INSERT INTO EMPLOYEE VALUES (?, ?, ?, ?, ?)");
// for(int i=0; i<5; i++) {
// System.out.println("Enter id, fname, lname, proj, salary");
// ps.setInt(1, in.nextInt());
// ps.setString(2, in.next());
// ps.setString(3, in.next());
// ps.setString(4, in.next());
// ps.setInt(5, in.nextInt());
// ps.execute();
// }
System.out.println("ALL RECORDS : ");
ResultSet rs = stmt.executeQuery("SELECT * FROM EMPLOYEE");
while(rs.next()) {
System.out.println("id: " + rs.getInt(1));
System.out.println("fname: " + rs.getString(2));
System.out.println("lname: " + rs.getString(3));
System.out.println("proj: " + rs.getString(4));
System.out.println("salary: " + rs.getInt(5));
System.out.println("");
}
rs = stmt.executeQuery("SELECT * FROM EMPLOYEE WHERE PROJECT = 'WebDev'");
while(rs.next()) {
System.out.println("id: " + rs.getInt(1));
System.out.println("fname: " + rs.getString(2));
System.out.println("lname: " + rs.getString(3));
System.out.println("proj: " + rs.getString(4));
System.out.println("salary: " + rs.getInt(5));
System.out.println("");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}