当前位置:网站首页>JDBC连接数据库读取前台无法显示数据
JDBC连接数据库读取前台无法显示数据
2022-07-27 15:43:00 【CSDN问答】
问题遇到的现象和发生背景
利用德鲁伊连接数据库实现后端读取数据库数据到前端显示 无法读取数据并后台报错
问题相关代码,请勿粘贴截图
实体类
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(); } }}配置文件```javaurl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8username=rootpassword=LYSlys0428driverClassName=com.mysql.jdbc.DriverinitialSize=5maxActive=10minIdle=5maxWait=3000jsp```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>代码结构

运行结果及报错内容


我的解答思路和尝试过的方法
前台只能出现样式,无法读取到数据

我想要达到的结果
点击超链,读取数据库数据并显示
边栏推荐
- Hidden iframe design details of SAP ui5 fileuploader
- Database hyperphone (4)
- Three table joint query 2
- 阿里巴巴鹰眼系统简介
- 【数据库系统概论(王珊)】第5章——数据库完整性
- How does vs2019 C language run multiple projects at the same time, how to add multiple source files containing main functions in a project and debug and run them respectively
- 信号量保护之位带操作
- Day07 operation
- 笔试缺考者入围教师招聘面试?河南祥符:个别考生成绩统计错误
- 苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条...
猜你喜欢

每条你收藏的资讯背后,都离不开TA

Design details of hidden iframe and form elements used by SAP ui5 fileuploader

About paths mapping in SAP ui5 application ui5.yaml

Two table joint query 1

深度学习能颠覆视频编解码吗?国家技术发明奖一等奖得主在小红书给你唠

20年前,他是马云最大的敌人

【单片机】2.3 AT89S52的CPU

The chess robot broke the finger of a 7-year-old boy. Netizen: it violated the first law of robots

Windows and network foundation-15-local security policy

Three table joint query 3
随机推荐
【obs】NewSocketLoopEnable 网络优化
如何开发一款在线Excel表格系统(上)
DDD (Domain Driven Design) layered architecture
立创EDA——原理图的布局与检查(三)
Day07 operation
#夏日挑战赛#【FFH】实时聊天室之WebSocket实战
Branch loop statement of C language
小于n的最大数
[OBS] newsocketloopenable network optimization
下棋机器人折断7岁男孩手指,网友:违反了机器人第一定律
Gods at dusk, "cat trembles" bid farewell to the big V Era
笔试缺考者入围教师招聘面试?河南祥符:个别考生成绩统计错误
#yyds干货盘点# 面试必刷TOP101:链表内指定区间反转
泰山OFFICE技术讲座:WORD奇怪的段落边框
Redis: configuring AOF does not work
国产新冠口服药为什么是治艾滋病的药
Database hyperphone (II)
微软默默给 curl 捐赠一万美元,半年后才通知
格力「不清凉」:巨头诉讼落幕又遭大经销商减持,空调新战场还晚人一步?
Can deep learning overturn video codec? The first prize winner of the National Technological Invention Award nags you in the little red book