Here is my code
import java.io.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.sql.*; @WebServlet(name = "Scores", urlPatterns = {"/Scores"}) public class Scores extends HttpServlet{ private Connection conn; private PreparedStatement psmt; private ResultSet rs; private String tableName; private String ssnNum; @Override public void init() throws ServletException { connect(); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); try{ tableName = request.getParameter("tableName"); ssnNum = request.getParameter("ssnNum"); rs = psmt.executeQuery(); out.print("\t\t\t"); out.print(rs.getString("Student") + " " + rs.getString("Score")); out.print("<br>"); out.close(); }catch(Exception e){ System.err.println(e); } } public void connect(){ try{ //Loads Driver Class.forName("com.mysql.jdbc.Driver"); //Establishes a connection to DataBase Javabook conn = DriverManager.getConnection( "jdbc:mysql://localhost/javabook", "root", "password"); psmt = conn.prepareStatement("select Student, Score from " + tableName + " where ssn = " + ssnNum); } catch (Exception e){ System.err.println(e); }//End Try/Catch Block } } Here is the html
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>TravisMeyersP3</title> </head> <body> Find Your Current Score <form method = "get" action = "Scores"> <p>Social Security Number <font color = "#FF0000">*</font> <input type = "text" name = "ssnNum"> </p> <p>Course Id <select size = "1" name = "tableName"> <option value = "Cpp">C++</option> <option value = "AdvJava">Advanced Java</option> </select> </p> <p><input type = "submit" name = "Submit" value = "Submit"> </p> </body> </html> I'm using apache tomcat 7.0.21, and java jdk 7 in netbeans. The servlet is supposed to access one of two tables I've created in mysql and display the values for the user parameters. I'm not getting any compile or runtime errors. For some reason the servlet isn't accessing mysql and displaying the result after the user submits the form.
First off, I have to thank Ryan Stewart, The Elite Gentleman and CodeBuzz for taking time out of their days to help me. Along with the above mentioned problems I also had a problem with sql syntax. As I set the variables tableName and ssnNum into my new PreparedStatement (PreparedStatement psmt = conn.prepareStatement("select Student, Score from ? where SSN = ?;");) using psmt.setString(1, tableName) etc. for some reason that was putting single quotes around the string in the prepared statement. MySQL didn't like that and the only place that was showing an error was in the tomcat command window. After fixing the above mentioned problems everything worked great. Again thank you everyone for helping me with this.