问题遇到的现象和发生背景
利用德鲁伊连接数据库实现后端读取数据库数据到前端显示 无法读取数据并后台报错
问题相关代码,请勿粘贴截图
实体类
public class Student {
private Integer stuid;
private String stuname;
private Integer age;
private Integer sex;
public Student() {
}
public Student(Integer stuid, String stuname, Integer age, Integer sex) {
this.stuid = stuid;
this.stuname = stuname;
this.age = age;
this.sex = sex;
}
public Integer getStuid() {
return stuid;
}
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
dao层
public class StudentDaoImpl extends DruidUtil implements StudentDao {
@Override
public List<Student> getall() {
List list=new ArrayList();
Connection connection =null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement("select * from student");
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
Student student = new Student();
student.setStuid(resultSet.getInt("stuid"));
student.setStuname(resultSet.getString("stuname"));
student.setAge(resultSet.getInt("age"));
student.setSex(resultSet.getInt("sex"));
list.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(connection,preparedStatement,resultSet);
}
return list;
}
}
service
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
@Override
public List<Student> getall() {
return studentDao.getall();
}
}
servlet
@WebServlet(urlPatterns = "/getstus")
public class StudentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.接收请求参数
//2.调取service层方法
StudentServiceImpl studentService = new StudentServiceImpl();
List<Student> getall = studentService.getall();
//3.跳转页面
//后台传递数据给前台
req.setAttribute("stulist",getall);
req.getRequestDispatcher("/show.jsp").forward(req,resp);
}
}
util
public class DruidUtil {
private static DataSource ds;
static {
try {
Properties ppt = new Properties();
ppt.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
ds = DruidDataSourceFactory.createDataSource(ppt);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 从连接池中取出一个连接给用户
*
* @return
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
public static void close(Connection conn, Statement state, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
if (state != null) {
state.close();
}
} catch (Exception throwables) {
throwables.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
} catch (Exception throwables) {
throwables.printStackTrace();
}
}
}
配置文件
```java
url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username=root
password=LYSlys0428
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10
minIdle=5
maxWait=3000
jsp
```java
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="getstus">查询学生列表</a>
</body>
</html>
jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>show.jsp</h1>
<table border="1" width="500px" bgcolor="aqua">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
<td>sex</td>
</tr>
<c:forEach items="${stulist}" var="stu">
<tr>
<td>${stu.stuid}</td>
<td>${stu.stuname}</td>
<td>${stu.age}</td>
<td>${stu.sex==1?"男":"女"}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
代码结构
运行结果及报错内容
我的解答思路和尝试过的方法
前台只能出现样式,无法读取到数据
我想要达到的结果
点击超链,读取数据库数据并显示