Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. //Paste this thing in class Employee
  2. public static void loadFromDB(){
  3.  
  4. String jdbcUrl = "jdbc:postgresql://localhost/postgres";
  5. String username = "postgres";
  6. String password = "postgres";
  7.  
  8. Connection conn = null;
  9. Statement stmt = null;
  10. ResultSet rs = null;
  11.  
  12.  
  13. try
  14. {
  15. conn = DriverManager.getConnection(jdbcUrl, username, password);
  16. stmt = conn.createStatement();
  17. rs = stmt.executeQuery("select id,first_name,last_name, year_of_birth,title,monthly_salary from employees"+
  18. " where id not in (select emp_id from managers)"+
  19. " and id not in (select emp_id from developers)");
  20. while (rs.next())
  21. {
  22. Employee employee = new Employee(rs.getInt("id"),rs.getString("first_name"),rs.getString("last_name"),rs.getInt("year_of_birth"),rs.getString("title"),rs.getFloat("monthly_salary"));
  23.  
  24. ResultSet rs1 = null;
  25. rs1 = conn.createStatement().executeQuery("select emp_id,language from emp_languages where emp_id=" + employee.getId());
  26. while (rs1.next())
  27. {
  28. employee.addLanguage(rs1.getString("language"));
  29. }
  30. employees.put(employee.getFullName(), employee);
  31. System.out.println(employee.toString() + employee.getLanguages().toString());
  32. }
  33. } catch (SQLException e)
  34. {
  35. e.printStackTrace();
  36. }
  37. finally
  38. {
  39. try {
  40. if (stmt != null) {
  41. stmt.close();
  42. }
  43. if (rs != null) {
  44. rs.close();
  45. }
  46. if (conn != null) {
  47. conn.close();
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement