• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question for good nerves

 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1755
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64993
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1755
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aris Doxakis
Ranch Hand
Posts: 136
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic