当我尝试在 jsp 代码中使用 java 类从 sql 数据库获取信息时,连接返回 null,如下所示。请帮忙。
org.apache.jasper.JasperException: An exception occurred processing [product.jsp] at line [9]
6: <%@ page import="grocery.*, java.util.*, java.sql.*" %>
7: <%
8: manageProduct m = new manageProduct();
9: ArrayList<product> p = m.getAllProducts();
10: %>
11: <script src="https://kit.fontawesome.com/3000a67192.js" crossorigin="anonymous"></script>
12: </head>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:610)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:499)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
Root Cause
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "grocery.manageProduct.conn" is null
grocery.manageProduct.getAllProducts(manageProduct.java:33)
org.apache.jsp.product_jsp._jspService(product_jsp.java:130)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:466)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
这是我的jsp代码:
<html>
<head>
<link rel="stylesheet" href="style.css">
<title>MCSC Supermarket</title>
<link rel="shortcut icon" href="images/mcsc.png" type="image/x-icon">
<%@ page import="grocery.*, java.util.*, java.sql.*" %>
<%
manageProduct m = new manageProduct();
ArrayList<product> p = m.getAllProducts();
%>
<script src="https://kit.fontawesome.com/3000a67192.js" crossorigin="anonymous"></script>
</head>
<body>
<nav class="navigation">
<a href=index.jsp>
<img src="url" alt="MCSC Supermarket">
</a>
<ul>
<li><a href="index.jsp">Home</a></li>
<li><a href="product/jsp" class="active">Products</a></li>
<li><a href="#">About Us</a></li>
<li><a href="signin.jsp">Sign In</a></li>
</ul>
<form action=" " class="search-box">
<input type="text" class="search-text" name="Search" required>
<button class="search" type="submit">
<i class="fa-solid fa-magnifying-glass"></i>
</button>
</form>
<div class="right-nav">
<a href="#" class="cart">
<i class="fa-solid fa-cart-shopping"></i>
</a>
</div>
</nav>
<%
for(int i=0; i<p.size(); i++){
product a = p.get(i);
String detailsLink = "productDetails.jsp?id=" + a.productId;
%>
<table>
<tr>
<td> <img src="images/<%= a.imageURL %>" </td>
<td>
<h2> <%= a.name %> </h2>
Price: <%= a.price %>
Category: <%= a.category %> <br><br>
<a href="detailsLink"> Details </a>
<%
}
%>
</body>
</html>
这是我用于连接到数据库的 java 类:
package grocery;
import java.sql.*;
public class DBConnect {
protected static Connection conn = null;
public DBConnect() {
if(conn == null){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/grocery?autoReconnect=true&useSSL=false",
"root",
"mysql"
);
}
catch (Exception e) {
System.out.println("Database connection error"+ e.getMessage());
}
}
}//end constructor
public void closeConnection(){
try {
conn.close();
}
catch (SQLException e) { }
}//end closeConnection()
}
这是我的管理产品类:
package grocery;
import java.util.*;
import java.sql.*;
public class manageProduct extends DBConnect {
public void test(){
try{
System.out.println("Testing database connection....");
String query = "select * from Product";
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
if (rs.next()){
String id = rs.getString("ProductID");
String t = rs.getString("Name");
String d = rs.getString("Category");
System.out.println(id + " - " + t + " - " + d);
}
}
catch (SQLException e) {
System.out.println("ERROR - test()" + e.getMessage());
}
}
public ArrayList<product> getAllProducts(){
ArrayList<product> aList = new ArrayList<product>();
try{
String query = "Select * From product";
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
product p = new product();
p.productId = rs.getInt("ProductID");
p.name = rs.getString("Name");
p.category = rs.getString("Category");
p.price = rs.getDouble("Price");
p.quantity = rs.getInt("Quantity");
p.imageURL = rs.getString("ImageURL");
p.productType = rs.getString("ProductType");
p.brand = rs.getString("Brand");
p.minQuantity = rs.getInt("MinQuantity");
aList.add(p);
}
}
catch(SQLException e){
System.out.println("ERROR - getAllProducts(): " + e.getMessage());
}
return aList;
}
我在主类中测试了连接,它从数据库返回了数据。
错误似乎很清楚:
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "grocery.manageProduct.conn" is null
grocery.manageProduct.getAllProducts(manageProduct.java:33)
它甚至给出了这条线。
基本上连接对象
conn
还没有被初始化。