1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
public class EmployeeManager {
Connection connection;
public EmployeeManager() {}
public void openConnection() {
ConfigurationPostgresql cfp = new ConfigurationPostgresql("localhost", "root", "xxx", "8080", "eltechdb", "");
connection = DriverManager.getConnection(cfp.getJDBCUrl(), cfp.getUser(), cfp.getPassword());
}
// create N rows in the database with Employee Object
public void saveEmployee(List<Emp> emps) {
String req = "INSERT INTO tblEmployees (emp_no, birth_date, first_name, last_name, gender, hire_date) VALUES=(emp_no=:emp_no, birth_date=:birth_date, first_name=:first_name, last_name=:last_name, gender=:gender, hire_date=:hire_date);";
PreparedStatement prestmt = connection.prepareStatement(req);
while (emps.hasNext()) {
try {
theEmp = (Emp) emps.next();
prestmt.setString("emp_no", theEmp.getId());
prestmt.setString("first_name", theEmp.getFirstName());
prestmt.setString("last_name", theEmp.getLastName());
prestmt.setString("birth_date", theEmp.getBirthDate());
prestmt.setString("gender", theEmp.getGender());
prestmt.setString("hire_date", theEmp.getHireDate());
prestmt.executeUpdate();
}
catch (SQLException ex) {
System.out.println("error save Employee");
}
prestmt.close();
}
}
} |
Partager