Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6675769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:54:03+00:00 2026-05-26T03:54:03+00:00

Here is my code import java.io.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.sql.*;

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:54:04+00:00Added an answer on May 26, 2026 at 3:54 am

    There’s no way that can work. On init(), you create a PreparedStatement using a query that includes the fields tableName and ssnNum. At that point, both of those are null. They don’t get assigned until a request is made later on (in doGet()). You’re definitely getting errors. You need to find them.

    Other stuff:

    1. Use local variables instead of fields where appropriate.
    2. Open a new connection on each request.
    3. Don’t close the response Writer (or OutputStream).
    4. //Loads Driver is a terrible comment. The others aren’t too hot, either.
    5. Never ever EVER concatenate strings directly into a SQL query. Use a PreparedStatement with placeholders and the set* methods to plug in parameters.
    6. Add some meaningful logging to help you identify what’s going on without having to attach a debugger. (You’ll obviously have to figure out where the messages are going first, though.)
    7. You’ll need to call next() on the ResultSet before you can get values from it.
    8. There’s no need for the Class.forName(...) line.
    9. Your ResultSet, Statement, and/or Connection need to be closed when you’re done with them. Otherwise, you’re hemmhoraging resources.
    10. Instead of catching and ignoring exceptions, allow them to be thrown out of your servlet. It will help your troubleshooting.
    11. Never catch Exception. Only catch the specific types you wish to catch.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
Here is my code: import java.io.*; import java.util.*; import javax.comm.*; public class SMS {
Here is the code: import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.event.*;
Here is a simple piece of code: import java.io.*; public class Read { public
Here is code: import java.awt.*; import java.util.*; import javax.media.*; import javax.media.protocol.*; import javax.media.control.*; import
Here is my code import org.ksoap2.*; import org.ksoap2.serialization.*; import org.ksoap2.transport.*; import android.app.Activity; import android.os.Bundle;
How to convert a value from nanoseconds to seconds? Here's the code segment: import
I am trying to subclass NSOutlineView. Here is my code: OutlineViewSublcass.h: #import <Cocoa/Cocoa.h> @interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.