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)