我试图加载或你可以说使用ServletConfig接口注册我的sql jdbc驱动程序 [英] I am trying to load or u can say register my sql jdbc driver with the use of ServletConfig interface

查看:113
本文介绍了我试图加载或你可以说使用ServletConfig接口注册我的sql jdbc驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过其他方式加载我的mysql jdbc驱动程序,但是当我尝试使用servletconfig接口注册它时会出现异常

I am able to load my mysql jdbc driver from the other ways but when I am trying to register it with the use of servletconfig interface it gives an exception


java.lang.ClassNotFoundException:drive

java.lang.ClassNotFoundException: drive

虽然我知道servletconfig接口的概念并试图通过遵循这个概念却失败了。
这是我的代码和我用过的其他东西。

Though I know the concept of servletconfig interface and trying to do it by following the concept but failed. Here's my code and other stuff I used.

这里是我的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();}

}

}


推荐答案

错误是因为没有课driver正确的句子是:

The error it's because there is no class "driver" the correct sentence is :

Class.forName("com.mysql.jdbc.Driver"); 

Class.forName( config.getInitParameter("driver") ); 

但实际上这不是访问servlet中数据库的正确方法,请参阅:的Class.forName(QUOT; com.mysql.jdbc.Driver")。的newInstance()

But actually this isn't a correct way to access a database in a servlet see : Class.forName("com.mysql.jdbc.Driver").newInstance()

这篇关于我试图加载或你可以说使用ServletConfig接口注册我的sql jdbc驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆