Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    169

    [JAVA] oggetto resultset

    ciao a tutti,
    dunque, mi sono costruito 1 classe DBconn, che contiene 1 metodo
    getResultset() che dovrebbe restituire il risultato di una query:
    --------
    public ResultSet getResultset(String strSQL) {
    Statement stmt = null;
    Connection tmpConn = null;
    ResultSet rs = null;
    errConn = "";
    try {
    tmpConn = DriverManager.getConnection("jdbc:mysql://localhost/dbbiblio?user=root&password=");
    } catch (SQLException ex) {
    errConn = ex.getMessage();
    }

    if ("".equals(errConn)){
    try {
    stmt = tmpConn.createStatement();
    rs = stmt.executeQuery(strSQL);
    }
    catch (SQLException sqlEx) { errConn = sqlEx.getMessage(); }
    finally {
    if (rs != null) {
    try {

    rs.close();

    } catch (SQLException sqlEx) { errConn = sqlEx.getMessage(); }
    }
    try {
    tmpConn.close();
    tmpConn = null;
    errConn = "";
    } catch (SQLException ex) {
    errConn = ex.getMessage();
    }
    if (stmt != null) {
    try {
    stmt.close();
    } catch (SQLException sqlEx) { errConn = sqlEx.getMessage(); }

    stmt = null;
    }
    }
    }
    return rs;
    }

    --------

    poi nella jsp:
    -----
    DBconn currConn = new DBconn("dbbiblio");
    String strUser = request.getParameter("txtUser");
    String strPWD = request.getParameter("txtPWD");
    currConn.openConn (strUser,strPWD);

    ResultSet tblUser = null;
    String strSQL;
    String strRes;
    strSQL = "SELECT * FROM TBuser WHERE CDuser='"+strUser+"'";
    strSQL = strSQL+" AND CDpwd='"+strPWD+"'";
    tblUser = currConn.getResultset(strSQL);
    String strCDuser = "";
    if (tblUser==null){
    strCDuser = "vuoto";}
    else
    { if(tblUser.next()){
    strCDuser = tblUser.getString("CDuser");
    }
    ----

    mi viene segnalato un errore di operazione non valida dopo la chiusura del recordset.
    credo sia un errore logico, ma essendo alle prime armi con java ho qualche difficoltà a trovare gli errori, se poi avete anche 1 classe seria o esempi di codice che utilizza resultset vi ringrazio molto...

  2. #2
    Utente di HTML.it L'avatar di Angelo1974
    Registrato dal
    Feb 2003
    Messaggi
    1,107
    Semplice non devi fare rs.close();
    Se vuoi trovare l'arcobaleno, devi sopportare la pioggia

  3. #3
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    169
    ma se faccio cosi non resta la connessione appesa?

  4. #4
    Utente di HTML.it L'avatar di Angelo1974
    Registrato dal
    Feb 2003
    Messaggi
    1,107
    Be' quando non ti sderve più la chiudi; per esempio potresti fare una classe del tipo:
    Codice PHP:
    class DbInteration {
      
      private 
    Connection connessione;
      private 
    Statement statement;
      private 
    ResultSet rs;
      public 
    DbInteration() {
      }
      
      private 
    boolean getConnection(){

        try{
     
          Class.
    forName"nome classe per connessione db" );
          
    connessione DriverManager.getConnection"url database" );
          return 
    true;
        }catch( 
    ClassNotFoundException cnf ){
          
          return 
    false;
        }
        catch( 
    SQLException sql ){
          
          return 
    false;
        }
      }
      private 
    boolean getStatement(){
     
        if( 
    getConnection() ){
     
          try{
            
    statement this.connessione.createStatement();
            return 
    true;
          }catch( 
    SQLException sql ){
     
            return 
    false;
          }
        }else{
          return 
    false;
        }
      }
      
      public 
    ResultSet getResultSet(){
     
        if( 
    this.getStatement() ){

          try{

            
    rs statement.executeQuery"tua select" );
            return 
    rs;
          }catch( 
    SQLException sql ){
            return 
    null;
          }
        }else{
     
          return 
    null;
        }
      }

      public 
    void closeConn() throws SQLException{

        if( 
    this.rs != null ){
          
          
    rs.close();
          
    rs null;
        }if( 
    this.statement != null ){

          
    statement.close();
          
    statement null;

        }if( 
    connessione != null ){

          
    connessione.close();
          
    connessione null;
        }
      }

    Se vuoi trovare l'arcobaleno, devi sopportare la pioggia

  5. #5
    Utente di HTML.it
    Registrato dal
    Nov 2002
    Messaggi
    169
    ok, a questo punto la funzione che mi restituisce il resultset funziona.
    ora la richiamo in questo modo per la verifica dell'utente e della password in una mia tabella tbuser:

    -----

    public String CtrlConnUser(String strUser,String strPWD) {

    Connection tmpConn;
    ResultSet tblUser;
    String strSQL;
    String tmpUser = "";
    String tmpPWD = "";
    String strRes = "";
    strSQL = "SELECT * FROM TBuser WHERE CDuser='"+strUser+"'";
    strSQL = strSQL+" AND CDpwd='"+strPWD+"'";

    tblUser = this.getResultset(strSQL);
    try {


    if (tblUser.next()){
    try {
    tmpUser = tblUser.getString("CDuser");
    } catch (SQLException ex) {
    tmpUser = ex.getMessage();
    }


    try {
    tmpPWD = tblUser.getString("CDpwd");
    } catch (SQLException ex) {
    tmpPWD = "pippo";
    }
    }
    } catch (SQLException ex) {
    tmpUser = "ciccio";
    }
    return tmpUser+","+tmpPWD;

    ----

    anche qui, dice che l'operazione di tblUser.next() non è possibile con il resultset chiuso...
    però la close nella funzione non c'è, e non ho chiuso la connessione in nessun modo...
    grazie mille!! (abbiate pietà di un giovane volenteroso e autodidatta!)

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 © 2024 vBulletin Solutions, Inc. All rights reserved.