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"
Code :
/*
* 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() {
}
}
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".
Re: Correct connection to DB?
"recommendation" is the name of my DB..
If I add what you say it still does the same
Re: Correct connection to DB?
@kney: Are there any exceptions? If yes, paste them here so that we could look over it.
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!!"
Re: Correct connection to DB?
Quote:
Originally Posted by
kney
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)?
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> </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>
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
Code :
SELECT * FROM users WHERE username like ('" + UName + "')"
Modify this as
Code :
"SELECT * FROM user WHERE LOWER(username) like"+UName;
But dont forget to lowercase the UName.
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
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?
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
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 :)
Re: Correct connection to DB?