利用德鲁伊连接数据库实现后端读取数据库数据到前端显示 无法读取数据并后台报错
实体类
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>
代码结构
前台只能出现样式,无法读取到数据
点击超链,读取数据库数据并显示
试试:
1、在jdbc_url中,加上 useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
2、将驱动类改成:com.mysql.cj.jdbc.Driver
,加个 .cj
,因为使用的是8.x的驱动;
报错的全部信息都贴上来
你把你的报错原因截个图呀,其实你把这么多拿过来,关键的却一个没有,比如druid.properties,还有具体报错,就光看这个我认为是你的数据库没有链接成功
这个应该是和mysql 版本有关系。query_cache_size在The query cache在 MySQL 5.7.20中已过时,在MySQL 8.0中已经移除不用了。不清楚你的mysql版本。https://dev.mysql.com/doc/refman/5.7/en/query-cache.html
可以尝试去修改mysql-connector-java的版本号。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
<scope>runtime</scope>
</dependency>
如有帮助,欢迎采纳!
mysql-connecter-java的版本过低,数据库驱动程序与数据库版本不对应 ,如果是用maven的话可以修改版本号重新拉取jar包
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
```xml
```
mysql驱动版本过低,你的数据库版本高,query_cache_size在mysql升到8.0时已经移除。升级本地驱动jar包
望采纳,您的采纳就是我最大的动力,谢谢
网络层渲染错误。检查网络和数据库
1.驱动的名称改了
driver-class-name: com.mysql.cj.jdbc.Driver #mysql8的版本
2.url: jdbc:mysql://localhost:3306/business?useSSL=true&userUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
数据库连接后面加上?useSSL=true&userUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
sql有问题呀,这不提示得很明显嘛,检查一下你写的东西。