一、创建实体类domain
package cn.tx.domain;
import java.io.Serializable;
//账户
public class Account implements Serializable {
private Integer id;
private String name;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
private Double money;
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;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
}
二、创建Dao类操作数据库
1、创建一个接口
package cn.tx.dao;
import cn.tx.domain.Account;
import java.util.List;
public interface AccountDao {
public List<Account> findAll();
}
2、实现接口
package cn.tx.dao;
import cn.tx.domain.Account;
import com.alibaba.druid.pool.DruidDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class AccountDaoImpl implements AccountDao{
public List<Account> findAll() {
//使用JDBC的程序
//创建连接池对象,设置4个参数
DruidDataSource dataSource=new DruidDataSource();
//设置4个参数
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///spring_db");
dataSource.setUsername("root");
dataSource.setPassword("root");
//定义对象
Connection conn =null;
PreparedStatement stmt=null;
ResultSet rs =null;
List<Account> list=new ArrayList<>();
try {
//获取到连接
conn =dataSource.getConnection();
//编写sql
String sql="select * from account";
//预编译
stmt=conn.prepareStatement(sql);
//执行sql
rs = stmt.executeQuery();
//遍历结果
while (rs.next()){
//进行数据封装
Account account=new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getDouble("money"));
//存储
list.add(account);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
三、创建一个业务层Service类
1、创建一个接口
package cn.tx.service;
import cn.tx.domain.Account;
import java.util.List;
public interface AccountService {
public List<Account> findAll();
}
2、实现接口
package cn.tx.service;
import cn.tx.dao.AccountDao;
import cn.tx.dao.AccountDaoImpl;
import cn.tx.domain.Account;
import java.util.List;
public class AccountServiceImpl implements AccountService{
public List<Account> findAll() {
//创建Dao对象
AccountDao accountDao=new AccountDaoImpl();
return accountDao.findAll();
}
}
四、测试类
package cn.tx.test;
import cn.tx.domain.Account;
import cn.tx.service.AccountService;
import cn.tx.service.AccountServiceImpl;
import org.junit.Test;
import java.util.List;
public class Demo1 {
/*第一种方式,new对象的方式查询数据*/
@Test
public void run1(){
//创建对象
AccountService accountService=new AccountServiceImpl();
//调用方法
List<Account> list= accountService.findAll();
//遍历
for (Account account:list){
System.out.println(account);
}
}
}
五、运行查询数据库account表的数据结果如下:
完成了。
但是,运行之前我遇到了一些问题:
1、数据库连接不成功:
IDEA连接mysql报错!Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ prope
翻译:服务器返回无效时区。进入“高级”选项卡,手动设置“serverTimezone”属性。
解决:
1、检查你的mysql是否已经配置完环境变量(如果你是刚刚下载mysql还没配置环境变量的可以去查查怎么样配置)
2、配置完环境变量后 进入命令窗口(Win + R),连接数据库 mysql -hlocalhost -uroot -p,回车,输入密码,回车,如图:
2、继续输入 show variables like’%time_zone’; (注意不要漏掉后面的分号),回车,如图:
3、设置时区。
输入set global time_zone = ‘+8:00’; 注意不要漏掉后面的分号),回车,如图:
设置成功,重新连接下数据库,连接成功!
以上就是我遇到的问题和解决的方法。