Felix奕泽 2022-07-20 21:44 采纳率: 100%
浏览 127
已结题

JDBC连接数据库读取前台无法显示数据

问题遇到的现象和发生背景

利用德鲁伊连接数据库实现后端读取数据库数据到前端显示 无法读取数据并后台报错

问题相关代码,请勿粘贴截图

实体类

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>

代码结构

img

运行结果及报错内容

img

img

我的解答思路和尝试过的方法

前台只能出现样式,无法读取到数据

img

我想要达到的结果

点击超链,读取数据库数据并显示

  • 写回答

9条回答 默认 最新

  • 原来我不知道啊 2022-07-21 10:14
    关注

    试试:
    1、在jdbc_url中,加上 useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    2、将驱动类改成:com.mysql.cj.jdbc.Driver,加个 .cj,因为使用的是8.x的驱动;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

问题事件

  • 系统已结题 7月29日
  • 已采纳回答 7月21日
  • 修改了问题 7月20日
  • 修改了问题 7月20日
  • 展开全部

悬赏问题

  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么