webApp1/src/main/java/usuario/usuario.java
public Boolean is_user_in_db(String user, String passwd){
        Boolean result = true;
        // define query
        String query = "select count(*) from users where username=? and password=?";
        Connection c = null;
        try{
            // create a database connection
            c = DriverManager.getConnection("jdbc:derby://localhost:1527/isdcm_lab_db;user=isdcm_lab_db;password=isdcm_lab_db");
            
            // prepare statement
            PreparedStatement statement;
            statement = c.prepareStatement(query);
            statement.setString(1, user);
            statement.setString(2, passwd);
            
            // execute query
            ResultSet r = statement.executeQuery();
        if (r.next())
            {
                if (r.getInt(1) == 0)//if the number of user == 0
                    result = false;   //then user does not exist yet
            }    
        }catch (SQLException e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
        } finally {
            try {
                if (c != null) 
                    c.close();                
            } catch (SQLException e) {
                System.out.println(Arrays.toString(e.getStackTrace()));
            }
        }
        return result;
    }
// function called by the servlet to know if a user is already stored in the database or not
function called by the servlet to know if a user is already stored in the database or not
function called by the servlet to know if a user is already stored in the database or not
public Boolean is_user_in_db(String user, String passwd){
        Boolean result = true;
        String query = "select count(*) from users where username=? and password=?";
        Connection c = null;
        try{
            c = DriverManager.getConnection("jdbc:derby://localhost:1527/isdcm_lab_db;user=isdcm_lab_db;password=isdcm_lab_db");
            PreparedStatement statement;
            statement = c.prepareStatement(query);
            statement.setString(1, user);
            statement.setString(2, passwd);
            ResultSet r = statement.executeQuery();
        if (r.next())
            {
                if (r.getInt(1) == 0)
                    result = false;   
            }    
        }catch (SQLException e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
        } finally {
            try {
                if (c != null) 
                    c.close();                
            } catch (SQLException e) {
                System.out.println(Arrays.toString(e.getStackTrace()));
            }
        }
        return result;
    }
public Boolean is_user_in_db ( String user , String passwd ) { Boolean result = true ; String query = "select count(*) from users where username=? and password=?" ; Connection c = null ; try { c = DriverManager . getConnection ( "jdbc:derby://localhost:1527/isdcm_lab_db;user=isdcm_lab_db;password=isdcm_lab_db" ) ; PreparedStatement statement ; statement = c . prepareStatement ( query ) ; statement . setString ( 1 , user ) ; statement . setString ( 2 , passwd ) ; ResultSet r = statement . executeQuery ( ) ; if ( r . next ( ) ) { if ( r . getInt ( 1 ) == 0 ) result = false ; } } catch ( SQLException e ) { System . out . println ( Arrays . toString ( e . getStackTrace ( ) ) ) ; } finally { try { if ( c != null ) c . close ( ) ; } catch ( SQLException e ) { System . out . println ( Arrays . toString ( e . getStackTrace ( ) ) ) ; } } return result ; }
function called by the servlet to know if a user is already stored in the database or not
function called by the servlet to know if a user is already stored in the database or not
// define query // create a database connection // prepare statement // execute query //if the number of user == 0 //then user does not exist yet