Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Bet ya cant fix this! lots of code!

Options
  • 07-03-2008 12:07pm
    #1
    Registered Users Posts: 18,272 ✭✭✭✭


    UserDao2 wrote:
    package sd3004project.model.dao;

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    import java.util.ArrayList;

    import java.util.List;

    import sd3004project.model.business.Dealers;
    import sd3004project.model.business.Users;
    import sd3004project.model.exceptions.DaoException;

    public class UserDao extends Dao{
    public ArrayList<Users> findAllUsers() throws DaoException
    {
    ArrayList<Users> users = new ArrayList<Users>();
    Connection con = null ;
    PreparedStatement ps = null ;
    ResultSet rs = null ;
    Users p = null ;

    try
    {
    con = getConnection() ;
    String query = "SELECT * FROM Users" ;
    System.out.println(query) ;

    ps = con.prepareStatement(query) ;
    rs = ps.executeQuery() ;
    while (rs.next() )
    {
    Long userId = rs.getLong("UserID") ;
    String fname = rs.getString("FirstName");
    String lname = rs.getString("LastName");
    String address = rs.getString("Address1") ;
    String address2 = rs.getString("Address2") ;
    String address3 = rs.getString("Address3") ;
    String province = rs.getString("Province") ;
    String county = rs.getString("County") ;
    String phoneno = rs.getString("PhoneNumber");
    String email = rs.getString("Emailaddress");
    String username = rs.getString("UserName");
    String pass = rs.getString("Password");


    p = new Users( userId, fname, lname, address, address2, address3, province, county, phoneno, email, username, pass) ;
    users.add(p) ;
    // System.out.println("Id " + userId + " Name "+ fname + " " + lname + " Address " + address + " " + address2 + " " + address3 + " Province " + province +" County " + county +" Telephone " + phoneno + " Email " + email + " UserNAme " + username + " Password " + pass);
    }
    }
    catch(SQLException e)
    {
    throw new DaoException("findallUsers(): " + e.getMessage() ) ;
    }
    finally
    {
    try
    {
    if (rs != null)
    rs.close() ;
    if (ps != null)
    ps.close() ;
    if (con != null)
    freeConnection(con) ;
    }
    catch(SQLException e)
    {
    throw new DaoException("findallUsers(): " + e.getMessage() ) ;
    }
    }
    return users ;
    }

    public int RegisterUser(Users u) throws DaoException
    {
    Connection con = null ;
    PreparedStatement ps = null ;
    ResultSet rs = null ;
    int rows = 0 ;

    try
    {
    con = getConnection() ;
    String query = "SELECT * FROM USERS WHERE USERNAME = ?" ;
    ps = con.prepareStatement(query) ;
    ps.setString(1, u.getUsername() ) ;
    rs = ps.executeQuery() ;
    if (rs.next() )
    throw new SQLException("Username " + u.getUsername() + " already exists") ;

    rs.close() ; rs = null ;

    String command = "INSERT INTO USERS VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ;

    System.out.println(command) ;

    ps = con.prepareStatement(command) ;
    ps.setInt(1, (int) u.getUserid() ) ;
    ps.setString(2, u.getFname() ) ;
    ps.setString(3, u.getLname() ) ;
    ps.setString(4, u.getAddress1() ) ;
    ps.setString(5, u.getAddress2() ) ;
    ps.setString(6, u.getAddress3() ) ;
    ps.setString(7, u.getProvince());
    ps.setString(8, u.getCounty());
    ps.setString(9, u.getPhoneno());
    ps.setString(10, u.getEmail());
    ps.setString(11, u.getUsername());
    ps.setString(12, u.getPassword());


    rows = ps.executeUpdate() ;

    }

    catch(SQLException e)
    {
    throw new DaoException("RegisterUser(): " + e.getMessage() ) ;
    }
    finally
    {
    try
    {
    if (rs != null)
    rs.close() ;
    if (ps != null)
    ps.close() ;
    if (con != null)
    freeConnection(con) ;
    }
    catch(SQLException e)
    {
    throw new DaoException("RegisterUser(): " + e.getMessage() ) ;
    }
    }
    return rows ;
    }


    public int deleteUser(long userId) throws DaoException
    {
    Connection con = null ;
    PreparedStatement ps = null ;
    ResultSet rs = null ;
    int rows = 0 ;
    try
    {
    con = getConnection() ;
    String command = "DELETE FROM Users WHERE useriD = ?" ;
    System.out.println(command) ;

    ps = con.prepareStatement(command) ;
    //ps.setLong(1, gameId) ; // setLong() not supported by JDBC-ODBC bridge
    ps.setInt(1, (int) userId ) ;

    rows = ps.executeUpdate() ;
    }
    catch(SQLException e)
    {
    throw new DaoException("deleteUser(): " + e.getMessage() ) ;
    }
    finally
    {
    try
    {
    if (rs != null)
    rs.close() ;
    if (ps != null)
    ps.close() ;
    if (con != null)
    freeConnection(con) ;
    }
    catch(SQLException e)
    {
    throw new DaoException("deleteUser(): " + e.getMessage() ) ;
    }
    }

    return rows ;
    }


    public int amendUser(List<Users> users) throws DaoException
    {
    Connection con = null ;
    PreparedStatement ps = null ;
    ResultSet rs = null ;
    int rows = 0 ;

    try
    {
    con = getConnection() ;
    for (Users u : users) {
    String command ="UPDATE Users SET FirstName = ?, LastName = ?, Address1 = ?, Address2 = ?, Address3 = ?, Province = ?, County = ?, PhoneNumber = ?, EmailAddress = ? WHERE UserId =?" ;
    System.out.println(command) ;
    ps = con.prepareStatement(command) ;
    ps.setString(1, u.getFname());
    ps.setString(2, u.getLname());
    ps.setString(3, u.getAddress1() ) ;
    ps.setString(4, u.getAddress2() ) ;
    ps.setString(5, u.getAddress3() ) ;
    ps.setString(6, u.getProvince() ) ;
    ps.setString(7, u.getCounty() ) ;
    ps.setString(8, u.getPhoneno() ) ;
    ps.setString(9, u.getEmail() ) ;
    //ps.setString(10, u.getUsername());
    //ps.setString(11, u.getPassword() ) ;

    //ps.setLong(6, c.getCompanyId() ) ; // setLong not supported by JDBC-ODBC bridge
    ps.setInt(10, (int) u.getUserid() ) ;

    rows += ps.executeUpdate() ;
    }
    }
    catch(SQLException e)
    {
    throw new DaoException("amendUser(): " + e.getMessage() ) ;
    }
    finally
    {
    try
    {
    if (rs != null)
    rs.close() ;
    if (ps != null)
    ps.close() ;
    if (con != null)
    freeConnection(con) ;
    }
    catch(SQLException e)
    {
    throw new DaoException("amendUser(): " + e.getMessage() ) ;
    }
    }

    return rows ;
    }

    public Users findUserByCodeNamePassword(String aCodeName, String aPassword) throws DaoException
    {
    Connection con = null ;
    PreparedStatement ps = null ;
    ResultSet rs = null ;
    Users p = null ;

    try
    {
    con = getConnection() ;
    String query = "SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?" ;

    ps = con.prepareStatement(query) ;
    ps.setString(1, aCodeName) ;
    ps.setString(2, aPassword) ;
    rs = ps.executeQuery() ;
    if (rs.next() )
    {
    Long userId = rs.getLong("UserID") ;
    String fname = rs.getString("FirstName");
    String lname = rs.getString("LastName");
    String address = rs.getString("Address1") ;
    String address2 = rs.getString("Address2") ;
    String address3 = rs.getString("Address3") ;
    String province = rs.getString("Province") ;
    String county = rs.getString("County") ;
    String phoneno = rs.getString("PhoneNumber");
    String email = rs.getString("Emailaddress");
    String username = rs.getString("UserName");
    String pass = rs.getString("Password");

    p = new Users( userId, fname, lname, address, address2, address3, province, county, phoneno, email, username, pass) ;
    }
    return p ; // p may be null
    }
    catch(SQLException e)
    {
    throw new DaoException("findPlayerByCodeNamePassword(): " + e.getMessage() ) ;
    }
    finally
    {
    try
    {
    if (rs != null)
    rs.close() ;
    if (ps != null)
    ps.close() ;
    if (con != null)
    freeConnection(con) ;
    }
    catch(SQLException e)
    {
    throw new DaoException("findPlayerByCodeNamePassword(): " + e.getMessage() ) ;
    }
    }
    }

    public int amendUser(Users u) throws DaoException
    {
    Connection con = null ;
    PreparedStatement ps = null ;
    ResultSet rs = null ;
    int rows = 0 ;

    try
    {
    con = getConnection() ;
    String command ="UPDATE Users SET FirstName = ?, LastName = ?, Address1 = ?, Address2 = ?, Address3 = ?, Province = ?, County = ?, PhoneNumber = ?, EmailAddress = ? WHERE UserId =?" ;
    System.out.println(command) ;
    ps = con.prepareStatement(command) ;
    ps.setString(1, u.getFname());
    ps.setString(2, u.getLname());
    ps.setString(3, u.getAddress1() ) ;
    ps.setString(4, u.getAddress2() ) ;
    ps.setString(5, u.getAddress3() ) ;
    ps.setString(6, u.getProvince() ) ;
    ps.setString(7, u.getCounty() ) ;
    ps.setString(8, u.getPhoneno() ) ;
    ps.setString(9, u.getEmail() ) ;
    //ps.setString(10, u.getUsername());
    //ps.setString(11, u.getPassword() ) ;

    //ps.setLong(6, c.getCompanyId() ) ; // setLong not supported by JDBC-ODBC bridge
    ps.setInt(10, (int) u.getUserid() ) ;

    rows = ps.executeUpdate() ;
    }

    catch(SQLException e)
    {
    throw new DaoException("amendUser(): " + e.getMessage() ) ;
    }
    finally
    {
    try
    {
    if (rs != null)
    rs.close() ;
    if (ps != null)
    ps.close() ;
    if (con != null)
    freeConnection(con) ;
    }
    catch(SQLException e)
    {
    throw new DaoException("amendUser(): " + e.getMessage() ) ;
    }
    }

    return rows ;
    }



    }


    import java.io.FileInputStream;

    import java.io.IOException;

    import java.util.Properties;

    import javax.faces.context.FacesContext;

    import javax.servlet.http.HttpServletRequest;


    import sd3004project.model.business.Users;
    import sd3004project.model.dao.UserDao;

    import sd3004project.model.exceptions.DaoException;

    import view.utils.ScopeUtility;

    public class LoginSessionBean {
    private long userId ;
    private String fname;
    private String lname;
    private String address1;
    private String address2;
    private String address3;
    private String province;
    private String county;
    private String phoneno;
    private String email;
    private String username;
    private String password;
    private boolean userLoggedIn ;
    private boolean adminLoggedIn ;
    private boolean loginAttempt = false ;

    UserDao dao = null ;

    public LoginSessionBean() throws DaoException {
    dao = new UserDao() ;
    }

    public void setUserId(long userId) {
    this.userId = userId;
    }

    public long getUserId() {
    return userId;
    }

    public void setUserName(String userName) {
    this.username = userName;
    }

    public String getUserName() {
    return username;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public String getPassword() {
    return password;
    }


    public void setUserLoggedIn(boolean userLoggedIn) {
    this.userLoggedIn = userLoggedIn;
    }

    public boolean isUserLoggedIn() {
    return userLoggedIn;
    }

    public void setAdminLoggedIn(boolean adminLoggedIn) {
    this.adminLoggedIn = adminLoggedIn;
    }

    public boolean isAdminLoggedIn() {
    return adminLoggedIn;
    }

    public void setLoginAttempt(boolean loginAttempt) {
    this.loginAttempt = loginAttempt;
    }

    public boolean isLoginAttempt() {
    return loginAttempt;
    }

    /*public String verifyLoginButton() {
    Users user = null ;
    try
    {
    user = dao.findUserByCodeNamePassword(username, password) ;
    }
    catch(DaoException e) {
    HttpServletRequest request = ScopeUtility.getRequest() ;
    request.setAttribute("error", "Verify login " + e.getMessage() );
    return "error" ;
    }
    if (user == null) {
    userLoggedIn = false ;
    adminLoggedIn = false ;
    loginAttempt = true ;
    return null ; // pack to same page
    }
    else
    {
    /*
    * This is not very clever (hardcoding admin password, etc.). It would make
    * more sense to have different types of user (player). The player type
    * could determine if the user is administrator or ordinary user.

    if (user.getUsername().equals("admin") && user.getPassword().equals("admin"))
    adminLoggedIn = true ;
    else
    userLoggedIn = true ;

    userId = user.getUserid() ;

    return "success" ;
    }
    }*/

    public String registerButton() {
    Users p = new Users(34, fname, lname, address1, address2, address3, province, county, phoneno, email, username, password) ;
    try
    {
    int rows = dao.RegisterUser(p) ;
    if (rows == 1)
    {
    userId = p.getUserid() ;
    userLoggedIn = true ;
    return "success";
    }
    else
    throw new DaoException("Register Player Pailed") ;
    }
    catch(DaoException e) {
    HttpServletRequest request = ScopeUtility.getRequest() ;
    request.setAttribute("error", e.getMessage() );
    return "error" ;
    }
    }

    public String verifyLogoutButton() {
    userId = 0 ;
    username = null ;
    password = null ;


    userLoggedIn = false ;
    adminLoggedIn = false ;
    loginAttempt = false ;

    ScopeUtility.resetSessionUsers();

    return "home";
    }

    public String verifyLoginButton() throws DaoException
    {
    UserDao dao = new UserDao();

    Users p = dao.findUserByCodeNamePassword(username,password);

    if (p == null)
    return "failure";
    else

    return "success";

    /*if (codeName.equals("lolman") && password.equals("lol123"))
    return "success";
    else
    return "failure";*/
    }

    public String amendProfileButton() {
    Users p = new Users(34, fname, lname, address1, address2, address3, province, county, phoneno, email, username, password) ;

    try
    {
    int rows = dao.amendUser(p) ;
    if (rows == 1)
    return "viewProfile" ;
    else
    throw new DaoException("Amend profile failed");
    }
    catch(DaoException e) {
    HttpServletRequest request = ScopeUtility.getRequest() ;
    request.setAttribute("error", e.getMessage() );
    return "error" ;
    }
    }

    public String deleteProfileButton() {
    try
    {
    int rows = dao.deleteUser(userId) ;
    if (rows == 0)
    throw new DaoException("Failed to delete profile") ;
    verifyLogoutButton() ;
    }
    catch(DaoException e) {
    HttpServletRequest request = ScopeUtility.getRequest() ;
    request.setAttribute("error", e.getMessage() );
    return "error" ;
    }
    return "home";
    }

    public void setAddress1(String address1) {
    this.address1 = address1;
    }

    public String getAddress1() {
    return address1;
    }

    public void setAddress2(String address2) {
    this.address2 = address2;
    }

    public String getAddress2() {
    return address2;
    }

    public void setAddress3(String address3) {
    this.address3 = address3;
    }

    public String getAddress3() {
    return address3;
    }

    public void setProvince(String province) {
    this.province = province;
    }

    public String getProvince() {
    return province;
    }

    public void setCounty(String county) {
    this.county = county;
    }

    public String getCounty() {
    return county;
    }

    public void setPhoneno(String phoneno) {
    this.phoneno = phoneno;
    }

    public String getPhoneno() {
    return phoneno;
    }

    public void setEmail(String email) {
    this.email = email;
    }

    public String getEmail() {
    return email;
    }

    public void setUsername(String username) {
    this.username = username;
    }

    public String getUsername() {
    return username;
    }
    }


    Index.jsp wrote:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;
    <%@ page contentType="text/html;charset=windows-1252"%>
    <%@ taglib uri="http://java.sun.com/jsf/html&quot; prefix="h"%>
    <%@ taglib uri="http://java.sun.com/jsf/core&quot; prefix="f"%>
    <f:view>
    <html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en">
    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="style.css" type="text/css" />
    <title>CarBuyer.ie</title>
    </head>
    <body>
    <div id="page" align="center">
    <div id="header">
    <div id="companyname" align="left">
    <h:form></h:form>CarBuyer.ie</div>
    <div align="right" class="links_menu" id="menu"><a href="#">Home</a> | <a href="#">About Us</a> | <a href="#">Products</a> | <a href="#">Our Services</a> | <a href="#">Contact Us</a> </div>
    </div>
    <br />
    <div id="content">
    <div id="leftpanel">
    <div class="table_top">
    <div align="center"><span class="title_panel">News</span> </div>
    </div>
    <div class="table_content">
    <div class="table_text">
    <span class="news_date">January 6th, 2007</span> <br />
    <span class="news_text">Ford today announced plans for there green hybrid mondeo</span><br />
    <span class="news_more"><a href="#">Read More</a></span><br /><br />
    <span class="news_date">January 27, 2007</span> <br />
    <span class="news_text">Honda reveal the FD2 Civic type R will not be realeased in Europe</span><br />
    <span class="news_more"><a href="#">Read More</a></span>
    </div>
    </div>
    <div class="table_bottom">
    <img src="images/table_bottom.jpg" width="204" height="23" border="0" alt="" />
    </div>
    <br />
    <div class="table_top">
    <span class="title_panel">Links</span>
    </div>
    <div class="table_content">
    <div class="table_text">
    <span class="news_more"><a href="http://www.cbg.ie">Car Buyers Guide</a></span><br />
    <span class="news_more"><a href="http://www.autotrader.ie">Autotrader</a></span><br />
    <span class="news_more"><a href="http://www.driver.ir">Driver</a></span><br />
    <span class="news_more"><a href="http://www.ros.ie">Vrt Calculator</a></span><br />
    </div>
    </div>
    <div class="table_bottom">
    <img src="images/table_bottom.jpg" width="204" height="23" border="0" alt="" />
    </div>
    <br />
    </div>
    <div id="contenttext">
    <span class="title_blue">Welcome To CarBuyer.ie</span><br />
    <span class="subtitle_gray">Please login use the site or register</span><br />
    <br />
    <br />

    <h:panelGrid columns="3">

    <f:facet name="header">
    <h:outputText value="invalidLoginPrompt" rendered="#{LoginSessionBean.loginAttempt}"/>
    </f:facet>

    <h:outputLabel value="User Name"/>
    <h:inputText value="#{LoginSessionBean.username}"
    required="true"
    maxlength="10"
    id="username"/>
    <h:message for="username"/>

    <h:outputLabel value="password"/>
    <h:inputSecret value="#{LoginSessionBean.password}"
    required="true"
    maxlength="10"
    id="password"/>
    <h:message for="password"/>
    <h:commandButton value="Submit"
    action="#{LoginSessionBean.verifyLoginButton}"/>
    </h:panelGrid>

    <%--
    oracle-jdev-comment:preferred-managed-bean-name:loginSessionBean
    --%>

    </div>
    <br /><div class="footer">
    <br />
    <a href="#">Home</a> | <a href="#">About Us</a> | <a href="#">Products</a> | <a href="#">Our Services</a> | <a href="#">Contact Us</a>
    </div>
    </div>
    </div>
    </body>
    </html>
    </f:view>



    I'm trying to make a website with a login, the loginsessionbean is used to verify the data in two input textboxes when the button is pressed, the method works when i test it in the userdao, everything compiles and runs fine and the web page shows up but when i try to login the page shows up the little yellow triangle with an error that is "Error: Object Required".

    Can anyone see why the object wouldn't be getting passed??


Comments

  • Registered Users Posts: 763 ✭✭✭Dar


    {LoginSessionBean.loginAttempt}

    Unless I'm reading the code wrong you're trying to call loginAttempt as a class method, which it isn't. You need to instance the bean first, no?


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Don't develop in Java myself but I do see in your final source there that you have an empty <h:form> tag - should that be wrapped around the login panel?

    If so that could give a javascript error if there's javascript validation on the login fields as the script would expect the login fields to appear in the context of a form. I'm guessing that's where the 'yellow triangle' is coming from. Use the view source option in the browser when displaying the login page and take a look through the outputted HTML to see if you can see something wrong with the structure of it and the javascript produced. Double clicking the little triangle should give you the line number where the code is failing - much easier to debug javascript in firefox.

    Before you deploy this live as well I would make sure you understand the mechanism and consequences of whatever you are doing to verify admin users.


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    ah yeh bluefrog it wont be deployed live, and the admin thing there is sort of a wee test it will be developed into a proper admin login. Thanks will look through the html.

    @Dar I'm trying to call the loginAttempt from the loginsessionbean to see if its true or false, am I doing this wrong?


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    if i view it as source it comes out as html and has a problem with this line
    <td><input type="submit" name="_id6" value="Submit" onclick="clearFormHiddenParams(this.form.id);" /></td>

    which relates to the submit button and i presume this line in jsp
    <h:commandButton value="Submit"
    action="#{LoginSessionBean.verifyLoginButton}"/>

    any ideas? like i said the error just comes up as Error: object required


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Are the input tags for the login all inside a form tag?


  • Advertisement
  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    yes i removed the empty form tag and all the input tags are in the other h:form tag


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Post the HTML source and I'll take a look - pm me it if you want


  • Moderators, Music Moderators Posts: 23,361 Mod ✭✭✭✭feylya


    Ah, so they're still doing the webshop in DKIT for the third year SDev course then?


  • Registered Users Posts: 378 ✭✭sicruise


    Can you not just get the application server to manage user accounts?

    It is much tidier and easier.

    Your bean looks more like a DTO than a bean and it doesn't implement a bean class.

    ... I know this doesn't help you solve your problem, but it is a simple issue to resolve if you just keep it neat and tidy.


  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Bluefrog wrote: »
    Post the HTML source and I'll take a look - pm me it if you want

    have pm'd you with it
    feylya wrote: »
    Ah, so they're still doing the webshop in DKIT for the third year SDev course then?

    yeh, find it pretty easy so far just this niggly little error is holding me back, cant get the lecterurs time because of erasmus students....gggrrr
    sicruise wrote: »
    Can you not just get the application server to manage user accounts?

    It is much tidier and easier.

    Your bean looks more like a DTO than a bean and it doesn't implement a bean class.

    ... I know this doesn't help you solve your problem, but it is a simple issue to resolve if you just keep it neat and tidy.

    sounds much easier but this is the only way we've been thought it


  • Advertisement
  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Folks I'll remind you all that requesting pm's in thread is against the rules of this forum.


Advertisement