I am having a proplem checking the stu_uid for a specified stu_password.

I am not really sure what error is. When I build the db for the first time and login as a

student
it says that unknown user name but it is all in the table and passed to the method good
is the password char[] the correct way to pass as a param?

run:
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in bCreatedTables:
created table: createAdminTable
created table: createPayeeTable
created table: createStudentTable
created table: createInstructorTable
created table: createBookingTable
created table: createInstrAvailableTable
created table: createInstrumentTable
created table: createLocationTable
created table: createPaymentTable
created table: createPayrollTable
created table: createUserTable
created table: createUserGroupMappingTable
created table: createUserGroupTable
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
db and tables created: 
inserted data int: insertAdminTable
inserted data int: insertPayeeTable
inserted data int: insertStudentTable
inserted data int: insertInstructorTable
inserted data int: insertPaymentTable
inserted data int: insertUserTable
inserted data int: insertUserGroupTable
inserted data int: insertUserGroupMappingTable
CONECTION IS GOOD FOR A TEST
test string query: SELECT * FROM student

number of columns: 15

, STU_UID, STU_PASSWORD, STU_LNAME, STU_MNAME, STU_FNAME, STU_GENDER, STU_AGE, 

STU_START_DATE, STU_END_DATE, STU_ADDRESS, STU_STATE, STU_ZIP, STU_AREA_CODE, STU_PHONE, 

PAYEE_UID
, sj, s, null, null, Johnny, null, null, null, null, null, null, null, null, null, null
, ss, s, null, null, Suzie, null, null, null, null, null, null, null, null, null, null

in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true
shutdown database 
disconnected after  building the database java.sql.SQLNonTransientConnectionException: 

Database 'schoolofdb' shutdown.
disconnected after building the database
input from splash: user: sj
in LoginInfoBean
LoginInfoBean password: [C@1f8bd0d
LoginInfoBean user: sj
LoginInfo: user: sj
LoginInfo profile: stu_
all models (Student.java extend LoginInfo.javaall beans extend models
in getConnection of DerbyDAOFactory
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true
In ConnectStudent.select(newUser,newPassword)
param passed: select(): newUser: sj newPassword: [C@1f8bd0d
studentUserName: SELECT stu_uid FROM student WHERE stu_password=?

UnknownUserNameException: Unknown user name

code:

public StudentBean select(String newUser, char[] newPassword) throws 

UnknownUserNameException, IncorrectPasswordException, LoginException, SQLException, 

FileNotFoundException, IOException, ProfileException {
        DerbyDAOFactory ddf = new DerbyDAOFactory();
        conn = ddf.getConnection();
        ps=null;
        System.out.println("");
        System.out.println("In ConnectStudent.select(newUser,newPassword)");
        System.out.println("param passed: select(): "
                + "newUser: " + newUser + " newPassword: " + newPassword);
        System.out.println("");
        System.out.println("studentUserName"+ModelUtils.getXMLResource("studentUserName"));
        System.out.println("");
        //[b]
        ps = conn.prepareStatement(ModelUtils.getXMLResource("studentUserName"));

        ps.setString(1, newPassword.toString());
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            String user = rs.getString(1);
            System.out.println("user from db: " + user);

        }
         //[/b]
        try {
            if (!newUser.equals(user)) {
                System.out.println("UnknownUserNameException: Unknown user name");
                throw new UnknownUserNameException();                
            }
        } catch (UnknownUserNameException ex) {
            ps.close();
            rs.close();
            String x = ex.getMessage();
            ViewUtils vu = new ViewUtils();
            vu.addExceptionMessage(x);           

        }
        ps = conn.prepareStatement(ModelUtils.getXMLResource("studentPassword"));
        System.out.println("studentPassword: " + 

ModelUtils.getXMLResource("studentPassword"));
        ps.setString(1, newUser);
        rs = ps.executeQuery();
        if (rs.next()) {
            String password = rs.getString(1);
        }
        try {
            if (!password.equals(newPassword)) {
                throw new IncorrectPasswordException();
            }
            ps = (PreparedStatement) conn.prepareStatement(
                    ModelUtils.getXMLResource("SelectStudent"));
            ps.setString(1, newUser);
            System.out.println("SelectStudent: "
                    + "\n" + ModelUtils.getXMLResource("SelectStudent"));
            rs = ps.executeQuery();
            studentUser.setUser(rs.getString(1));
            studentUser.setPassword(rs.getString(2));
            studentUser.setlName(rs.getString(3));
            studentUser.setmName(rs.getString(4));
            studentUser.setfName(rs.getString(5));
            studentUser.setGender(rs.getString(6));
            studentUser.setStartDate(rs.getDate(7));
            studentUser.setEndDate(rs.getDate(8));
            studentUser.setAge(rs.getInt(9));
            studentUser.setAddress(rs.getString(10));
            studentUser.setState(rs.getString(11));
            studentUser.setZip(rs.getInt(12));
            studentUser.setAreaCode(rs.getInt(13));
            studentUser.setPhone(rs.getInt(14));
            studentUser.setPayRate(rs.getDouble(15));
            ps.close();
            rs.close();
        } catch (IncorrectPasswordException ex) {
            ps.close();
            rs.close();
            String x = ex.getMessage();
            ViewUtils vu = new ViewUtils();
            vu.addExceptionMessage(x);
        }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Why is your password field a character array? Assuming that the password field in the database table is of type VARCHAR, you need to pass in a password "string" and not the "string representation" of the password character array.

Thanks
I changed the password data type throughout. i also changed the "studentUserName" query back to:
changing the WHERE password=? to WHERE stu_uid=? Select stu_uid FROM student WHERE stu_uid=? I hoped this would be logical. Contrarily, the prepared statement is showing a NullPointer Exception.

Is there a way to further test this statement. all other aspects of the first post are the same.

error at

//[b]
ps=conn.prepareStatement(ModelUtils.getXMLResource("studentUserName"));
//[/b]

Though I used trim() on the newUser param compared to the user data from the db they
are not the same though they look the same.
the int t=newUser.compareTo(user); returns 0

do to error int len2=anotherString.count;

Edited 6 Years Ago by ceyesuma: x

Is there a reason that the table shows the proper strings.The method is passed the correct strings.The newUser string is compared to variable user from the db that shows as "sj".When compared in the method it is null;

run:
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in bCreatedTables:
created table: createAdminTable
created table: createPayeeTable
created table: createStudentTable
created table: createInstructorTable
created table: createBookingTable
created table: createInstrAvailableTable
created table: createInstrumentTable
created table: createLocationTable
created table: createPaymentTable
created table: createPayrollTable
created table: createUserTable
created table: createUserGroupMappingTable
created table: createUserGroupTable
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
db and tables created: 
inserted data int: insertAdminTable
inserted data int: insertPayeeTable
inserted data int: insertStudentTable
inserted data int: insertInstructorTable
inserted data int: insertPaymentTable
inserted data int: insertUserTable
inserted data int: insertUserGroupTable
inserted data int: insertUserGroupMappingTable
CONECTION IS GOOD FOR A TEST
test string query: SELECT * FROM student
[b]
number of columns: 15
, STU_UID, STU_PASSWORD, STU_LNAME, STU_MNAME, STU_FNAME, STU_GENDER, STU_AGE, STU_START_DATE, STU_END_DATE, STU_ADDRESS, STU_STATE, STU_ZIP, STU_AREA_CODE, STU_PHONE, PAYEE_UID
, sj, s, null, null, Johnny, null, null, null, null, null, null, null, null, null, null
, ss, s, null, null, Suzie, null, null, null, null, null, null, null, null, null, null
[/b]
in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true
shutdown database 
disconnected after  building the database java.sql.SQLNonTransientConnectionException: Database 'schoolofdb' shutdown.
disconnected after building the database



input from splash: user: sj
in LoginInfoBean
LoginInfoBean password: [C@15e293a
LoginInfoBean user: sj
LoginInfo: user: sj
LoginInfo profile: stu_
all models (Student.java extend LoginInfo.javaall beans extend models
in getConnection of DerbyDAOFactory
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true

In ConnectStudent.select(newUser,newPassword)
param passed: select(): newUser: sj newPassword: [C@15e293a

studentUserName: SELECT stu_uid FROM student WHERE stu_uid=?
[b]
user from db: null
[/b]
newUser param: sj
UnknownUserNameException: Unknown user name
public StudentBean select(String newUser, String newPassword) throws UnknownUserNameException, IncorrectPasswordException, LoginException, SQLException, FileNotFoundException, IOException, ProfileException {

        DerbyDAOFactory ddf = new DerbyDAOFactory();
        conn = ddf.getConnection();
        ps = null;
        System.out.println("");
        System.out.println("In ConnectStudent.select(newUser,newPassword)");
        System.out.println("param passed: select(): "
                + "newUser: " + newUser + " newPassword: " + newPassword);
        System.out.println("");
        System.out.println("studentUserName: " + ModelUtils.getXMLResource("studentUserName"));
        System.out.println("");
        ps=null;
        ps = conn.prepareStatement(ModelUtils.getXMLResource("studentUserName"));
        ps.setString(1, newUser);
        rs=null;
        rs=ps.executeQuery();
        while(rs.next()){
        String user=rs.getString(1).trim();
        }
        //int t = newUser.compareTo(user); [b] //<--------- int len2 = anotherString.count;     [/b]
        //System.out.println("compareTo: " + t);
        System.out.println("user from db: " + user);
        System.out.println("newUser param: " + newUser);
        
        try {
            if (!newUser.equals(user)) {
                System.out.println("UnknownUserNameException: Unknown user name");
                throw new UnknownUserNameException();
            }
        } catch (UnknownUserNameException ex) {
            ps.close();
            rs.close();
            String x = ex.getMessage();
            ViewUtils vu = new ViewUtils();
            vu.addExceptionMessage(x);
        }
        ps = conn.prepareStatement(ModelUtils.getXMLResource("studentPassword"));
        System.out.println("studentPassword: " + ModelUtils.getXMLResource("studentPassword"));
        ps.setString(1, newUser);
        rs = ps.executeQuery();
        while (rs.next()) {
            password = rs.getString(1);
        }
        try {
            if (!password.equals(newPassword)) {
                throw new IncorrectPasswordException();
            }
            ps = (PreparedStatement) conn.prepareStatement(
                    ModelUtils.getXMLResource("SelectStudent"));
            ps.setString(1, newUser);
            System.out.println("SelectStudent: "
                    + "\n" + ModelUtils.getXMLResource("SelectStudent"));
            rs = ps.executeQuery();
            while (rs.next()) {
                studentUser.setUser(rs.getString(1));
                studentUser.setPassword(rs.getString(2));
                studentUser.setlName(rs.getString(3));
                studentUser.setmName(rs.getString(4));
                studentUser.setfName(rs.getString(5));
                studentUser.setGender(rs.getString(6));
                studentUser.setStartDate(rs.getDate(7));
                studentUser.setEndDate(rs.getDate(8));
                studentUser.setAge(rs.getInt(9));
                studentUser.setAddress(rs.getString(10));
                studentUser.setState(rs.getString(11));
                studentUser.setZip(rs.getInt(12));
                studentUser.setAreaCode(rs.getInt(13));
                studentUser.setPhone(rs.getInt(14));
                studentUser.setPayRate(rs.getDouble(15));
            }
            ps.close();
            rs.close();
        } catch (IncorrectPasswordException ex) {
            ps.close();
            rs.close();
            String x = ex.getMessage();
            ViewUtils vu = new ViewUtils();
            vu.addExceptionMessage(x);
        }

You do this to compare the users:

while(rs.next()){
  String user=rs.getString(1).trim();
}

if (!newUser.equals(user)) {

}

You put the username in the "user" variables that is defined and initialized in the while loop, but the one you are using for comparing the names is a different user variable. The "user" in which you put the username from the DB is declared inside the while and cannot be seen from outside. It is out of scope.
The ONLY reason the code compiles is because you have probably declared another global "user" variable. That is the one you are using for comparing but you don't give it value.

Hello
user is used in my bean and model so that helped so
I changed the user to uid. I still show the same problem
I am having a problem understanding how to pass the password field. It will never equal

what is in the db.
and
my newUser param and my uid from the db look equal but it is throwing an exception.

I am using my close(conn,ps) and then reconnecting every time I re use ps is this wrong?
thanks

CREATED NEW INSTANCE OF ConnectAdminDAO
newUser param: ag
newPassword param: [C@71dc3d
in getConnection of DerbyDAOFactory
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true
uid:rs.getString(1): ag
newUser param: ag

adminUserName: SELECT admin_uid FROM admin WHERE admin_uid=?

adminPassword: SELECT admin_password FROM admin WHERE admin_uid=?
selectAdmin: 
SELECT
    admin_uid,
    admin_password,
    admin_lname,
    admin_mname,
    admin_fname,
    admin_gender,
    admin_age,
    admin_start_date,
    admin_end_date,
    admin_address,
    admin_state,
    admin_zip,
    admin_area_code,
    admin_phone,
    admin_pay_rate
    FROM admin
    WHERE admin_uid=? 
CONECTION IS GOOD FOR A TEST
query test: selectAllAdmin

number of columns: 15
, ADMIN_UID, ADMIN_PASSWORD, ADMIN_LNAME, ADMIN_MNAME, ADMIN_FNAME, ADMIN_GENDER, 

ADMIN_AGE, ADMIN_START_DATE, ADMIN_END_DATE, ADMIN_ADDRESS, ADMIN_STATE, ADMIN_ZIP, 

ADMIN_AREA_CODE, ADMIN_PHONE, ADMIN_PAY_RATE
, ag, a, null, null, Garth, null, null, null, null, null, null, null, null, null, null
, aw, a, null, null, Wayne, null, null, null, null, null, null, null, null, null, null
Error: UnKnown User Name
public void ConnectStudent() throws UnknownUserNameException, SQLException, 

IncorrectPasswordException, LoginException, FileNotFoundException, IOException {
    }

    public StudentBean select(String newUser, String newPassword) throws 

UnknownUserNameException, IncorrectPasswordException, LoginException, SQLException, 

FileNotFoundException, IOException, ProfileException {
        DerbyDAOFactory ddf = new DerbyDAOFactory();
        conn = ddf.getConnection();
        testDB();
        ps = null;
        System.out.println("");
        System.out.println("In ConnectStudent.select(newUser,newPassword)");
        System.out.println("param passed: select(): "
                + "newUser: " + newUser + " newPassword: " + newPassword);
        System.out.println("");
        System.out.println("studentUserName: " + 

ModelUtils.getXMLResource("studentUserName"));
        System.out.println("");
        ps=null;
        ps = conn.prepareStatement(ModelUtils.getXMLResource("studentUserName"));
        ps.setString(1, newUser);
        rs=null;
        rs=ps.executeQuery();
        while(rs.next()){
        String uid=rs.getString(1).trim();
        }
        //int t = newUser.compareTo(uid); [b] //<----error: int len2 = anotherString.count; 

    [/b]
        //System.out.println("compareTo: " + t);
        System.out.println("uid from db: " + uid);
        System.out.println("newUser param: " + newUser);
        
        try {
            if (!newUser.equals(uid)) {
                System.out.println("UnknownUserNameException: Unknown uid name");
                throw new UnknownUserNameException();
            }
        } catch (UnknownUserNameException ex) {
            ps.close();
            rs.close();
            String x = ex.getMessage();
            ViewUtils vu = new ViewUtils();
            vu.addExceptionMessage(x);
        }
        ps = conn.prepareStatement(ModelUtils.getXMLResource("studentPassword"));
        System.out.println("studentPassword: " + 

ModelUtils.getXMLResource("studentPassword"));
        ps.setString(1, newUser);
        rs = ps.executeQuery();
        while (rs.next()) {
            password = rs.getString(1);
        }
        try {
            if (!password.equals(newPassword)) {
                throw new IncorrectPasswordException();
            }
            ps = (PreparedStatement) conn.prepareStatement(
                    ModelUtils.getXMLResource("selectStudent"));
            ps.setString(1, newUser);
            System.out.println("selectStudent: "
                    + "\n" + ModelUtils.getXMLResource("selectStudent"));
            rs = ps.executeQuery();
            while (rs.next()) {
                studentUser.setUser(rs.getString(1));
                studentUser.setPassword(rs.getString(2));
                studentUser.setlName(rs.getString(3));
                studentUser.setmName(rs.getString(4));
                studentUser.setfName(rs.getString(5));
                studentUser.setGender(rs.getString(6));
                studentUser.setStartDate(rs.getDate(7));
                studentUser.setEndDate(rs.getDate(8));
                studentUser.setAge(rs.getInt(9));
                studentUser.setAddress(rs.getString(10));
                studentUser.setState(rs.getString(11));
                studentUser.setZip(rs.getInt(12));
                studentUser.setAreaCode(rs.getInt(13));
                studentUser.setPhone(rs.getInt(14));
                studentUser.setPayRate(rs.getDouble(15));
            }
            ps.close();
            rs.close();
        } catch (IncorrectPasswordException ex) {
            ps.close();
            rs.close();
            String x = ex.getMessage();
            ViewUtils vu = new ViewUtils();
            vu.addExceptionMessage(x);
        }
        return (StudentBean) studentUser;
    }

Is there any way to make the password from the db match the user input password here?

ps = conn.prepareStatement(ModelUtils.getXMLResource("studentPassword"));
        ps.setString(1, user);
        rs = ps.executeQuery();       
        if(rs.next()) {
            PASSWORD = rs.getString(1);
             System.out.println("PASSWORD: " + PASSWORD);
        }
        System.out.println("password: "+password);
        System.out.println("PASSWORD: "+PASSWORD);
        try {
            if (!PASSWORD.equals(password)) {
                throw new IncorrectPasswordException();
            }
            ps = (PreparedStatement) conn.prepareStatement(
                    ModelUtils.getXMLResource("selectStudent"));
            rs = ps.executeQuery();
            while (rs.next()) {
                studentUser.setUser(rs.getString(1));
                studentUser.setPassword(rs.getString(2));
                studentUser.setlName(rs.getString(3));
                studentUser.setmName(rs.getString(4));
                studentUser.setfName(rs.getString(5));
                studentUser.setGender(rs.getString(6));
                studentUser.setStartDate(rs.getDate(7));
                studentUser.setEndDate(rs.getDate(8));
                studentUser.setAge(rs.getInt(9));
                studentUser.setAddress(rs.getString(10));
                studentUser.setState(rs.getString(11));
                studentUser.setZip(rs.getInt(12));
                studentUser.setAreaCode(rs.getInt(13));
                studentUser.setPhone(rs.getInt(14));
                studentUser.setPayRate(rs.getDouble(15));
            }
            close(conn, ps);
            
        } catch (IncorrectPasswordException ex) {
            close(conn, ps);
            String x = ex.getMessage();
            System.out.println("error: "+x);
            ViewUtils vu = new ViewUtils();
            vu.addExceptionMessage(x);
        }
        return (StudentBean) studentUser;
    }
CREATED NEW INSTANCE OF ConnectStudentDAO
LoginInfo profile: stu_
LoginInfo user: sj
in getConnection of DerbyDAOFactory
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true
user: sj
selectStudent: 
SELECT
    stu_uid,
    stu_password,
    stu_lname,
    stu_mname,
    stu_fname,
    stu_gender,
    stu_age,
    stu_start_date,
    stu_end_date,
    stu_address,
    stu_state,
    stu_zip,
    stu_area_code,
    stu_phone,
    payee_uid
    FROM student
    WHERE stu_uid=?
studentUserName: SELECT stu_uid FROM student WHERE stu_uid=?
studentPassword: SELECT stu_password FROM student WHERE stu_uid=?
CONECTION IS GOOD FOR A TEST
query test: selectAllStudent

number of columns: 15
, STU_UID, STU_PASSWORD, STU_LNAME, STU_MNAME, STU_FNAME, STU_GENDER, STU_AGE, STU_START_DATE, STU_END_DATE, STU_ADDRESS, STU_STATE, STU_ZIP, STU_AREA_CODE, STU_PHONE, PAYEE_UID
, sj, s, null, null, Johnny, null, null, null, null, null, null, null, null, null, null
, ss, s, null, null, Suzie, null, null, null, null, null, null, null, null, null, null
PASSWORD: s
password: [C@16d2633
PASSWORD: s
error:  Incorrect password.
    Please try to login again.

I thought you already changed the type of password from character array to string? If `password` is still of type char[], the statement `PASSWORD.equals(password)` would never be true given that PASSWORD is of type String. If you really don't want to change the type of your `password` field, convert it to a String using one of its constructors and you should be good to go with the comparison.

This article has been dead for over six months. Start a new discussion instead.