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

  • SEARCH
  • Home
  • 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 6074005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:20:03+00:00 2026-05-23T10:20:03+00:00

I have been trying to display a MySQL table for some time and I

  • 0

I have been trying to display a MySQL table for some time and I did succeed, however I’m having a hard time figuring out how to display a table using a query taken from a text box.

So basically this is my setup, I have a servlet, the servlet has a html button which is supposed to update the table being shown according to the query that was written on the mentioned text box.

The table is displayed using this code

protected void showDB(PrintWriter out, ResultSet rs) throws SQLException {


    ResultSetMetaData rsMD = rs.getMetaData();
    int counter = 1;

    while (rs.next()) {

        //Prints column names
        if (counter <= 1) {

            out.println("<table border=5>");

            out.println("<thead>");

            for (; counter <= rsMD.getColumnCount(); counter++) {

                out.println("<th>" + rsMD.getColumnName(counter) + "</th>");

            }

            out.println("</thead>");
        }

        counter = 1;

        out.println("<tr>");

        // Prints row data
        for (; counter <= rsMD.getColumnCount(); counter++) {

            out.println("<td>" + rs.getString(counter) + "</td>");
        }

        out.println("</tr>");
    }

    out.println("</tbody>");
    out.println("</table>");

}

I would like to have something like this (pseudocode):

if (Button1.isPressed()){
     showDB(out, rs);
}

I do not know if I should be using a jsp or a servlet (Or both as the title suggests) to accomplish, so I will take any kind of response into account.

  • 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-23T10:20:03+00:00Added an answer on May 23, 2026 at 10:20 am

    A JSP is actually a Servlet written down in a different notation. The JSP code is compiled into Servlet code, which is compiled into a regular Servlet before it is used (who ever came up with this probably thought it was a good idea at the time).

    In this case I think you don’t need an additional JSP since you already have a Servlet. To do what you want you could create an HTML file (yourForm.html) with a form in it to allow entering a query and submitting it, something like:

    <form action="yourServlet" method="post">
        <input type="text" name="sql-query/>
        <input type="submit"/>
    </form>
    

    You should put this in the ‘document root’ directoy of your web application, then when your server is running you should be able to access the HTML file at something like http://localhost:8080/yourForm.html. This is assuming your web application context root is ‘/’ and port 8080, which is configured in your web server.

    In your web.xml you need something like:

    <servlet>
      <servlet-name>yourDbServlet</servlet-name>
      <servlet-class>your.app.YourDbServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
      <servlet-name>yourDbServlet</servlet-name>
      <url-pattern>/yourServlet</url-pattern>
    </servlet-mapping>
    

    Where ‘your.app.YourDbServlet’ is the fully-qualified classname of your servlet.

    After entering an sql query in the text field and clicking on the submit button, the form will be submitted to http://localhost:8080/yourServlet. I chose HTTP-POST (instead of HTTP-GET like another responder) because I think it’s more appropriate here – up for debate. This means you’d have to handle the request in your servlet’s doPost() method instead of doGet().

    Now before continuing: A WARNING NOTICE. Please realize that taking an SQL query from your client and firing it blindly at a database is A BIG SECURITY HAZARD. The query could delete or corrupt data, drop tables etc. depending on the database user’s access rights. For a real life system this is usually a BAD IDEA. Please also read about ‘SQL injection‘ to get an idea of the problem.

    To run the query against the database do something like this:

    String sqlQuery = getParameter("sql-query");
    PrintWriter out = response.getWriter();
    Connection conn = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost/yourdatabase";
        conn = DriverManager.getConnection(url, "username", "password");
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sqlQuery);
        showDB(out, rs);
    }
    finally {
        if (conn != null) {
            conn.close();
        }
    }
    

    Note that I didn’t catch any exceptions for brevity (look into that, exception handling is important). I did put conn.close() in the finally block to make sure an attempt is always made to close the database connection, even if an exception occurs in the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to figure out how to display static variables in the
I have been trying to figure out how to display a advertisement on a
I have been trying to display a table of results for weekly sales using
I am trying to display every odd instance of a table row, however there
I have been trying to display image from database using php but i am
I'm new to Flash/AS3 and I have been trying to display objects from class
I am trying to figure out how to display time in PST on my
I have been trying to play around with this for a long time and
I have been trying all I know in order to enable my site display
I am new to mysql and I have been pulling my hair out about

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.