This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2017/11/27

db error to login in electronic role

divinity divinity

2017/11/27

#
hello there this is my current school project assignment. i have made a db to login but am stuck. I have three class. can someone tell me why am stuck and how to fix it. here is my appMain where the program run from
the appMain 

package ElectronicRole;

/**
 *
 * @author luanataylor
 */
public class AppMain {
    
    
      public static void main(String args[]) {
        /* Set the Nimbus look and feel */
  
             Login logscrn = new Login();
             logscrn.setVisible(true);
             
            while(!logscrn.isLoginValid()){
                
                //do nothing
            }
            if(logscrn.getUserType().equals("STUDENT")){
                StudentHomePage shp = new StudentHomePage();
                shp.setVisible(true);
            }
            else if( logscrn.getUserType().equals("LECTURER")){
             LecturerHomePage lhp = new LecturerHomePage();
             lhp.setVisible(true);
      }
            logscrn.setVisible(false);            
            
   }
    
      }
here is the login, the interface
package ElectronicRole;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;


public class Login extends javax.swing.JFrame implements ActionListener {
     
    private boolean isLoginValid;
            
    public Login() {
       
        initComponents();
        this.isLoginValid = false;
         
     
    }
    public String getUserType(){// this method is being used to check the usertype, whether it is either the student or lecturer
        
        //this codes here is to check to see if it is the lecturer or the student
        
        int i = jBoxselect.getSelectedIndex();
        String UserType ="";
        if(i == 0){ // this is checking to see which user will be login 
            
            UserType = "STUDENT";
        }
        else
            if(i == 1){
               
                UserType =  "LECTURER";
            }
          return UserType;
    }
    public boolean validateLogin(){
      //this checking to see if the userid, password and usertype is valid or not
        
        Model model = new Model();
        HashMap <String, String > login = new HashMap<String, String>();   // values are pass to the ash map
        login.put("userid",txtuserid.getText());
        login.put("password", pass.getText());
        login.put("usertype", this.getUserType());
        return  model.isLoginValid(login);
        
    }
    // check to see if the login is valid or not and if it is, the user can login
    public boolean isLoginValid(){
        return this.isLoginValid;
    }
                          
    @Override
  public void actionPerformed(ActionEvent ae){
         if("Ok".equals(ae.getActionCommand())){
             Object src = ae.getSource();
             if(src.equals("Ok")){
                                            
             }
         }       
   }
this is the dbcon class
package ElectronicRole;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author luanataylor
 */
public class DBCon {
    
    public DBCon(){
        
    }
    
    public  Connection connect(){
        Connection conn =null;
         try{
            
            //connecting to the db
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("dbc.sql://localhost:3306/electronicrole", "dbuser", "dbpass");
          
        }
        catch(Exception e){
            
            System.out.println(e);
        }
         return conn;
    }
    public Statement statement(){
         Statement stmt=null;
        try {
            stmt = this.connect().createStatement();
        } catch (SQLException ex) {
            
        }
          
          return stmt;
    }
    
        public ResultSet executeQuery(String querystr) {
        
            ResultSet rs =null;
            try{
                   
                       rs = this.statement().executeQuery(querystr);
                      
            }
            catch(SQLException e){
                System.out.println(e);
                
            }

            return rs;
        }
            
and this is my model class
public class Model extends DBCon {
    
    ResultSet rs;
    
    
    public boolean isLoginValid(HashMap <String, String> login) {
        
        boolean valid = false;
         String querystr, table;
         
         //check for the usertype if it is equal to student when check for the login
        if( login.get("usertype").equals("STUDENT")){
           
            table = "students";
        }
        else{
            table  =  "lecturers";// it is not the students, it will be the lecturer
        }
        querystr = String.format("SELECT COUNT(StudentID) AS count FROM %s WHERE StudentID=\"%s\" AND Password=\"%s\"", table, login.get("userid"),login.get("password"));
        System.out.println(querystr); 
        try {
         
             this.rs =  this.executeQuery(querystr);
             rs.next();
             if(rs.getInt("count") > 0){
             valid = true;
        }
        }
             catch(SQLException e){
                     
      }
        return valid;
  }
             
  }
    
divinity divinity

2017/11/27

#
these are the errors that is where I am stuck: at ElectronicRole.DBCon.statement(DBCon.java:42) at ElectronicRole.DBCon.executeQuery(DBCon.java:57) at ElectronicRole.Model.isLoginValid(Model.java:37) at ElectronicRole.Login.validateLogin(Login.java:52) at ElectronicRole.Login.btnOKActionPerformed(Login.java:197) at ElectronicRole.Login.access$100(Login.java:16) at ElectronicRole.Login$2.actionPerformed(Login.java:105) the errors are at line 42 which is at line 130, line 57 which is line 143 line 37 which is at line 177 line 197 which is this line below
  private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {                                      
        this.isLoginValid = this.validateLogin();
divinity divinity

2017/11/27

#
is there anyway it can be fix, and tell me what is wrong here.
danpost danpost

2017/11/27

#
divinity wrote...
is there anyway it can be fix, and tell me what is wrong here.
See what happens when you put the following at line 132:
System.out.println(ex);
divinity divinity

2017/12/3

#
hey danpost I have since fixed that problems already but thanks all the same
You need to login to post a reply.