找不到JDBC驱动程序(servlet,DAO,mariaDB)

时间:2018-05-01 15:51:24

标签: java jsp servlets jdbc mariadb

我有一项需要创建的练习:

  1. JSP - 显示servlet转发的数据
  2. 一个servlet - 从DAO类获取数据并转发到JSP
  3. DAO类 - 从mariaDB数据库连接和检索数据
  4. 问题是我无法在servlet中打开与数据库的连接。当我调试servlet文件时,它在创建新DAO对象的行(我在** ... **下面标记了行)中断了,在DAO文件中,它无法打开与消息的连接"没有合适的找到jdbc的驱动程序:mysql:// localhost:3306 / a1605354&#34 ;;即使我在构建路径中为mariaDB添加了jdbc驱动程序。

    我在帖子的末尾包含了我的项目浏览器的图片。我很困惑,并没有明确地解决问题。真的需要你的帮助!

    我有一个名为"连接参数"的文件。以及连接到mariaDB所需的所有信息:

    ConnectionParameter.java

    public class ConnectionParameters {
    
    public static final String username = "a1605354"; 
    public static final String password = "jaZEMs85o";
    
    
    private static final String databaseName = username;
    public static final String databaseURL = "jdbc:mysql://localhost:3306/" + databaseName;
    public static final String jdbcDriver = "org.mariadb.jdbc.Driver";
    
    // PK violation: error code in MariaDB is 1062
    public static final int PK_VIOLATION_ERROR = 1062;
    }
    

    StudentDAO.java

    public class StudentDAO {
    private final String username;
    private final String password;
    private final String databaseURL;
    
    public StudentDAO() throws Exception {
        username = ConnectionParameters.username;
        password = ConnectionParameters.password;
        databaseURL = ConnectionParameters.databaseURL;
    
        try {
            Class.forName(ConnectionParameters.jdbcDriver);
        } catch (Exception ex) {
            System.out.print(ex.getMessage());
        }
    }
    
    private Connection openConnection() throws SQLException {
        Connection dbConnection = DriverManager.getConnection(databaseURL, username, password);
        return dbConnection;
    }
    
    private void closeConnection(Connection dbConnection) throws SQLException {
        if (dbConnection != null) {
            dbConnection.close();
        }
    }
    
    public ArrayList<Student> getAllStudents() throws SQLException {
        ArrayList<Student> studentList = new ArrayList<Student>();
        Connection dbConnection = null;
    
        try {
            dbConnection = openConnection();
            String sqlText = "SELECT id, firstname, lastname, streetaddress, postcode, postoffice FROM Student ORDER BY lastname";
    
            Statement statement = dbConnection.createStatement();
    
            ResultSet resultSet = statement.executeQuery(sqlText);
    
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String firstname = resultSet.getString("firstname");
                String lastname = resultSet.getString("lastname");
                String streetaddress = resultSet.getString("streetaddress");
                String postcode = resultSet.getString("postcode");
                String postoffice = resultSet.getString("postoffice");
    
                studentList.add(new Student(id, firstname, lastname, streetaddress, postcode, postoffice));
            }
    
            return studentList;
        } catch (SQLException sqle) {
            throw sqle;
        } finally {
            closeConnection(dbConnection);
        }
    }
    

    StudentListServlet.java

    @WebServlet("/StudentListServlet")
    public class StudentListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        try {
            **StudentDAO studentDAO = new StudentDAO();**
            ArrayList<Student> studentList = studentDAO.getAllStudents();
    
    
            request.setAttribute("studentList", studentList);
            request.getRequestDispatcher("StudentList.jsp").forward(request, response);
    
        } catch (Exception ex) {
            ex.getMessage();
        }
    
      }
    
    }
    

    StudentList.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
    
    <body>
        <h2>List of Students</h2>
        <br/>
        <form action="StudentListServlet" method="GET"></form>
        <table>
            <thead>
                <tr>
                    <th>Student ID</th><th>Last Name</th><th>First Name</th><th>Street</th><th>Postcode</th><th>Post Office</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${ studentList }" var="studentListObject">
                    <tr>
                        <td><c:out value="${studentListObject.id }" /></td>
                        <td><c:out value="${studentListObject.lastname }" /></td>
                        <td><c:out value="${studentListObject.firstname }" /></td>
                        <td><c:out value="${studentListObject.streetaddress }" /></td>
                        <td><c:out value="${studentListObject.postcode }" /></td>
                        <td><c:out value="${studentListObject.postoffice }" /></td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>
    </body>
    

    项目资源管理器视图:

    enter image description here

0 个答案:

没有答案