连接mysql的servlet中的类未找到异常

时间:2013-03-13 14:20:20

标签: java mysql servlets classnotfoundexception

我试图在我的servlet中连接mysql数据库。然后我得到一个异常。
但是当我从一个普通的Java类测试数据库连接时,连接是正常的,我从数据库中获取数据。
我在tomcat上获得的异常服务器控制台是: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
DatabaseHandler.java的代码是:

import java.sql.*;
public class DatabaseHandler {
private Statement stmt;
private Connection con;
String username = "root";
String password = "thunder";
String dbname = "bidderboy";
public DatabaseHandler()
{
    this.createConnection();
}

private void createConnection()
{
    //create the connection for first time
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:mysql://localhost/"+dbname , username , password);
        stmt = con.createStatement();

    } catch (Exception ex) {
        System.out.println(ex.toString());
    }
}
public int executeUpdate(String sql)
{
    int result = 0;

    //before update checks if connection is open
    try {
        if(con.isClosed()) {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost/"+dbname , username , password);
            stmt = con.createStatement();
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }

    //try to executeUpdate the sql command
    try{
        result = stmt.executeUpdate(sql);
    }
    catch(Exception ex){
        System.out.println("Couldn't executeUpdate sql command");
    }
    return result;
}

public ResultSet executeQuery(String sql)
{
    ResultSet rs = null;
    //before Query checks if connection is open
    try {
        if(con.isClosed()) {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+dbname , username , password);
            stmt = con.createStatement();
        }
    } catch (Exception ex) {
        System.out.println(ex.toString());
    }

    //try to executeQuery the sql command
    try{
        rs = stmt.executeQuery(sql);
    }
    catch(Exception ex){
        System.out.println("Couldn't executeQuery sql command");
    }
    return rs;
}

public void closeConnection()
{
    //if connection is open try to close the connection
    try {
        if(!con.isClosed()) {
            con.close();
        }
    } catch (SQLException ex) {
        System.out.println("Failed to close database connection");
    }
}
}

我的servlet的代码是:

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

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


@SuppressWarnings("serial")
public class UserLogin extends HttpServlet{

public UserLogin()
{

}

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    DatabaseHandler dbh = new DatabaseHandler();
    String username="mamun";
    String password="1234";

    String dbusername="";
    String dbpassword="";

    String sql = "select * from users where username='"+username+"' and password='"+password+"'";
    ResultSet rs = dbh.executeQuery(sql);
    try {
        while(rs.next())
        {
            dbusername = rs.getNString(1);
            dbpassword = rs.getNString(2);
        }
    } catch (SQLException e) {}
    System.out.println(dbusername+" "+dbpassword);
}
}

我从中获取数据的普通java类的代码是:

import java.sql.ResultSet;
import java.sql.SQLException;

public class DatabaseConnectionTest {

public static void main(String[] args) {
    String sql = "Select * from users";
    DatabaseHandler dbh = new DatabaseHandler();

    ResultSet rs = dbh.executeQuery(sql);

    try {
        while(rs.next())
        {
            String n = rs.getNString(1);
            String c = rs.getNString(2);
            System.out.println("Name: "+n+" Contact: "+c);
        }
    } catch (SQLException ex) {
        System.out.println(ex.toString());
    }
}
}

任何人请解释为什么我得到这个类没有找到异常,什么可以解决。提前谢谢。

1 个答案:

答案 0 :(得分:1)

mysql-connector-java-5.1.11-bin.jar文件添加到WEB-INF/lib文件夹修复了我的问题。