0

当我尝试从 javabean 连接到 mysql 数据库时出现 NullPointerException

org.apache.jasper.JasperException: An exception occurred processing JSP page     /ShowProductCatalog.jsp at line 9
<jsp:useBean id= "data" class= "cart.ProductDataBean" scope="request"/>

<html>
    <body>
8:     <body>
9:         <%  List productList = data.getProductList();
10:             Iterator prodListIterator = productList.iterator();
11:           %> 

根本原因

java.lang.NullPointerException
    cart.ProductDataBean.getProductList(ProductDataBean.java:36)

ProductDataBean.java

    public ProductDataBean()
    {
        try
        {
                String userName = "root";
                String password = "root";
                String url = "jdbc:mysql://localhost:/eshopdb";
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        connection = DriverManager.getConnection(url,userName,password);
                System.out.println("Database connection established");
        }catch(Exception e){e.printStackTrace();}
    }

    public static Connection getConnection()
    {
        return connection;
    }



public ArrayList getProductList() throws SQLException
        {
            ArrayList productList = new ArrayList();
/**********************HERE IS LINE 36. ERROR HERE*******************************/
            Statement statement = connection.createStatement();//ERROR HERE
            ResultSet results = statement.executeQuery("SELECT * FROM product");

        Help
4

1 回答 1

0

connection在分配给它之后查看是否为空。(您打印“已建立数据库连接”)我怀疑它是,并且您真的想调试该DriverManager.getConnection调用。另外,你确定这catch(Exception e){e.printStackTrace();}不是开枪吗?

于 2013-07-03T04:01:51.163 回答