2015-08-20 8 views
8

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) 
); 
+0

Puoi pubblicare la descrizione della tabella (DDL) e il messaggio di errore completo? –

+0

Aggiunta la descrizione della tabella, viene fornito un messaggio di errore completo in questione. – Himanshu

risposta

4

The Oracle JDBC documentation dice:

If key columns are not explicitly indicated, then Oracle JDBC drivers cannot identify which columns need to be retrieved. When a column name or column index array is used, Oracle JDBC drivers can identify which columns contain auto-generated keys that you want to retrieve. However, when the Statement.RETURN_GENERATED_KEYS integer flag is used, Oracle JDBC drivers cannot identify these columns. When the integer flag is used to indicate that auto-generated keys are to be returned, the ROWID pseudo column is returned as key. The ROWID can be then fetched from the ResultSet object and can be used to retrieve other columns.

Non sta specificando le colonne (come mostrato nel loro codice di esempio) in modo che stai recuperando il ROWID; cercando di ottenere che con getInt() sta causando l'errore che vedi. (In realtà vedo Invalid column type: getInt not implemented for class oracle.jdbc.driver.RowidAccessor ERROR CODE: 17004 ma penso che sia dovuto a una versione di driver diversa).

È necessario specificare la colonna che ottiene il valore generato automaticamente. Se si chiama MEETING_ID allora faresti:

String returnCols[] = { "MEETING_ID" }; 
    preparedStatement = dbConnection.prepareStatement(insertTableSQL, returnCols); 

... passando l'array di colonne - uno solo in questo caso - piuttosto che la bandiera RETURN_GENERATED_KEYS.

Il rs.getInt(1) recupererà quindi quel valore numerico.

+0

Grazie, ha fatto il mio lavoro. – Himanshu

Problemi correlati