当前位置:网站首页>JDBC connection database reading foreground cannot display data
JDBC connection database reading foreground cannot display data
2022-07-27 17:52:00 【CSDN Q & A】
The phenomenon and background of the problem
Using the druid to connect to the database, the back-end reads the database data to the front-end display Unable to read data and background error
Problem related code , Do not paste screenshots
Entity class
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 layer
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. Receive request parameters //2. Transfer service Layer method StudentServiceImpl studentService = new StudentServiceImpl(); List<Student> getall = studentService.getall(); //3. Jump to the page // The background transfers data to the foreground 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(); } } /** * Take a connection from the connection pool to the user * * @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(); } }} The configuration file ```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"> Look up the list of students </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?" male ":" Woman "}</td> </tr> </c:forEach></table></body></html>The code structure

Operation results and error reporting contents


My solution ideas and tried methods
Only styles can appear in the foreground , Unable to read data

What I want to achieve
Click hyperlink , Read the database data and display
边栏推荐
- JSP自定义标签(下)
- Likeshop takeout ordering system "100% open source without encryption"
- VO、DO、DTO、PO是什么
- MLX90640 红外热成像仪测温传感器模块开发笔记(七)
- Switch and router technology-02-working principle of Ethernet switch
- 20年前,他是马云最大的敌人
- DDD (Domain Driven Design) layered architecture
- (2) CBAM integrated two stream project construction - data preparation
- 成本高、落地难、见效慢,开源安全怎么办?
- Oracle 11g数据库安装教程
猜你喜欢

From digitalization to intelligent operation and maintenance: what are the values and challenges?

微软默默给 curl 捐赠一万美元,半年后才通知

Neural network implementation of handwritten numeral classification matlab

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?

The whole process of neural network construction is finally completed! Can't you come to me~

KMP template - string matching

Introduction to Alibaba eagle eye system

Switch and router technology-02-working principle of Ethernet switch

信通院陈屹力:降本增效是云原生应用最大价值

Fast analysis combined with Haidian medicine
随机推荐
(2) CBAM integrated two stream project construction - data preparation
【数据库系统概论(王珊)】第1章——绪论
树莓派驱动代码的编译和测试
Database hyperphone (II)
With the arrival of large displacement hard core products, can the tank brand break through the ceiling of its own brand?
奇瑞欧萌达也太像长安UNI-T了,但长得像,产品力就像吗?
微信小程序 云函数批量删除多条数据 Error: errCode: -502005 database collection not exists
Array of C language
步 IE 后尘,Firefox 的衰落成必然?
#yyds干货盘点# 面试必刷TOP101:链表内指定区间反转
大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?
js实现右键菜单栏功能
Help, boost and take responsibility, the new value and significance of the 6th Tuba rabbit 718 national home decoration Festival
交换机和路由器技术-03-交换机基本配置
数据库超话(四)
灵魂一问:为什么ES比MySQL更适合复杂条件搜索?
Some suggestions for writing original technical articles
Understanding service governance in distributed development
信通院陈屹力:降本增效是云原生应用最大价值
7 岁男孩被 AI 机器人折断手指,仅因下棋太快?