Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Correct connection to DB?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Correct connection to DB?

    Hi,

    I'm kind of new in Java and trying to get a DB connection through JSP pages.
    I'm using Derby as DB since MySQL won't install correctly.
    Below is my login_bean which I use to compare what a user types in the login boxes to what's in DB, but something is not correct since I only get "Incorrect Username or Password!!" as message. That message is created on the login_fail.jsp page which it goes to when checkValidUser() returns "invalid"

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package beans;
     
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.RequestScoped;
    import java.sql.*;
    import java.util.*;
     
    /**
     *
     * @author kdehouwer
     */
    @ManagedBean(name="login_bean")
    @RequestScoped
    public class login_bean {
        private String username;
        private String password;
        private String dbusername;
     
        public String getDbpassword(){
            return dbpassword;
        }
        public String getDbusername(){
            return dbusername;
        }
     
        private String dbpassword;
        Connection con;
        ResultSet rs;
     
        public void dbData(String UName){
            try{
                Class.forName("org.apache.derby.jdbc.ClientDriver");
                con = DriverManager.getConnection("jdbc:derby://localhost:1527/recommendation");
                PreparedStatement statement = con.prepareStatement("SELECT * FROM users WHERE username like ('" + UName + "')");
                rs = statement.executeQuery();
                rs.next();
                dbusername = rs.getString(2).toString();
                dbpassword = rs.getString(3).toString();
            }
            catch(Exception ex){
                ex.printStackTrace();
                System.out.println("Exception Occur: " + ex);
            }
        }
     
        public String getPassword(){
            return password;
        }
     
        public void setPassword(String password){
            this.password = password;
        }
     
        public String getUsername(){
            return username;
        }
     
        public void setUsername(String username){
            this.username = username;
        }
     
        public String checkValidUser(){
            dbData(username);
            if(username.equalsIgnoreCase(dbusername)){
                if(password.equals(dbpassword)){
                    return "valid";
                }else{
                    return "invalid";
                }
            }else{
                return "invalid";
            }
        }
     
        /** Creates a new instance of login_bean */
        public login_bean() {
        }
    }


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    33
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Correct connection to DB?

    It looks like your JDBC does not specify a database for which to find the table. Try using a full URL like "jdbc:derby://localhost:1527/myDB;create=true;user=user;password=user".


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Correct connection to DB?

    "recommendation" is the name of my DB..
    If I add what you say it still does the same

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Correct connection to DB?

    @kney: Are there any exceptions? If yes, paste them here so that we could look over it.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Correct connection to DB?

    No, there are no exceptions.. It just shows my login page as it's supposed to, and when I enter the data from the only user i have in my db, it says "Incorrect Username or Password!!"

  6. #6
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Correct connection to DB?

    Quote Originally Posted by kney View Post
    No, there are no exceptions.. It just shows my login page as it's supposed to, and when I enter the data from the only user i have in my db, it says "Incorrect Username or Password!!"
    If there are no exceptions, it means, your DB connection is successfully established.

    So, as far as i know, it's probably not fetching the records from table or some other issues. Can you post the code where you actually checking the username and password (Credentials)?

  7. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Correct connection to DB?

    checkValidUser() checks the credentials
    but here is my login page

    HTML Code:
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <link href="login-box.css" rel="stylesheet" type="text/css" />
            <title>Login</title>
        </head>
        <body bgcolor="#0099CC">
            <f:view>
            <div style="padding: 100px 0 0 250px;">
            <div id="login-box">
            <h:form id="login_frm">   
            <h2>Login</h2>
            Enter your login information in the box below.<br /><br />
            <h:message for="username" styleClass="errorMsg" /> <br />
            <h:message for="password" styleClass="errorMsg" /> <br />
            <div id="login-box-name" style="margin-top:20px;">
                <h:outputText value="Username: " />
            </div>
            <div id="login-box-field" style="margin-top:20px;">
                <!--<input type="text" name="username" class="form-login" title="username" value="" size="30" maxlength="2048" />-->
                <h:inputText id="username" value="#{login_bean.username}" required="true" styleClass="form-login" />
            </div>
            <div id="login-box-name">
                <h:outputText value="Password: " />
            </div>
            <div id="login-box-field">
                <!--<input name="password" type="password" class="form-login" title="password" value="" size="30" maxlength="2048" />-->
                <h:inputSecret id="password" value="#{login_bean.password}" required="true" styleClass="form-login" />
            </div>
            <br />
            <label>&nbsp;</label>
            <h:commandButton action="#{login_bean.checkValidUser}" value="" type="submit" styleClass="button" />
            <!--<input type="submit" value="" name="submit" class="button"/>
            <a href="sessionAction.jsp">
                <img src="images/login-btn.png" width="103" height="42" style="margin-left:90px;" />
            </a>-->
            </h:form>
            </div>
            </div>
            </f:view>
        </body>
    </html>

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Correct connection to DB?

    Debug your dbData() function or place print statements after each line and fine what are the values coming into and what do you want them to.

    Also, modify your query by applying LOWER() at both sides, the username and the querytable column.
    So, that both could be of same case.
    And
    SELECT * FROM users WHERE username like ('" + UName + "')"
    Modify this as
    "SELECT * FROM user WHERE LOWER(username) like"+UName;
    But dont forget to lowercase the UName.

  9. #9
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Correct connection to DB?

    If I debug,
    variable "con" is empty
    variable "statement" says: statement => "statement" is not a known variable in the current context
    variable rs is empty
    dbusername & dbpassword is also empty

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Correct connection to DB?

    No, this can't be. I think, you are getting values of con and rs before creating connection, check them after connection creation and one more thing, your username and passwords are not exactly mapping to the bean.

    And also test that when you press the button(submit), does this come to the checkValidateUser function?

  11. #11
    Junior Member
    Join Date
    Oct 2011
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Correct connection to DB?

    Hmmm weird.
    When I step over the parts where the connection is being made it sends me to the exception part. And even though I have a print statement to print the exception, it doesn't do that
    and yes it comes to the checkValidUser function because that returns "invalid"

    EDIT: I get a java.sql.SQLNonTransientConnectionException: Connection authentication failure occured. Reason: userid or password invalid
    Last edited by kney; November 23rd, 2011 at 08:04 AM.

  12. #12
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Correct connection to DB?

    Exactly now, fill in the userid and password in the connection statement.

    Hope you get this solved. Got to go now

  13. #13
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Correct connection to DB?

    Did you get this solved?

Similar Threads

  1. Replies: 2
    Last Post: August 2nd, 2011, 08:11 AM
  2. Replies: 0
    Last Post: March 26th, 2011, 11:07 AM
  3. Is My answers correct??
    By Java.Coder() in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 28th, 2010, 06:22 AM
  4. Correct Coupling
    By poulenc in forum Java Theory & Questions
    Replies: 0
    Last Post: December 26th, 2010, 04:28 AM