Hello
Could someone tell me if this code is even close to code for a

login using a DAO and an embeded db?

if not there is a proceedure losted below that I really don't

understand the '?' and how to use the
uid to query the db


specific question

//[b]
            //SelectUserGroup will return the user group related
            /*
             * <entry key="SelectUserGroup"> SELECT groupName 

FROM usergroup AS ug,usergroup_mapping
    AS ugm WHERE  ugm.uid">? AND ugm.groupid">ug.groupid 

</entry>
             */
            //[/b]

Really confused at this point here is the output thus far

run:
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true
shutdown database 
disconnected after building the database
Exception in thread "main" java.lang.ClassCastException: 

model.dao.ConnectStudentDAO cannot be cast to 

view.bean.StudentBean
in LoginInfoBean
        at 

view.bean.LoginInfoBean.loginAction(LoginInfoBean.java:43)
in the getStudentUserDAO studentUserDAO in ModelUtils.java 
        at 

view.SchoolJDesktopPane.login(SchoolJDesktopPane.java:183)
        at view.SchoolJDesktopPane.<init>(and it is not going 

back to LoginInfoBean.java with an instance 
SchoolJDesktopPane.java:118)
        at view.Main.createJDesktopPane(Main.java:43)
of StudentDAO which is an instance of daoClass.newInstance 
        at view.Main.main(Main.java:38)
FROM XML <entry 

key='studentDAO'>model.dao.ConnectStudentDAO</entry>

Java Result: 1
BUILD SUCCESSFUL (total time: 58 seconds)

loginInfo is a model (getters and setters
at login a profile is selected and uer uid and password

collected
student is selected (stu_)

public String loginAction(String newUser, char[] newPassword, 

String newProfile) throws ClassNotFoundException, 

InstantiationException, IllegalAccessException, 

ProfileException, LoginException, FileNotFoundException, 

IOException {
        try {
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setPassword(newPassword.toString());
            loginInfo.setUser(newUser);
            loginInfo.setProfile(newProfile);
            if (newProfile != null) {
                if (newProfile.equals("stu_")) {
                    Student student = new Student();
                    [b]StudentBean studentBean = (StudentBean) 

ModelUtils.getStudentUserDAO();[/b]
                    studentBean.setLoggedIn(true);
                    ModelUtils.copy(studentBean, student);
                    return "profile";
                }
....

ModelUtils.java Selects StudentDAO.java

public class ModelUtils {

    private static ResourceBundle resources;
    public static final String RESOURCES = 

ModelUtils.class.getPackage().getName()
            + ".res.ModelResources";
    private static StudentDAO studentDAO;
    private static InstructorDAO instructorDAO;
    private static AdminDAO adminDAO;
    private static PayeeDAO payeeDAO;
    public static final String XMLRESOURCES = 

"xml/ModelResourcesXML.properties";
    public static Properties xmlResource;
....


public synchronized static StudentDAO getStudentUserDAO() throws 

ClassNotFoundException, InstantiationException, 

IllegalAccessException, FileNotFoundException, IOException {
        if (studentDAO == null) {

            Class daoClass= 

Class.forName(getResource("studentDAO"));
            studentDAO = (StudentDAO) daoClass.newInstance();

        }
        System.out.println("in the getStudentUserDAO 

studentUserDAO in ModelUtils.java \n" +
                "and it is not going back to LoginInfoBean.java 

with an instance \n" +
                "of StudentDAO which is an instance of 

daoClass.newInstance \n" +
                "FROM XML <entry 

key='studentDAO'>model.dao.ConnectStudentDAO</entry>\n");
        return studentDAO;
    }
....

StudentDAO.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;
import model.Student;
import model.err.IncorrectPasswordException;
import model.err.LoginException;
import model.err.ProfileException;
import model.err.SubscribeException;
import model.err.UnsubscribeException;
/**
 *
 * @author depot
 */
public interface StudentDAO {
    public Student connect(ConnectStudentDAO connectStudentDAO)
            throws LoginException,
            //UnknownstudentUserException,
            IncorrectPasswordException;
//this code not in use not valid
/*
    public void insertStudent(Student studentUser)
            throws SubscribeException;

    public void update(Student studentUser)
            throws ProfileException;

    public void delete(Student studentUser)
            throws UnsubscribeException;
*/
}

ConnectStudentDAO.java is an instance of ConnectDAO.JAVA

which shows it is getting a connection to the embedded derby

db

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;


import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.LoginInfo;
import model.ModelUtils;
import model.Student;
import model.err.IncorrectPasswordException;
import model.err.LoginException;
import model.err.ProfileException;
import model.err.SubscribeException;
import model.err.UnknownSubscriberException;
import model.err.UnsubscribeException;

/**
 *
 * @author depot
 */
public class ConnectStudentDAO implements StudentDAO{
    public String type;
    public Student newUser;
    private LoginInfo loginInfo;
    
    private Student studentUser;

    public ConnectStudentDAO(){

    }
    

////////////////////////////////////////////////////////////////

//////
    //////////////////  login find user 

/////////////////////////
    

////////////////////////////////////////////////////////////////
   public Connection conn=null;
    public void ConnectStudent() throws 

UnknownSubscriberException, SQLException, 

IncorrectPasswordException, LoginException, 

FileNotFoundException, IOException{
        DerbyDAOFactory ddf =new DerbyDAOFactory();
        conn=(Connection)ddf.getConnection();

        PreparedStatement ps = null;

            //conn = dataSource.getConnection();
            ps = (PreparedStatement) conn.prepareStatement(
                    

ModelUtils.getXMLResource("SelectUserGroup"));

            //[b]
            //SelectUserGroup will return the user group related
            /*
             * <entry key="SelectUserGroup"> SELECT groupName 

FROM usergroup AS ug,usergroup_mapping
    AS ugm WHERE  ugm.uid">? AND ugm.groupid">ug.groupid 

</entry>
             */
            //[/b]
            //ps.setString(1, loginInfo.getEmail());
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                throw new UnknownSubscriberException();// no 

exceptions are good
            }

            String groupName = rs.getString(1);
           
           
            
            if (groupName.equals("student")) {
                setType("student");

                Student studentUser = select();

                newUser = studentUser;
                select();

            }
    }
     public Student select() throws UnknownSubscriberException, 

IncorrectPasswordException, LoginException, SQLException, 

FileNotFoundException, IOException {
        Connection conn = getConn();

        PreparedStatement ps = null;


            //conn = dataSource.getConnection();

            ps = (PreparedStatement) conn.prepareStatement(
                    

ModelUtils.getXMLResource("SelectAdministrator"));
            //ps.setString(1, loginInfo.getEmail());
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                throw new UnknownSubscriberException();// no 

exceptions are good
            }
            String uid = rs.getString(1);


            if (!loginInfo.getUser().equals(uid)) {
                throw new UnknownSubscriberException();
            }

            String password = rs.getString(2);
            if (!loginInfo.getPassword().equals(password)) {
                throw new IncorrectPasswordException();
            }

            //newUser.setEmail(loginInfo.getEmail());
            studentUser.setUid(rs.getString(uid));
            studentUser.setPassword(password);
            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));

            return studentUser;

    }   

}

output

run:
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
returns isConnected: true
Exception in thread "main" java.lang.ClassCastException: 

model.dao.ConnectStudentDAO cannot be cast to 

view.bean.StudentBean
        at 

view.bean.LoginInfoBean.loginAction(LoginInfoBean.java:42)
        at 

view.SchoolJDesktopPane.login(SchoolJDesktopPane.java:183)
        at 

view.SchoolJDesktopPane.<init>(SchoolJDesktopPane.java:118)
        at view.Main.createJDesktopPane(Main.java:43)
        at view.Main.main(Main.java:38)
Java Result: 1
BUILD SUCCESSFUL (total time: 27 seconds)

Hello
This code seems to progress further in the app Could someone tell me if the code is

logical?

public String loginAction(String newUser, char[] newPassword, String newProfile) throws 

ClassNotFoundException, InstantiationException, IllegalAccessException, ProfileException, 

LoginException, FileNotFoundException, IOException {
        try {
            System.out.println("in LoginInfoBean");
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setPassword(newPassword.toString());
            loginInfo.setUser(newUser);
            loginInfo.setProfile(newProfile);
            if (newProfile != null) {
                if (newProfile.equals("stu_")) {
                    Student studentBean = new Student();
                   // StudentBean studentBean = (StudentBean) Student();
                    studentBean.setLoggedIn(true);
                    //ModelUtils.copy(studentBean, student);
                    return "profile";
                }
....

StudentBean.java

public class StudentBean extends Student{

    public transient boolean loggedIn = false;
/*
    public boolean isLoggedIn() {
        return loggedIn;
    }

    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }
 * 
 */

}

ModelUtils.java Selects StudentDAO.java

....
public synchronized static StudentDAO getStudentUserDAO() throws ClassNotFoundException, 

InstantiationException, IllegalAccessException, FileNotFoundException, IOException {
        if (studentDAO == null) {

            Class daoClass= Class.forName(getResource("studentDAO"));

            studentDAO = (StudentDAO) daoClass.newInstance();

        }
        
        return studentDAO;
    }
....

StudentDAO.java

...
public interface StudentDAO {
    
    public Student connect(ConnectStudentDAO connectStudentDAO)
            throws LoginException,
            //UnknownstudentUserException,
            IncorrectPasswordException;
/*
    public void insertStudent(Student studentUser)
            throws SubscribeException;

    public void update(Student studentUser)
            throws ProfileException;

    public void delete(Student studentUser)
            throws UnsubscribeException;
 * 
 */
}

ConnectStudentDAO.java is an instance of ConnectDAO.JAVA which shows it is getting a

connection to the embedded derby db

public class ConnectStudentDAO implements StudentDAO{
    public String type;
    public Student newUser;
    public LoginInfo loginInfo;    
    public Student studentUser;
    public ConnectStudentDAO(){

    }
    //////////////////////////////////////////////////////////////////////
    //////////////////  login find user /////////////////////////
    ////////////////////////////////////////////////////////////////
   public Connection conn=null;
  
    public void ConnectStudent() throws UnknownSubscriberException, SQLException, 

IncorrectPasswordException, LoginException, FileNotFoundException, IOException{
         System.out.println("in ConnectStudentDAO");
        DerbyDAOFactory ddf =new DerbyDAOFactory();
        conn=(Connection)ddf.getConnection();

        PreparedStatement ps = null;

            //conn = dataSource.getConnection();
            ps = (PreparedStatement) conn.prepareStatement(
                    ModelUtils.getXMLResource("SelectUserGroup"));

            //[b]
            //SelectUserGroup will return the user group related
            /*
             * <entry key="SelectUserGroup"> SELECT groupName FROM usergroup AS 

ug,usergroup_mapping
    AS ugm WHERE  ugm.uid">? AND ugm.groupid">ug.groupid </entry>
             */
            //[/b]
            //ps.setString(1, loginInfo.getEmail());
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                throw new UnknownSubscriberException();// no exceptions are good
            }

            String groupName = rs.getString(1);
           
           
            
            if (groupName.equals("student")) {
                setType("student");

                Student studentUser = select();

                newUser = studentUser;
                select();

            }
    }
     public Student select() throws UnknownSubscriberException, IncorrectPasswordException, 

LoginException, SQLException, FileNotFoundException, IOException {
        Connection conn = getConn();

        PreparedStatement ps = null;


            //conn = dataSource.getConnection();

            ps = (PreparedStatement) conn.prepareStatement(
                    ModelUtils.getXMLResource("SelectAdministrator"));
            //ps.setString(1, loginInfo.getEmail());
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                throw new UnknownSubscriberException();// no exceptions are good
            }
            String uid = rs.getString(1);


            if (!loginInfo.getUser().equals(uid)) {
                throw new UnknownSubscriberException();
            }

            String password = rs.getString(2);
            if (!loginInfo.getPassword().equals(password)) {
                throw new IncorrectPasswordException();
            }

            
            ///////////////// test output //////////////////////////
            ///////////////// test output //////////////////////////
            System.out.println(uid);                          /////
            System.out.println(password);                      /////
            Properties dbProperties=ModelUtils.loadXMLResources();///
            DBOut o =new DBOut();               //////////////////////
            o.testOutput(conn,dbProperties);    //////////////////////
            ///////////////// test output //////////////////////////
            ///////////////// test output //////////////////////////

            //newUser.setEmail(loginInfo.getEmail());
            studentUser.setUid(rs.getString(uid));
            studentUser.setPassword(password);
            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));

            return studentUser;

    }
//////////////////////////////////////////////////////////////////////
    ////////////////// end login ////////////////////////////////////
    ////////////////////////////////////////////////////////////////

output

run:
driver tableName from xml: org.apache.derby.jdbc.EmbeddedDriver
in StudentDAO.JAVA and in  connect of the ConnectDerbyDAO
returns isConnected: true
shutdown database 
disconnected after building the database
in LoginInfoBean
returned: profile
so,count and name: stu_guideTab.total stu_guideTabLabels
 start obj() type= guide
in main: String: xml/keycards.xml
doc=null
in main: String: xml/keycards.xml
doc=null
internal frame openFrameCount= 1
JOptionPane delivered error message 
 [b]Login failed Please contact the Webmaster.[/b]

new output

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
in LoginInfoBean
returned: profile
so,count and name: stu_guideTab.total stu_guideTabLabels
 start obj() type= guide
in main: String: xml/keycards.xml
doc=null
in main: String: xml/keycards.xml
doc=null
internal frame openFrameCount= 1
JOptionPane delivered error message 
 Login failed Please contact the Webmaster.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.