Hi again
I still have the problem when I try to run my servlet and connect to the
database. I don't know why?
Here is my code. I will be very gratefull if you look at it and tell me if
something is wrong. Otherwise can you tell any good web site with
information concerning a connection to mysql database using jdbc. Note that
I am using linux mandrake 9.0, tomcat 4.1.18, and mm.mysql-2.0.13. I am not
sure if i define well the url of my database? What exactly should I put
there?
Thank you in advance
Alex
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Validate extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println( "<html><title>Cannot Reload</title><body
bgcolor=#FFFFFF>\n" );
out.println( "<h1>This page can not be reloaded\n" );
out.println( "</h1></body></html>" );
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//Get the user's account number and password
String username = req.getParameter("USERNAME");
String password = req.getParameter("PASSWORD");
//Check the name and password for validity
if (!allowUser(username, password)) {
out.println("<HTML><HEAD>");
out.println("<TITLE>Access Denied</TITLE>\n");
out.println("<BR><BR><BODY BGCOLOR = \"#DFF1FF\">");
out.println("<FONT FACE=Arial SIZE = 6 COLOR=Red><STRONG>\n");
out.println("You entered an invalid login and password.<br>\n");
out.println("Access has been denied.</STRONG></FONT></BODY>");
out.println("<BR><BR><BR>\n<FONT SIZE = 6>");
out.println("<A HREF = http://localhost:8080/servlets/validate.html>");
out.println("Try again </A>\n<BR><BR>");
out.println("<A HREF = http://localhost:8080/servlets/shop.html>");
out.println("Go shopping </A>\n<BR><BR>");
out.println("<A HREF = http://localhost:8080/servlets/insert.html>");
out.println("Register </A>\n<BR><BR>");
}
else {
out.println("<HTML><HEAD>");
out.println("<TITLE>Thank you</TITLE>\n");
out.println("<BODY BGCOLOR = \"#DFF1FF\">");
out.println("<FONT SIZE = 3 COLOR = BLUE>");
out.println("Permision has been granted </FONT>\n");
out.println("<BR><BR><BR><FONT SIZE = 8 COLOR = BLUE>");
out.println("Welcome back " + username );
out.println(" ! <BR></FONT>\n");
out.println("<BR><BR><BR>\n<FONT SIZE = 6>");
out.println("<A HREF = http://localhost:8080/servlets/shop.html>");
out.println("Go shopping</A></FONT>\n");
}
}
protected boolean allowUser(String username, String password) {
String login ;
String pass ;
try {
Class.forName("org.gjt.mm.mysql.Driver");
}
catch (Exception E) {
System.err.println("Unable to load driver.");
E.printStackTrace();
}
try {
String url = "jdbc:mysql://localhost/project";
Connection con = DriverManager.getConnection(url, "client", "alex");
Statement stmt = con.createStatement();
//Execute a select statement to fetch login and password
ResultSet rs = stmt.executeQuery("SELECT login, password FROM customers");
while (rs.next()) {
login = rs.getString(1);
pass = rs.getString(2);
if (username.equals( login ) ) {
if (password.equals( pass ) ) { return true;}
}
}
stmt.close();
con.close();
}
catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return false;
}
Quote:}