带有JDBC的Servlet [英] Servlet with JDBC
问题描述
我的程序有问题. Progect文件:
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;
}
}
和Servlet: HousesBaseServlet
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 excreprion java.sql.SQLException:找不到适合的驱动程序 jdbc:mysql://localhost:3306/mydb2
SQL exceprion java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/mydb2
我尝试添加:
Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver");
在我的代码中,然后将mysql连接器jar添加到我的项目中,但是会引发异常:
to my code, and add mysql connector jar to my project, but it throws exception:
SQL异常java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
SQL exception java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
第二个例外:
JasperException:绝对uri: http://java.sun.com/jsp/jstl/核心 无法在web.xml或通过以下方式部署的jar文件中解析 此应用程序无法与基础连接
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文件:
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:
JSP taglib:
%@ taglib prefix ="c" uri ="http://java.sun.com/jsp/jstl/core"%
%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %
和项目结构: [项目结构] [1]
and project structure: [project structure][1]
在项目结构->库中,我有所有的jar.
In project structure -> libraries i have all jars.
推荐答案
由于您使用的是IntelliJ,我认为您可能需要将库添加到工件中,因为根据我的经验,Intellij会将maven依赖项添加到Classpath中,而不是添加到人工产物.
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);
希望这会有所帮助.
这篇关于带有JDBC的Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!