Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Paste this thing in class Employee
- public static void loadFromDB(){
- String jdbcUrl = "jdbc:postgresql://localhost/postgres";
- String username = "postgres";
- String password = "postgres";
- Connection conn = null;
- Statement stmt = null;
- ResultSet rs = null;
- try
- {
- conn = DriverManager.getConnection(jdbcUrl, username, password);
- stmt = conn.createStatement();
- rs = stmt.executeQuery("select id,first_name,last_name, year_of_birth,title,monthly_salary from employees"+
- " where id not in (select emp_id from managers)"+
- " and id not in (select emp_id from developers)");
- while (rs.next())
- {
- 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"));
- ResultSet rs1 = null;
- rs1 = conn.createStatement().executeQuery("select emp_id,language from emp_languages where emp_id=" + employee.getId());
- while (rs1.next())
- {
- employee.addLanguage(rs1.getString("language"));
- }
- employees.put(employee.getFullName(), employee);
- System.out.println(employee.toString() + employee.getLanguages().toString());
- }
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try {
- if (stmt != null) {
- stmt.close();
- }
- if (rs != null) {
- rs.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement