This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes JSP and the fly likes a question for good nerves Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "a question for good nerves" Watch "a question for good nerves" New topic
Author

a question for good nerves

Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

i have a jsp page that has an mysql command.when i try the command in mysql it works perfectly.when i try it out with my project it just doesnt send the right data..
in specific my mysql command finds a customer id using validation with username and password.
in mysql window as i said previously it works fine when i run from my project it brings no data....

thnx in advance guys
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Are there any error messages?
How are you connecting to the database?
Do you have permissions to connect?
Can you run any queries at all or is it just this query wrong?

Please post some code showing
- database connection
- your query that is failing
- any error messages
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

ok i have a checkout.jsp page that has 2 text fields.each one for username and password.
then i call a servlet from the action attribute from form

this is the servlet

import conne.Conne;

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class CustomerLogin extends HttpServlet
{
Connection con;
public void init()
{
Conne TheConnection=new Conne();
con=TheConnection.getConnection();
}

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

String Username = request.getParameter("username");
String Password = request.getParameter("password");
String Login = "true";
Integer Customerid = 0;

try
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select Customer_ID From User Where E_Mail = '" + Username + "' and Password = '" + Password +"';");
Customerid = Integer.parseInt(rs.getString("Customer_ID"));

if (Customerid!=null)
Login = "true";
else
Login = "false";
}
catch (SQLException e)
{
log("SQL Exception",e);
}

if (Login.equals("true"))
{
response.sendRedirect("/checkout1.jsp?Customerid='" + Customerid +"'");
}
else
{
response.sendRedirect("/checkout.jsp");
}
}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61086
    
  66

Please use better subject lines when posting (read this), and please be sure to use the UBB code tags when posting code.

Few people will take the time to look at reams of unformatted code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
I guess I'm one of those stupid idiots...

Can't see the database connection code you have (whatever is in that class Conne), so can't confirm if it works or not. Assuming it does work though...

The problem is, you haven't called rs.next().
When you first retrieve a result set it is pointing at nothing.
You have to call rs.next() to navigate to the first record (if any).
rs.next() returns false if there is no next record.

So your code


Whole lot of other things wrong with this code in my opinion.

Connecting to the database as part of the init() method is a wasteful approach. Each individual servlet in your application will have its own DBConnection - and it never gets released!
Connections should be got on a per request basis from a Connection Pool

Naming conventions/Coding standards
Variables should be names with camel case (start with lower case letter rather than upper case letter). I would refer you to Java coding conventions

Why are you using a String "true"/"false" instead of a boolean variable?

You need to close your resultset/statement objects (preferably in a finally clause)

That should be enough to be moving along with.
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Connecting to the database as part of the init() method is a wasteful approach. Each individual servlet in your application will have its own DBConnection - and it never gets released!
Worse still, you'll probably find that after 8 hours of inactivity, the next request will throw an IOException and all subsequent requests fail. This is because MySQL has a timeout of (by default) 8 hours on all connections.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Aris Doxakis
Ranch Hand

Joined: Dec 05, 2004
Posts: 136

thnx found out what was wrong.
im not familiar with the connection pool but will find out and fix that.didnt know that about the connections.i thought that after the servlet was not used any more it closed the connection.
the naming was just temp.naming is one of the basivs that i think i can handle.
thnx again...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question for good nerves