33import java .sql .*;
44
55public 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+ }
0 commit comments