0

I am trying to check username and pwd from a MySQL DB, the method CheckFromDB() calls methods from AccessoDB class, in order to establish a connection with the DB.

I get that the problem shows in the moment I crate the statement in the AccessoDB.selectFixedQuery() method.

Here is the code I used:

   public boolean checkFromDB(){
    AccessoDB accesso = new AccessoDB();
    accesso.openConnectionToDB();
    ResultSet rs = accesso.selectFixedQuery("SELECT * FROM user WHERE nome = "+this.username+" && pwd = "+this.password);
    String uname_db = "", pwd_db = "";
    try {
        while (rs.next())
        {
            uname_db=rs.getString("nome");
            System.out.println(uname_db);
            pwd_db = rs.getString("pwd");
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    accesso.closeConnectionToDB();
    boolean res = false;
    res = uname_db.equals(this.username)&& pwd_db.equals(this.password);
    return res;
}

Method selectFixedQuery in class AccessoDB:

    public ResultSet selectFixedQuery(String p_sqlQuery) {

    // istantiate a resultSet
    ResultSet rs = null;
    Statement st = null;
    try {

        // create statement associated to the query
        st = dbConnection.createStatement();

        // esegue la query
        rs = st.executeQuery(p_sqlQuery); 

    } catch (SQLException e) {
        Logger.println("ANALISI DELL'ERRORE");
        Logger.println(e.getMessage());
        Logger.println("FINE ANALISI DELL'ERRORE");
    }

    // ritorna il risultato
    return rs;
} // end method

    public void openConnectionToDB() {

    try {
        // allocazione del driver (antico)
        Class.forName(dbDriverName).newInstance();

        // creazione della connessione
        dbConnection = DriverManager.getConnection(connectionString, 
                                                    dbUsername, 
                                                    dbPassword);                    
    }
    // serve solo se si usa il Driver
    catch (ClassNotFoundException cnfe) {
        Logger.println("Driver not found!");
        Logger.println(cnfe.getMessage());
    } 
    catch (SQLException sqle) {
        Logger.println("Conessione al DB non riuscita!");
        Logger.println(sqle.getMessage());
    }
    catch (Exception e) {
        // gestire errore insolito
        Logger.println(e.getMessage());
    }
} // end method
Argentina
  • 1,071
  • 5
  • 16
  • 30

1 Answers1

0

openConnectionToDB() should return dbConnection object, right now your connection object is lost in the method calls. Store it and pass it back to your selectFixedQuery() method

KP.
  • 393
  • 1
  • 12