Skip to content

Commit 98c77a1

Browse files
committed
EMPLOYEE CLASS
1 parent 91dd176 commit 98c77a1

3 files changed

Lines changed: 104 additions & 26 deletions

File tree

src/main/java/com/napier/sem/App.java

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,41 @@
33
import java.sql.*;
44

55
public class App {
6+
// Hold the connection for this instance
7+
private Connection con = null;
68

7-
public static void main(String[] args)
8-
{
9-
// Create new Application
10-
App a = new App();
9+
public static void main(String[] args) {
10+
// Create new Application
11+
App a = new App();
1112

12-
// Connect to database
13-
a.connect();
13+
// Connect to database
14+
a.connect();
1415

15-
// Disconnect from database
16-
a.disconnect();
17-
}
18-
private Connection con = null;
16+
a.disconnect();
17+
}
18+
public Employee getEmployee(int id) {
19+
try {
20+
Statement stmt = con.createStatement();
21+
String query =
22+
"SELECT emp_no, first_name, last_name " +
23+
"FROM employees " +
24+
"WHERE emp_no = " + id;
25+
ResultSet rs = stmt.executeQuery(query);
26+
27+
if (!rs.next()) return null;
1928

20-
public void connect() { /* load driver + DriverManager.getConnection(...) */ }
29+
Employee e = new Employee();
30+
e.emp_no = rs.getInt("emp_no");
31+
e.first_name = rs.getString("first_name");
32+
e.last_name = rs.getString("last_name");
33+
return e;
34+
} catch (SQLException ex) {
35+
System.out.println(ex.getMessage());
36+
return null;
37+
}
38+
}
2139

22-
public void disconnect() { /* close con if not null */ }
23-
{
40+
public void connect() {
2441
try {
2542
// Load Database driver
2643
Class.forName("com.mysql.cj.jdbc.Driver");
@@ -29,37 +46,39 @@ public void disconnect() { /* close con if not null */ }
2946
System.exit(-1);
3047
}
3148

32-
// Connection to the database
33-
Connection con = null;
3449
int retries = 100;
3550
for (int i = 0; i < retries; ++i) {
3651
System.out.println("Connecting to database...");
3752
try {
3853
// Wait a bit for db to start
3954
Thread.sleep(30000);
40-
// Connect to database
41-
con = DriverManager.getConnection("jdbc:mysql://db:3306/employees?allowPublicKeyRetrieval=true&useSSL=false", "root", "example");
55+
56+
// Connect to database — NOTE: assign to the FIELD, not a local var
57+
con = DriverManager.getConnection(
58+
"jdbc:mysql://db:3306/employees?allowPublicKeyRetrieval=true&useSSL=false",
59+
"root",
60+
"example"
61+
);
62+
4263
System.out.println("Successfully connected");
43-
// Wait a bit
44-
Thread.sleep(10000);
45-
// Exit for loop
46-
break;
64+
return; // success
4765
} catch (SQLException sqle) {
48-
System.out.println("Failed to connect to database attempt " + Integer.toString(i));
66+
System.out.println("Failed to connect to database attempt " + i);
4967
System.out.println(sqle.getMessage());
5068
} catch (InterruptedException ie) {
5169
System.out.println("Thread interrupted? Should not happen.");
5270
}
5371
}
72+
}
5473

74+
public void disconnect() {
5575
if (con != null) {
5676
try {
57-
// Close connection
5877
con.close();
59-
} catch (Exception e) {
78+
System.out.println("Disconnected");
79+
} catch (SQLException e) {
6080
System.out.println("Error closing connection to database");
6181
}
6282
}
6383
}
64-
65-
}
84+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.napier.sem;
2+
3+
/**
4+
* Represents an employee
5+
*/
6+
public class Employee
7+
{
8+
/**
9+
* Employee number
10+
*/
11+
public int emp_no;
12+
13+
/**
14+
* Employee's first name
15+
*/
16+
public String first_name;
17+
18+
/**
19+
* Employee's last name
20+
*/
21+
public String last_name;
22+
23+
/**
24+
* Employee's job title
25+
*/
26+
public String title;
27+
28+
/**
29+
* Employee's salary
30+
*/
31+
public int salary;
32+
33+
/**
34+
* Employee's current department
35+
*/
36+
public String dept_name;
37+
38+
/**
39+
* Employee's manager
40+
*/
41+
public String manager;
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.napier.sem;
2+
3+
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
4+
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
5+
public class Main {
6+
public static void main(String[] args) {
7+
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
8+
// to see how IntelliJ IDEA suggests fixing it.
9+
System.out.printf("Hello and welcome!");
10+
11+
for (int i = 1; i <= 5; i++) {
12+
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
13+
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
14+
System.out.println("i = " + i);
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)