Jump to content
JamesBong

Ajutor java

Recommended Posts

Posted

Salut. Am o problema si nu reusesc sa ii dau de cap... incerc sa invat java facand diverse programe si acum incerc sa fac un ecommerce app in consola cu database. Incerc sa fac un login system in care userii sa fie stocati in o baza de date si as vrea cumva sa opresc query cand gaseste userul. Va multumesc

 

  public static void login(){
    
      System.out.println("Enter username: ");
      String username = input.next();
      System.out.println("Enter password: ");
      String password =  input.next();
      int error = 0;
      
      try{
        String SQL = "SELECT username, password FROM ADMINISTRATOR.users";
        
        ResultSet rs = stmt.executeQuery( SQL );
        
        while(rs.next()){
           if(rs.getString("username").contains(username) && rs.getString("password").equals(password)){
               System.out.println("Login successfull");               
               
               error=0;
               mainMenu();

           } else {
               error =1;        
           }
        }
        if(error == 1){
              System.out.println("Incorrect details. Returing to menu.");
              welcomeMenu(); 
              
        }           
         }catch (SQLException err) {
            System.out.println(err.getMessage() );
            }
}

 

Posted (edited)

Ai gresit query-ul baiatul meu.

Incearca sa-i dai select username, password from administrator.users where username='userultau' and password='parolata';

 

Sau, e de preferat sa folosesti Prepared Statement, pt ca e mai frumix, mai ales cand ai nevoie de parametri

Ar trebui sa scrii ceva de genul

 

PreparedStatement stmt="SELECT username, password FROM ADMINISTRATOR.users where username=? and password=?;";

 

stmt.setString(1,VARIABILA_USERNAME);

stmt.setString(2,VARIABILA_PAROLA);

rs=stmt.execute... si de aici te descurci

 

 

Btw, avand in vedere ca ai un db, ar trb sa ai coloana de username UNICA,

Deci daca tu dai select, ar trebui sa primesti MAXIM UN REZULTAT, deci tu poti doar sa verifici doar 
 

if(rs.next()){

inseamna ca ti-a returnat un rezultat, si avant in vedere ca i-ai specificat din prima username-ul si parola, ar trebui sa fie ok;

deci iti bagi aici codul tau drumi;

}

 

BTW, ai grija la contains.

Incearca sa folosesti String.equals(AltString);

 

Pt ca daca eu am ca si user si ca si parola, un substring din alt user si alta parola, m-as putea log.

 

 

BTW 2;

}catch (SQLException err) {
            System.out.println(err.getMessage() );
            }

 

da-i cu err.printStackTrace();

in loc de sysout err.getmessage

Edited by theandruala
  • Thanks 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...