繁体   English   中英

连接MySQL的Servlet中未找到类异常

[英]Class Not found exception in servlet connecting mysql

我试图在我的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());
    }
}
}

任何人都请解释一下为什么我无法找到此类,为什么可以解决。 提前致谢。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM