我正在尝试将我在eclipse IDE中本地的java servlet程序连接到我在Web托管中创建的远程SQL数据库。
AdminLoginServlet -
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.Model;
public class Dao
{
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://xyz.com/devadmin_CustomerDatabasev1";
private static String username="USER";
private static String password="Password";
private static Connection con=null;
private static ResultSet rs=null;
public static Connection connect()
{
try
{
Class.forName(driver);
con=DriverManager.getConnection(url,username, password);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
return con;
}
public static ResultSet validate(Model m, String sql)
{
try
{
connect();
PreparedStatement ps=con.prepareStatement("select userid,password from adminlogin where userid='user' and password='password'");
rs=ps.executeQuery();
}
catch(SQLException e)
{
e.printStackTrace();
}
return rs;
}
}
我在UI上遇到以下错误的500错误代码 -
`HTTP Status 500 -type Exception report message description
The server encountered an internal error that prevented it from fulfilling this request.exception
java.lang.NullPointerException
dao.Dao.validate(Dao.java:40)
controller.AdminLoginServlet.doPost(AdminLoginServlet.java:41)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
注意Apache Tomcat / 7.0.47日志中提供了根本原因的完整堆栈跟踪。
Apache Tomcat / 7.0.47 ` 并在控制台上出现以下错误 -
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dao.Dao.connect(Dao.java:22)
at dao.Dao.validate(Dao.java:39)
at controller.AdminLoginServlet.doPost(AdminLoginServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [AdminLoginServlet] in context with path [/FirstApp] threw exception
java.lang.NullPointerException
at dao.Dao.validate(Dao.java:40)
at controller.AdminLoginServlet.doPost(AdminLoginServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我不知道如何继续或调试这段代码。提前感谢任何人都可以提供的指针。
编辑:(2017年2月26日) 我发现以下链接中缺少java到mysql的连接器: http://www.ccs.neu.edu/home/kathleen/classes/cs3200/JDBCtutorial.pdf
但是我无法将其作为外部存档添加到构建路径中。我正在获得以下选项 -
提前感谢任何人都可以提供的指针。
答案 0 :(得分:0)
您正在连接的数据库可能没有值,也检查列名称,或者您可能提供了错误的名称,或者您可能没有在WebContent文件夹下包含相应的jar文件。请仔细查看所有这些。< / p>