此问题已经在这里有了答案:
我是JEE的初学者,我试图从数据库中获取对象,但是遇到了nullpointerexception错误,我试图通过System查找nullpointerexception的起源(我知道该连接以null传递)。 out.println()但我失败了,我忘了告诉你我有用于连接的类singletonconnection,我通过.getconnection()在函数中调用它,我还向项目构建路径添加了jdbc驱动程序(版本8.0.18) (我问这个错误是否可以归因于jdbc驱动程序版本?),如果有人可以提供帮助,请先感谢。
这里是连接到数据库并将对象带到我在PreparedStatement ps = connection.prepareStatement(“ req”)]中告诉过我的错误的位置的函数>
System.out.println("connection"); Connection connection=SingletonConnection.getConnection(); System.out.println(connection); try { PreparedStatement ps=connection.prepareStatement("SELECT * FROM PRODUITS WHERE DESIGNATION LIKE ?"); ps.setString(1, mc); ResultSet rs=ps.executeQuery();
这里是错误日志
HTTP Status 500 – Internal Server Error Type Exception Report Description The server encountered an unexpected condition that prevented it from fulfilling the request. Exception java.lang.NullPointerException dao.ProduitDaoImpl.produitsParMC(ProduitDaoImpl.java:48) web.ControleurServelet.doGet(ControleurServelet.java:49) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) Note The full stack trace of the root cause is available in the server logs.
这是我的单身人士联系
public class SingletonConnection {
private static Connection connection;
//le block static charge la classe en mémoire lors de son appel
static{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
try {
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/db-natal","root","");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
我是JEE的初学者,我正在尝试从数据库中获取对象,但是遇到了nullpointerexception错误,我试图找到nullpointerexception的来源(我知道...
mysql-connector-java