java - 使用 JDBC 的 Servlet

标签 java mysql jsp servlets jdbc

我的项目有问题。项目文件:

House.class

 public class House implements Serializable {

    //properties -------------------------------------------------------------
    private String price;
    private String square;
    private String RoomNumbers;
    //------------------------------------------------------------------------

    //getters - settersm Object overriding.... -----------------------------

HouseDAO.class

public class HouseDAO {

    Connection connection;
    final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb2";
    final String DB_USER = "root";
    final String DB_PASSWORD = "root";

    public HouseDAO(Connection connection) {
        this.connection = connection;
    }

    public List<House> getList() {
       List<House> houses = new ArrayList<>();
        try {

            connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
            System.out.println("Connection available");
            PreparedStatement ps = connection.prepareStatement("SELECT Square, RoomNumbers, Price FROM houses  WHERE  District = 'Dnepr'");
            ResultSet rs = ps.executeQuery();
              while (rs.next()){
                 House house = new House();
                  house.setSquare(rs.getString("Square"));
                  house.setRoomNumbers(rs.getString("RoomNumbers"));
                  house.setPrice(rs.getString("Price"));
                    houses.add(house);
              }

        }catch (Exception ex){
            System.out.println("SQL exceprion");
            ex.printStackTrace();

        }
           return houses;
    }
}

和 Servlet: HousesBaseServlet

@WebServlet("/post")
public class HosesBaseServlet extends HttpServlet {
    Connection conn;

    private HouseDAO houseDAO;
    @Override
    public void init(){
        houseDAO = new HouseDAO(conn);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        //choice from html form
      //  String choice = request.getParameter("district");
        try {
            List<House> houses = houseDAO.getList();

            request.setAttribute("houses", houses);
            request.getRequestDispatcher("/houses.jsp").forward(request,response);


        }catch (Exception ex ) {
            System.out.println("Fail to connect with base");
             ex.printStackTrace();
        }
    }
}

我读过一些解决方案,但没有帮助。问题有两个异常(exception):

SQL exceprion java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydb2

我尝试添加:

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

到我的代码,并将mysql连接器jar添加到我的项目中,但它抛出异常:

SQL exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

第二个异常(exception):

JasperException: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application Fail to connect with base

这是我的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>


    </dependencies>

JSP 标签库:

%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %

和项目结构: [项目结构][1]

Project structure - artifacts

在项目结构 -> 库中,我拥有所有 jar。

最佳答案

由于您使用的是 IntelliJ,我相信您可能需要将库添加到工件中,因为根据我的经验,IntelliJ 将 Maven 依赖项添加到类路径中,而不是添加到工件中。

确保转到"file"->“项目结构”->“工件”,然后将可用端的所有库添加到工件中。

但是您需要在获得连接之前注册驱动程序,否则无论如何都不起作用:

Class.forName("com.mysql.jdbc.Driver");     
connection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

希望这有帮助。

关于java - 使用 JDBC 的 Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62135132/

相关文章:

MySQL-如何获取行号

mysql - LOCK IN SHARE MODE 是否与 COUNT 一起使用

java - 在 Controller 和服务之间共享相同的方法前置条件逻辑?

java - 如何修复 JMX 中的 java 序列化漏洞?

java ldap 连接池超时属性未按预期工作

java - 创建目录以存储文件时出错

mysql - 查询中的累计金额

java - 我无法覆盖 Tomcat 的默认欢迎文件列表

java - 为Java Web应用程序中的所有jsp设置默认字符编码

jsp - 使用 Servlets 和 JSP 从 html 表中获取选定的行