gpt4 book ai didi

java - 我正在尝试使用 ServletConfig 接口(interface)加载我的 sql jdbc 驱动程序

转载 作者:行者123 更新时间:2023-12-01 11:16:44 25 4
gpt4 key购买 nike

我可以从其他方式加载我的 sql jdbc 驱动程序,但是当我尝试使用 servletconfig 接口(interface)注册它时,它给出了一个异常:java.lang.ClassNotFoundException:虽然我知道 servletconfig 接口(interface)的概念并尝试遵循该概念来做到这一点,但失败了。

这是我的 web.xml 代码

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>newuser</servlet-name>
<servlet-class>com.rr.org.newuser</servlet-class>



</servlet>
<servlet-mapping>
<servlet-name>newuser</servlet-name>
<url-pattern>/newuser</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>Validation</display-name>
<servlet-name>Validation</servlet-name>
<servlet-class>com.pi.org.Validation</servlet-class>

<init-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>



</servlet>
<servlet-mapping>
<servlet-name>Validation</servlet-name>
<url-pattern>/Validation</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>inbox</display-name>
<servlet-name>inbox</servlet-name>
<servlet-class>com.qq.org.inbox</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>inbox</servlet-name>
<url-pattern>/inbox</url-pattern>
</servlet-mapping>
</web-app>

这是我的java文件在我的 java 类中,它给出了一个警告:未使用局部变量驱动程序的值。

package com.pi.org;

import java.io.IOException;

import javax.sql.*;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
* Servlet implementation class Validation
*/
public class Validation extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Validation() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Boolean valid= false;
PrintWriter out = response.getWriter();
String username= request.getParameter("user");
String password= request.getParameter("pass");



try{
ServletConfig config = getServletConfig();
String driver = config.getInitParameter("driver");
Class.forName("driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdata","root","annie1993");

PreparedStatement ps= conn.prepareStatement("select * from emp ");
ResultSet rs = ps.executeQuery();

while(rs.next()){
if(username.equals(rs.getString(1))&& password.equals(rs.getString(2)))
{
valid= true;
}
}

if(valid)
{
request.setAttribute("user", username);
RequestDispatcher rd = request.getRequestDispatcher("inbox");
rd.forward(request, response);

}

else{
RequestDispatcher rd = request.getRequestDispatcher("/index");
rd.include(request, response);
out.print("<html><body>either username or password is incorrect<body><html>");


}

}catch(Exception e){e.printStackTrace();}

}

}

这是我得到的异常(exception)

java.lang.ClassNotFoundException: driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at com.pi.org.Validation.doPost(Validation.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

最佳答案

在 Class.forName 调用中取消引用驱动程序。您需要驱动程序变量的值,而不是字符串“driver”。

关于java - 我正在尝试使用 ServletConfig 接口(interface)加载我的 sql jdbc 驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31736269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com