Servlet with JDBC [duplicate]

穿精又带淫゛_ 提交于 2021-02-17 07:11:30

问题


I have a problem with my progect. Files of progect:

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;
    }
}

and 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();
        }
    }
}

I was read some solutiotuns, but it doesn't help. The problem in two exceptions:

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

I try to add:

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

to my code, and add mysql connector jar to my project, but it throws exception:

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

Second 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

Here is my pom.xml file:

<?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:

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

and project structure: [project structure][1]

Project structure - artifacts

In project structure -> libraries i have all jars.


回答1:


Since you are using IntelliJ I believe you might need to add the libraries to the artifact because from my experience Intellij adds the maven dependencies to the Classpath but not to the artifact.

Make sure you go to File -> Project Structure -> Artifacts and then add all the libraries from the available side to the artifact.

But you need to register the driver before getting the connection otherwise it doesn't work either way :

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

Hope this helps.



来源:https://stackoverflow.com/questions/35816418/servlet-with-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!