简体   繁体   English

简单的Java Web应用程序:启动时未加载表

[英]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. 我正在使用Eclipse和Apache开发一个简单的应用程序,其中显示一个表,该表是使用Java,JSP和HTML用MySQL创建的与产品相关的表(列ID,名称和价格)。 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. 我不确定,但是过滤器元素的脚本和表之间似乎存在一些冲突,因为如果我在JSP文件中注释过滤器元素的脚本,表将在启动时像应加载的那样加载。

This is the code for JSP class which loads the table and the search elements (with some Bootstrap styling): 这是用于JSP类的代码,该类加载表和搜索元素(具有某些Bootstrap样式):

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Product</title>
<style type="text/css">
    <%@include file="bootstrap/css/bootstrap.css" %>
    <%@include file="bootstrap/css/bootstrap-theme.css" %>
</style>
<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>
</head>
<body>
<% 
DBConnector dbc = new DBConnector();
Connection conn = dbc.connect();
String query = "select * from product";
%>
</body>

<form method="post">

<div class="col-xs-2 col-xs-offset-5">
<input type="text" class="form-control" name="value" value=<%=request.getParameter("firstinput")%>>
<br><br>
<input type="submit" class="form-control" name="submit" value="Filter">
<% 
    if(!request.getParameter("value").isEmpty()){
        String val = request.getParameter("value");
        Filter ft = new Filter();
        query = ft.filterByValue(val);
    }
%>
</div>
<br></br>
<table class = 'table table-hover'>
<tr>
<td>Id</td>
<td>Name</td>
<td>Product</td>
</tr>
<%
try
{               
    ResultSet rs = dbc.obtainTable(query);

    while(rs.next())
{

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

}
%>
    </table>
    <%
    rs.close();
    conn.close();
    }
catch(Exception e)
{
    e.printStackTrace();
    }




%>

</form>

</html>

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: 连接到数据库并通过给定查询获取数据的Java类,将其加载到上一个JSP文件中定义的表上:

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{
        Class.forName("com.mysql.jdbc.Driver");
        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: 最后,过滤器本身在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;
        }
        else{
            query = "select * from product";
        }
        return query;
    }
}

Any help would be really appreciated. 任何帮助将非常感激。

Could you please replace the following: 您能否替换以下内容:

if(!request.getParameter("value").isEmpty()){

by: 通过:

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

and see the results? 看看结果吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM