I am trying to fix this code to update my log table in a mysql database 5.6 Do you know how to retrieve the primary key from the database and assign it to my id variable (Log)
Thanks
Log model without getters and setters
private int id;
private Timestamp date;
private String userName;
private String event;
private String message;
private String audioPath;
Database Table
CREATE TABLE `log` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`date` datetime(2) NOT NULL DEFAULT '0000-00-00 00:00:00.00',
`user_name` char(60) NOT NULL,
`event` varchar(50) NOT NULL,
`message` text NOT NULL,
`audio_path` varchar(250) NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
Add Entry Log
@Override
public int addEntry(Log log) throws SQLException {
Connection conn = Database.getInstance().getConnection();
PreparedStatement p = conn.prepareStatement(
"insert into log ( id, date, user_name, event, message, audio_path) "
+ "values (?,?,?,?,?,?)",
PreparedStatement.RETURN_GENERATED_KEYS);
p.setInt(1, p.getGeneratedKeys()); //error
p.setTimestamp(2, log.getDate());
p.setString(3, log.getUserName());
p.setString(4, log.getEvent());
p.setString(5, log.getMessage());
p.setString(6, log.getAudioPath());
int updated = p.executeUpdate();
p.close();
return updated;
}