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.
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:
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:
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:
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.