Advertisement
Guest User

Untitled

a guest
Feb 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1. package com.napier.sem;
  2.  
  3. import java.sql.*;
  4.  
  5. public class App {
  6.     private Connection con = null;
  7.  
  8.     /**
  9.      * Connect to the MySQL database.
  10.      */
  11.     public void connect() {
  12.         try {
  13.             // Load Database driver
  14.             Class.forName("com.mysql.jdbc.Driver");
  15.         } catch (ClassNotFoundException e) {
  16.             System.out.println("Could not load SQL driver");
  17.             System.exit(-1);
  18.         }
  19.  
  20.         int retries = 10;
  21.         for (int i = 0; i < retries; ++i) {
  22.             System.out.println("Connecting to database...");
  23.             try {
  24.                 // Wait a bit for db to start
  25.                 Thread.sleep(30000);
  26.                 // Connect to database
  27.                 con = DriverManager.getConnection("jdbc:mysql://db:3306/employees?useSSL=false", "root", "example");
  28.                 System.out.println("Successfully connected");
  29.                 break;
  30.             } catch (SQLException sqle) {
  31.                 System.out.println("Failed to connect to database attempt " + Integer.toString(i));
  32.                 System.out.println(sqle.getMessage());
  33.             } catch (InterruptedException ie) {
  34.                 System.out.println("Thread interrupted? Should not happen.");
  35.             }
  36.         }
  37.     }
  38.  
  39.     /**
  40.      * Disconnect from the MySQL database.
  41.      */
  42.     public void disconnect() {
  43.         if (con != null) {
  44.             try {
  45.                 // Close connection
  46.                 con.close();
  47.             } catch (Exception e) {
  48.                 System.out.println("Error closing connection to database");
  49.             }
  50.         }
  51.     }
  52.  
  53.     public Employee getEmployee(int ID)
  54.     {
  55.         try
  56.         {
  57.             // Create an SQL statement
  58.             Statement stmt = con.createStatement();
  59.             // Create string for SQL statement
  60.             String strSelect =
  61.                     "SELECT emp_no, first_name, last_name "
  62.                             + "FROM employees "
  63.                             + "WHERE emp_no = " + ID;
  64.             // Execute SQL statement
  65.             ResultSet rset = stmt.executeQuery(strSelect);
  66.             // Return new employee if valid.
  67.             // Check one is returned
  68.             if (rset.next())
  69.             {
  70.                 Employee emp = new Employee();
  71.                 emp.emp_no = rset.getInt("emp_no");
  72.                 emp.first_name = rset.getString("first_name");
  73.                 emp.last_name = rset.getString("last_name");
  74.                 return emp;
  75.             }
  76.             else
  77.                 return null;
  78.         }
  79.         catch (Exception e)
  80.         {
  81.             System.out.println(e.getMessage());
  82.             System.out.println("Failed to get employee details");
  83.             return null;
  84.         }
  85.     }
  86.  
  87.     public void displayEmployee(Employee emp)
  88.     {
  89.         if (emp != null)
  90.         {
  91.             System.out.println(
  92.                     emp.emp_no + " "
  93.                             + emp.first_name + " "
  94.                             + emp.last_name + "\n"
  95.                             + emp.title + "\n"
  96.                             + "Salary:" + emp.salary + "\n"
  97.                             + emp.dept_name + "\n"
  98.                             + "Manager: " + emp.manager + "\n");
  99.         }
  100.     }
  101.  
  102.     public static void main(String[] args)
  103.     {
  104.         // Create new Application
  105.         App a = new App();
  106.  
  107.         // Connect to database
  108.         a.connect();
  109.         // Get Employee
  110.         Employee emp = a.getEmployee(10001);
  111.         // Display results
  112.         a.displayEmployee(emp);
  113.  
  114.         // Disconnect from database
  115.         a.disconnect();
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement