Witam, napisałem kod historii logowania (jdbc), ale potrzebuję to samo w JPA Hibernate. I nie wiem jak to napisać, proszę o pomoc.
private static Timestamp getCurrentDate() {
java.util.Date today = new java.util.Date();
return new java.sql.Timestamp(today.getTime());
}
//Dodaje historiê dla ostatniego logowania
//id_user jest to wartoœæ identyfikuj¹ca danego u¿ytkownika
public void add_history(int id_user) throws SQLException{
try{
int id_max = last_id("login_history");
id_max += 1;
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO login_history(ID, id_user, date) values(?,?,?)");
pStmt.setInt(1, id_max);
pStmt.setInt(2, id_user);
pStmt.setTimestamp(3, getCurrentDate());
pStmt.executeUpdate();
pStmt.close();
}catch (SQLException E){
System.out.println(E);
}
}
//Zwraca ostatnie ID w tablicy
public int last_id(String table) throws SQLException {
int id_max = 0;
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT MAX(ID) AS maxID FROM " + table);
if (rs.next()){
id_max = rs.getInt("maxID");
}
rs.close();
stmt.close();
return id_max;
}