当前位置:网站首页>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
边栏推荐
- Database hyperphone (II)
- Run loam_ Velodyne real-time mapping
- Flutter的布局
- Summer Challenge [FFH] real time chat room websocket practice
- General process of background management system permission setting
- #夏日挑战赛#【FFH】实时聊天室之WebSocket实战
- x-sheet 开发教程:初始化配置自定义布局
- Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
- The whole process of neural network construction is finally completed! Can't you come to me~
- From digitalization to intelligent operation and maintenance: what are the values and challenges?
猜你喜欢

可口可乐的首要挑战,不是元气森林
![[single chip microcomputer] 2.1 hardware composition of AT89S52 single chip microcomputer](/img/8d/27d99bf7a94e59f158ba952e9239bd.png)
[single chip microcomputer] 2.1 hardware composition of AT89S52 single chip microcomputer

Because the employee set the password to "123456", amd stolen 450gb data?

How to develop an online Excel spreadsheet system (Part 1)

js实现右键菜单栏功能

DDD (Domain Driven Design) layered architecture

Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds

ACL 2022 | prompt based automatic depolarization: effectively reducing bias in the pre training language model

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

快解析结合海典医药
随机推荐
Because the employee set the password to "123456", amd stolen 450gb data?
细数国产接口协作平台的六把武器!
Run loam_ Velodyne real-time mapping
General process of background management system permission setting
科目三: 直线行驶
Database hyperphone (4)
Coca Cola's primary challenge is not vitality forest
How to resolve the public domain name to the intranet IP server -- quickly resolve the domain name and map the Internet access
Likeshop takeout ordering system "100% open source without encryption"
JS to realize the right-click menu bar function
(2) CBAM integrated two stream project construction - data preparation
Following the example of IE, is the decline of Firefox inevitable?
High cost, difficult to implement, slow to take effect, what about open source security?
成本高、落地难、见效慢,开源安全怎么办?
Hegong sky team vision training Day7 - vision, Jetson naon and d435i
The chess robot broke the finger of a 7-year-old boy. Netizen: it violated the first law of robots
Some suggestions for writing original technical articles
神经网络实现手写数字分类matlab
Sharing of local file upload technology of SAP ui5 fileuploader
What should we pay attention to when choosing the LED display screen of the stadium