繁体   English   中英

Java MVC JSP从JavaBean获取连接和数据

[英]Java MVC JSP get Connection and Data from JavaBean

我正在尝试从JavaBean ProductDataBean.java获取ShowProductCatalog.jsp中List的值(List productList = data.getProductList();)。 我收到错误nullpointerexception。 我不知道怎么了。 请帮忙!

      **ShowProductCatalog.jsp**


    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
     pageEncoding="ISO-8859-1"%>
     <%@ page import = "java.util.*" import="cart.*,java.net.*,java.text.*"%>
     <jsp:useBean id="data" scope="session" class="cart.ProductDataBean" />

      <!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">
</head>
<body>
    <%
        List productList = data.getProductList();
        Iterator prodListIterator = productList.iterator();
        %>



      **ProductDataBean.java**


package cart;
import java.io.*;
import java.sql.*;
import java.util.*;
public class ProductDataBean implements Serializable{

    private static Connection connection;
    private PreparedStatement addRecord, getRecords;

    public ProductDataBean(){
        try{
            // Step1: Load JDBC Driver
            Class.forName("com.mysql.jdbc.Driver");
            // Step 2: Define Connection URL
            String connURL ="jdbc:mysql://localhost/onlineshop?user=root&password=teck1577130713"; 
            // Step 3: Establish connection to URL
            connection =   DriverManager.getConnection(connURL);
        }catch(Exception e){e.printStackTrace();}
    }
    public static Connection getConnection(){
        return connection;
    }
    public ArrayList getProductList() throws SQLException{
        ArrayList productList = new ArrayList();
        Statement statement = connection.createStatement();
        ResultSet results = statement.executeQuery("SELECT * FROM products");
        while (results.next()){
            DVD movie = new DVD();
            movie.setMovie(results.getString("movieName"));
            movie.setRating(results.getString("movieRate"));
            movie.setYear(results.getString("movieYear"));
            movie.setPrice(results.getDouble("moviePrice"));
            productList.add(movie);
        }
        return productList;
    }
}
  1. 您必须检查条件if(productList!= null && productList.size()> 0)是否为null和jsp中的大小,然后填充该值。然后将类型从ArrayList更改为ArrayList DVD,

  2. 您必须在请求中设置所有值,包括请求中的ArrayList值,然后servlet Java代码添加以下代码request.getRequestDispatcher(“ ShowProductCatalog.jsp”)。forward(request,response); 如果在此答案下方有任何问题要发表评论。

    public ArrayList getProductList()抛出SQLException {ArrayList productList = new ArrayList(); 语句语句= connection.createStatement(); ResultSet结果= statement.executeQuery(“ SELECT * FROM产品”);

      while (results.next()){ DVD movie = new DVD(); movie.setMovie(results.getString("movieName")); movie.setRating(results.getString("movieRate")); movie.setYear(results.getString("movieYear")); movie.setPrice(results.getDouble("moviePrice")); productList.add(movie); } return productList; } 

暂无
暂无

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

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