B. Pal B. Pal - 1 year ago 105
SQL Question

Simple Java Web Application: Table Not Loading On Startup

I'm developing a simple application with Eclipse and Apache in which i display a table created with MySQL pertaining to products (with columns id, name and price) using Java, JSP and HTML. I also have a text box with a button which allows to filter the table showing only the products which have a bigger price than the one inserted on the text box.

Everything seems to be working fine including the filter. However, when i run the application for the first time, the full table isn't displayed like it should. It only displays when i use the filter. I'm not sure but there seems to be some conflict between the scriptlet for the filter elements and the table because if i comment the scriptlet for the filter elements in the JSP file, the table loads on startup like it should.

This is the code for JSP class which loads the table and the search elements (with some Bootstrap styling):

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.sql.Connection,java.sql.ResultSet,java.sql.DriverManager,project1.components.DBConnector, project1.components.Filter"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<style type="text/css">
<%@include file="bootstrap/css/bootstrap.css" %>
<%@include file="bootstrap/css/bootstrap-theme.css" %>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="bootstrap/js/jquery.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
DBConnector dbc = new DBConnector();
Connection conn = dbc.connect();
String query = "select * from product";

<form method="post">

<div class="col-xs-2 col-xs-offset-5">
<input type="text" class="form-control" name="value" value=<%=request.getParameter("firstinput")%>>
<input type="submit" class="form-control" name="submit" value="Filter">
String val = request.getParameter("value");
Filter ft = new Filter();
query = ft.filterByValue(val);
<table class = 'table table-hover'>
ResultSet rs = dbc.obtainTable(query);


<td><%=rs.getInt("ID") %></td>
<td><%=rs.getString("NAME") %></td>
<td><%=rs.getFloat("PRICE") %></td>
</tr> <%

catch(Exception e)




The Java class which connects to the database and obtains data with a given query to be loaded on the table defined in the previous JSP file:

package project1.components;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class DBConnector {

String url = "jdbc:mysql://localhost:3306/products?autoReconnect=true&useSSL=false";
String user = "root";
String pass = "root";

public Connection connect() throws ClassNotFoundException, SQLException{
return (Connection) DriverManager.getConnection(url,user,pass);

public ResultSet obtainTable(String query) throws ClassNotFoundException, SQLException{
Connection cn = this.connect();
Statement stmt= (Statement) cn.createStatement();
return stmt.executeQuery(query);

And finally, the filter itself defined in Java:

package project1.components;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Filter {

public String filterByValue(String val) throws ClassNotFoundException, SQLException{
int num = Integer.parseInt(val);
String query = null;
if(num >= 0){
query = "select * from product where price >" + val;
query = "select * from product";
return query;

Any help would be really appreciated.

Answer Source

Could you please replace the following:



if(request.getParameter("value")!=null && !request.getParameter("value").trim().isEmpty()){

and see the results?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download