因此,我有一个jsp文件(ShowProducCatalog.jsp),它试图通过javabean (ProductDataBean.java)从mysql数据库中访问检索数据。我正在使用netbeans IDE。我已经通过从netbeans ids创建表并在mysql工作台中查看相同的表来确保服务器正在运行。但每次我启动我的应用程序时,我都会收到这个错误
org.apache.jasper.JasperException: An exception occurred processing JSP page /ShowProductCatalog.jsp at line 9
6:
7: <html>
8: <body>
9: <% List productList = data.getProductList();
10: Iterator prodListIterator = productList.iterator();
11: %>
12: 而根本原因来自于我的bean根本原因
java.lang.NullPointerException
cart.ProductDataBean.getProductList(ProductDataBean.java:36)
org.apache.jsp.ShowProductCatalog_jsp._jspService(ShowProductCatalog_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)这是我的ProductDataBean部分,其中的错误来自于:
public class ProductDataBean implements Serializable
{
private static Connection connection;
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();
Statement statement = connection.createStatement();//ERROR HERE
ResultSet results = statement.executeQuery("SELECT * FROM product");
while (results.next())
{...连接变量似乎从来没有初始化过,或者别的什么!但是我的用户名、密码和url都是正确的。请帮帮我!
发布于 2013-07-03 09:13:38
不,不是的。
String url = "jdbc:mysql://localhost/eshopdb";请参阅http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
这里假设您使用的是默认端口3306。
你不应该走这条路。JSP中的Scriptlet代码是1998年的老东西--早就不可信了。
您的ProductDataBean不应该获取数据库连接-至少不能以这种方式获取。连接池是一条可行的道路。
你似乎没有清理任何资源。此应用程序将在短时间内崩溃,因为您将耗尽数据库中的连接和游标。
https://stackoverflow.com/questions/17438250
复制相似问题