Essays.club - Dissertations, travaux de recherche, examens, fiches de lecture, BAC, notes de recherche et mémoires
Recherche

La base de données

Par   •  13 Juin 2018  •  1 678 Mots (7 Pages)  •  542 Vues

Page 1 sur 7

...

}else if (valueName.equals("first_name")) {

columnString = insertIntoTableEmployees.get(valueName) + (valuesCount != 0 ? "’ , " : "’)");

}else

{

columnString = insertIntoTableEmployees.get(valueName) + (valuesCount != 0 ? "’ , ’" : "’)");

}

sqlStatement.append(columnString);

}

stmt.executeUpdate(sqlStatement.toString());

stmt.close();

System.out.println("Inserted into table " + tableName + "...");

} catch (SQLException e) {

e.printStackTrace();

}

}

public void deleteEmployee(Employee employee, Connection con) {

Statement stmt;

String tableName = "employees";

try {

stmt = con.createStatement();

String deleteStatement = "DELETE FROM " + tableName + " WHERE employee_id = " + employee.getId();

stmt.executeUpdate(deleteStatement);

stmt.close();

System.out.println("Dropped table " + tableName + " from database...");

} catch (SQLException e) {

e.printStackTrace();

}

}

public void updateEmployee(Employee employee, Connection con) {

HashMap<String, String> insertIntoTableEmployees = new HashMap<String, String>();

insertIntoTableEmployees.put("employee_id", employee.getId().toString());

insertIntoTableEmployees.put("first_name", employee.getFirstName());

insertIntoTableEmployees.put("last_name", employee.getLastName());

insertIntoTableEmployees.put("email", employee.getEmail());

insertIntoTableEmployees.put("phone_number", employee.getPhoneNumber());

insertIntoTableEmployees.put("hire_date", "TO_DATE(’" + employee.getHireDate().toString() + "’,’yyyy-mm-dd’)");

insertIntoTableEmployees.put("job_id", employee.getJob().getId().toString());

insertIntoTableEmployees.put("salary", employee.getSalary().toString());

insertIntoTableEmployees.put("commission_pct", employee.getCommissionPoints().toString());

insertIntoTableEmployees.put("manager_id", employee.getManager().getId().toString());

insertIntoTableEmployees.put("department_id", employee.getDepartment().getId().toString());

String tableName = "employees";

Statement stmt;

try {

stmt = con.createStatement();

String createTableString = "UPDATE " + tableName + " SET ";

StringBuilder sqlStatement = new StringBuilder();

sqlStatement.append(createTableString);

Integer columnsCount = insertIntoTableEmployees.keySet().size();

for (String columnName : insertIntoTableEmployees.keySet()) {

columnsCount--;

String columnString;

if (columnName.equals("hire_date")) {

columnString = columnName + " = " + insertIntoTableEmployees.get(columnName) + (columnsCount != 0 ? " , " : "’ ");

} else {

columnString = columnName + " = ’" + insertIntoTableEmployees.get(columnName) + (columnsCount != 0 ? "’ , " : "’ ");

}

sqlStatement.append(columnString);

}

sqlStatement.append("WHERE employee_id = " + employee.getId());

stmt.executeUpdate(sqlStatement.toString());

stmt.close();

System.out.println("Created table " + tableName + " in database...");

} catch (SQLException e) {

e.printStackTrace();

}

}

}

...

Télécharger :   txt (7.4 Kb)   pdf (46.6 Kb)   docx (13.5 Kb)  
Voir 6 pages de plus »
Uniquement disponible sur Essays.club