Sto cercando di ottenere l'ultimo ID riga inserito durante l'inserimento utilizzando istruzioni preparate JDBC. Ho una colonna di chiave primaria di incremento automatico come colonna di identità nella tabella. Il mio codice è qui sotto:Perché è stata richiesta una conversione non valida CODICE DI ERRORE: 17132?
public static String insertMeetingToDB(String organizer,String subject,String location,String start_date_time,String end_date_time,String description) throws Exception {
Connection dbConnection = null;
PreparedStatement preparedStatement = null;
Integer last_inserted_id=0;
String insertTableSQL = "INSERT INTO MEETINGS"
+ "(ORGANIZER_EMAIL, SUBJECT, MEETING_LOCATION, START_DATE_TIME, END_DATE_TIME, MEETING_DESCRIPTION) VALUES"
+ "(?,?,?,?,?,?)";
SimpleDateFormat from = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
from.setTimeZone(TimeZone.getTimeZone("IST")); //--CONVERTING DATE/TIME TO INDIAN STANDARD TIME
SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
Date input_start_date_val = from.parse(start_date_time);
Date input_end_date_val = from.parse(end_date_time);
String input_start_date = datetimeFormat.format(input_start_date_val);
String input_end_date = datetimeFormat.format(input_end_date_val);
try {
dbConnection = getConnection();
//--INSERTING MEETING DETAILS
preparedStatement = dbConnection.prepareStatement(insertTableSQL, preparedStatement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, organizer);
preparedStatement.setString(2, subject);
preparedStatement.setString(3, location);
preparedStatement.setTimestamp(4, java.sql.Timestamp.valueOf(input_start_date));
preparedStatement.setTimestamp(5, java.sql.Timestamp.valueOf(input_end_date));
preparedStatement.setString(6, description);
// execute insert SQL stetement
preparedStatement.executeUpdate();
ResultSet rs = preparedStatement.getGeneratedKeys();
if(rs.next())
{
last_inserted_id = rs.getInt(1);
}
return last_inserted_id.toString();
} catch (SQLException e) {
return e.getMessage()+" ERROR CODE: "+e.getErrorCode();
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
dbConnection = null;
}
}
}
Se elimino questa linea, allora non ho ottenuto questo errore:
last_inserted_id = rs.getInt(1);
Ma dopo googleing sembra essere ok con questo codice, che dovrebbe tornare me ieri ID riga inserita.
Tabella Descrizione:
CREATE TABLE MEETINGS (
MEETING_ID NUMBER GENERATED ALWAYS AS IDENTITY,
ORGANIZER_EMAIL VARCHAR2(100),
SUBJECT VARCHAR2(250),
START_DATE_TIME TIMESTAMP,
END_DATE_TIME TIMESTAMP,
ATTENDEES_LIST_CONFIDENTIAL CHAR(1),
ATTENDEES_CONF_CONFIDENTIAL CHAR(1),
ATTENDEES_COUNT_INTERNAL NUMBER(11),
ATTENDEES_COUNT_EXTERNAL NUMBER(11),
CONFIRMED_COUNT_INTERNAL NUMBER(11),
CONFIRMED_COUNT_EXTERNAL NUMBER(11),
PREVIOUS_MEETING_ID NUMBER(20),
APPOINTMENT_SOURCE CHAR(1),
MEETING_LOCATION VARCHAR(100),
LATITUDE FLOAT(10),
LONGITUDE FLOAT(10),
MEETING_DESCRIPTION VARCHAR2(1000),
PRIMARY KEY(MEETING_ID)
);
Puoi pubblicare la descrizione della tabella (DDL) e il messaggio di errore completo? –
Aggiunta la descrizione della tabella, viene fornito un messaggio di errore completo in questione. – Himanshu