java servlet jdbc,带有JDBC的Servlet

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 getList() {

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

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">

4.0.0

javax.servlet

javax.servlet-api

3.1.0

mysql

mysql-connector-java

5.1.35

jstl

jstl

1.2

,

JSP taglib:

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

and project structure:

[project structure][1]

In project structure -> libraries i have all jars.

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值