将MySQL JDBC驱动程序导入动态Web项目

时间:2017-04-02 19:26:33

标签: java jdbc

我在连接到基于Web(非本地)的MySQL数据库时遇到问题。我一直收到错误:在控制台中找不到合适的驱动程序jdbc:mysql:// host:port / database (我当然使用实际的主机)。网页只显示" null"。

我是Java新手所以使用this post 作为入门的参考。我在Eclipse中创建了一个动态Web项目,并在WebContent>下添加了JDBC连接器JAR文件。 WEB-INF> LIB。我完全迷失了为什么这会继续给我一个错误。

我将Tomcat 7用于Eclipse IDE。

过去几个小时我一直在谷歌搜索没有运气...请帮助!以下是我使用的代码:

Connect.java

package dbObjects;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class Connect {
    protected Connection getConnection() throws SQLException {
        String url = "jdbc:mysql://host:port/database";
        String username = "username";
        String password = "password";

        Connection conn = (Connection) DriverManager.getConnection(url, username, password);
        return conn;
    }

    protected ResultSet getResultSet(String sql) throws SQLException {
        Connection conn = getConnection();
        Statement st = (Statement) conn.createStatement();
        return st.executeQuery(sql);
    }
}

Employees.java

package dbObjects;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class Employees extends Connect {
    private String login;
    private String workgroup;
    private String csm;

    public Employees(String login, String workgroup, String csm) {
        super();
        this.login = login;
        this.workgroup = workgroup;
        this.csm = csm;
    }

    public Employees(String login) throws SQLException {
        super();
        this.login = login;
        setUserById(login);
    }

    private void setUserById(String login) throws SQLException {
        ResultSet resultSet = getResultSet("SELECT * FROM employeeData LIMIT 5");
        while(resultSet.next()) {
            System.out.println(resultSet.getString("login"));
            workgroup = resultSet.getString("workgroup");
            csm = resultSet.getString("csm");
        }
    }

    @Override
    public String toString() {
        return workgroup;
    }

}

我试图运行的servlet

package servlets;

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

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbObjects.Employees;

/**
 * Servlet implementation class HomeServlet
 */
@WebServlet(description = "Home page shown to user", urlPatterns = { "/HomeServlet" })
public class HomeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HomeServlet() {
        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
        Employees user = null;
        try {
            user = new Employees(null);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PrintWriter pw = response.getWriter();
        pw.println(user);
    }

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

}

我还创建了一个简单的连接测试类,它正在工作,没有返回任何合适的驱动程序错误。

import java.sql.DriverManager;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;

public class connTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String url = "jdbc:mysql://host:port/database";
    String username = "username";
    String password = "password";

        System.out.println("Connecting database...");

        try (Connection connection = (Connection) DriverManager.getConnection(url, username, password)) {
            System.out.println("Database connected!");
        } catch (SQLException e) {
            throw new IllegalStateException("Cannot connect the database!", e);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

问题出在Connect.java类中。我在DriverManager.registerDriver(new com.mysql.jdbc.Driver ());

之前添加了以下代码行Connection conn = (Connection) DriverManager.getConnection(url, username, password);

现在正在工作!