am trying to load my sql jdbc driver with the use

2019-09-08 23:44发布

问题:

This question already has an answer here:

  • I am trying to load or u can say register my sql jdbc driver with the use of ServletConfig interface 2 answers

I am able to load my my sql jdbc driver from the other ways but when am trying to register it with the use of servletconfig interface it gives an exception: java.lang.ClassNotFoundException: Though I know the concept of servletconfig interface and trying to do it by following the concept but am failed.

Here is my web.xml code

<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>

here is my java file in my java class it gives an warning The value of the local variable driver is not used.

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();}

}

}

and here is the exception which i got

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)

回答1:

Unquote driver in Class.forName call. You want the value of the driver variable, not the string "driver".