当我尝试从 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