从数据库中的三个不同表中选择数据到jsp故事中

时间:2015-08-20 14:25:23

标签: mysql jsp

我试图将数据库数据加载到表中,但每次(LEFTJOIN,JOIN)我这样做我得到空指针错误,我已经尝试了LEFT JOIN,JOIN和WHERE子句(没有JOIN)但是相同的查询一直都是错误的。我是否需要使用coloumn名称

rs.getString("p.Name")? Since the Name colomn is in Products table

这是我正在做的事情,请让我知道我错在哪里

---在JSP ---

     <table class="table table-striped">
                    <thead>
                        <tr>
                            <th class="col-sm-1">Name</th>
                            <th class="col-sm-2">Price</th>
                            <th class="col-sm-2">Shipping Address</th>
                            <th class="col-sm-2">Order Date</th>
                            <th class="col-sm-2">Discount</th>
                            <th class="col-sm-1">Sales Tax</th>
                            <th class="col-sm-2">Quantity</th>
                            <th class="col-sm-2">Final Amount</th>
                            <th class="col-sm-2">Payment Status</th>
                            <th class="col-sm-2">User</th>

                        </tr>
                    </thead>
                    <tbody align="center">
                    <%ProductDAO s = new ProductDAO();
                    Sales_Details pr[] = s.getSalesDetails(); %>
                    <% int i=0;
                    while(i>pr.length){ %>
                        <tr>
                            <td class="col-sm-1"><%out.print(pr[i].getName());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getPrice());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getSales_Address());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getOrder_Date());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getDiscount());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getSales_Tax());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getQuantity());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getFinal_Amount());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getPayment_Status());%></td>
                            <td class="col-sm-2"><%out.print(pr[i].getUser());%></td>
                            <td class="col-sm-2"> <a href="ViewProducts.jsp?value=<%out.print(pr[i].getName());%>"  class="btn btn-default">delete</a></td>

                        </tr>
                      <% i++; } %>
                    </tbody>
                </table>

---在DAO ---

public Sales_Details[] getSalesDetails()
{
    //List<String> obj = new ArrayList<String>();
    String query = "SELECT s.*,d.*,p.* FROM sales_order s JOIN sales_order_details d on s.Order_No = d.Order_No JOIN products p on d.Products_ID = p.ID";

    Sales_Details[] sd = new Sales_Details[500];

    int i = 0;

    try
    {
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next())
        {
            sd[i].setName(rs.getString("Product").toString());
            sd[i].setPrice(rs.getString("Price").toString().toString());
            sd[i].setSales_Address(rs.getString("Sales_Address").toString());
            sd[i].setOrder_Date(rs.getString("Order_Date").toString());
            sd[i].setDiscount(rs.getString("Discount").toString());
            sd[i].setSales_Tax(rs.getString("Sales_Tax").toString());
            sd[i].setQuantity(rs.getString("Quantity").toString());
            sd[i].setFinal_Amount(rs.getString("Final_Amount").toString());
            sd[i].setPayment_Status(rs.getString("Payment_Status").toString());
            sd[i].setUser(rs.getString("User").toString());

            i++;
        }
    }
    catch(SQLException e)
    {
        System.out.println("Problem in Query");
        e.printStackTrace();
    }

    return sd;
}

--- --- Sales_Details

public class Sales_Details {

private String Name;
private String Price;
private String Sales_Address;
private String Order_Date;
private String Discount;
private String Sales_Tax;
private String Quantity;
private String Final_Amount;
private String Payment_Status;
private String User;

//Getters, Setters and Constructors

栈跟踪

    Aug 20, 2015 7:17:07 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [Servlets.Login] in context with path [/Final_Project] threw exception [java.lang.NullPointerException] with root cause
    java.lang.NullPointerException
at DAO.ProductDAO.getSalesDetails(ProductDAO.java:151)
at org.apache.jsp.admin_jsp._jspService(admin_jsp.java:301)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
at Servlets.Login.doPost(Login.java:71)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

下面:

<%ProductDAO s = new ProductDAO();
Sales_Details pr[] = s.getSalesDetails(); %>
<% int i=0;
while(i>pr.length){ %>

这种情况while(i>pr.length)什么时候会成真?

另一点:
检查数据库中的查询。它是否返回任何数据?
在以html显示之前,请检查Sales_details数组的大小。如果它有数据,则处理显示数据的逻辑。否则,显示相应的消息。

编辑:

检查/ WEB-INF / lib是否包含jsp-api - * .jar等内容。如果是,则删除,重建,部署然后测试。

答案 1 :(得分:0)

谢谢大家,问题确实是我的病情错了

         while(i>pr.length)

并且数组没有初始化,现在就开始工作了。