bug1.java.sql.SQLException: The server time zone value ‘?й???ʱ?’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.
解决方法:添加serverTimezone=UTC
url: jdbc:mysql://localhost:3306/testserverTimezone=UTC
bug2.Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class is
com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
解决方法:
提示信息表明数据库驱动com.mysql.jdbc.Driver’已经被弃用了、应当使用新的驱动com.mysql.cj.jdbc.Driver’
最终成功从sql中读出了数据
Items.java
public class Items {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Itenms{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
ItemsDao.java
public interface ItemsDao {
public List<Items> findAll() throws Exception;
}
ItemsDaoImpl.java
public class ItemsDaoImpl implements ItemsDao {
public List<Items> findAll() throws Exception {
List<Items> list = new ArrayList<Items>();
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
//加载驱动类
Class.forName("com.mysql.cj.jdbc.Driver");
//先获取connection对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/students?serverTimezone=UTC", "root", "123");
//获取真正操作数据的对象
preparedStatement = connection.prepareCall("select * from stu");
//执行数据库查询操作
resultSet = preparedStatement.executeQuery();
//把数据库结果集转成java的List集合
while (resultSet.next()) {
Items items = new Items();
items.setId(resultSet.getInt("id"));
items.setName(resultSet.getString("name"));
list.add(items);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.close();
preparedStatement.close();
resultSet.close();
}
return list;
}
}
ItemsTest.java
public class ItemsTest {
@Test
public void findAll() throws Exception{
ItemsDao itemsDao = new ItemsDaoImpl();
List<Items> list = itemsDao.findAll();
for (Items items : list) {
System.out.println(items.getName());
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.heima</groupId>
<artifactId>java04</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
参考文章
1.https://www.cnblogs.com/chenziyue/p/12537157.html
2.https://blog.csdn.net/weixin_42323802/article/details/82500458