Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123

    [Java] JDBC, problemi con la lettura di 2 oggetti ResultSet

    Ciao a tutti,

    Devo leggere da DB un codice in una tabella, questo codice è diverso per ogni record (ogni record è composto da una data, e da questo codice).
    In pratica devo ottenere tutti i codici che hanno quella certa data.. Il codice in questione, è in comune ad un altra tabella, quindi una volta ottenuta mi basterebbe cercare lo stesso codice..

    ..l'ho scritto così, ma ovviamente non va

    codice:
    rs = stm.executeQuery("SELECT CodFiscale FROM Appuntamenti WHERE Appuntamenti.DataAppuntamento = '"+data+"'");
    
    while(rs.next()) {
      codiceFiscale = rs.getString("CodFiscale");
    		
      rs1 = stm.executeQuery("SELECT nome,cognome,DataNascita FROM Pazienti WHERE codice = '"+codiceFiscale+"'");
    		
        while(rs1.next()) {
          String nome = rs1.getString("nome");
          String cognome = rs1.getString("cognome");
          String nascita = rs1.getString("DataNascita");
    		  
          nomi.add(nome+" "+cognome+" "+nascita);
        }
    }
    nomi è un ArrayList che memorizza stringhe..

    Come risolvo il problema di lettura?

    PS: Qualora dovesse servire, l'errore che ottengo è
    codice:
    java.sql.SQLException: Operation not allowed after ResultSet closed
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
            at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
            at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:795)
            at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7146)
            at Visite.main(Visite.java:132)
    Grazie!

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284

    Re: [Java] JDBC, problemi con la lettura di 2 oggetti ResultSet

    Originariamente inviato da Patrick Jane
    Come risolvo il problema di lettura?

    codice:
    java.sql.SQLException: Operation not allowed after ResultSet closed
    La documentazione javadoc di java.sql.Statement dice proprio all'inizio:

    ---
    By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.
    ---

    Cosa ti fa pensare?
    Che tu hai usato un unico Statement (quel 'stm') per creare 2 ResultSet che usi in modo "intervallato".
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    La documentazione javadoc di java.sql.Statement dice proprio all'inizio:

    ---
    By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.
    ---

    Cosa ti fa pensare?
    Che tu hai usato un unico Statement (quel 'stm') per creare 2 ResultSet che usi in modo "intervallato".

    Questo significa che per risolvere il mio problema è sufficiente creare un nuovo oggetto Statement (uguale al precedente), e quindi assegnare a rs1 questo?

    Grazie!!

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Originariamente inviato da Patrick Jane
    Questo significa che per risolvere il mio problema è sufficiente creare un nuovo oggetto Statement (uguale al precedente), e quindi assegnare a rs1 questo?
    Sì, 2 oggetti Statement distinti.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  5. #5
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123
    Originariamente inviato da andbin
    Sì, 2 oggetti Statement distinti.
    Fatto, ed ovviamente funziona! :P

    Grazie come sempre!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.