刚开始是set和get方法中的变量名与MySQL数据库字段不一样,打印时一直为null,
将变量名修改成数据库字段一样就好了。不知道为什么,请大佬解答
代码有点多 我贴点重要的
//查询功能实现
public static void Query() throws SQLException {
List<Account> accounts=as.query();
if(!accounts.isEmpty()){
//遍历集合
System.out.println("账目编号\t账目描述\t消费价格\t消费日期");
for(Account account:accounts){
System.out.println(account.getAccount_id()+"\t"+account.getAccount_desc()+"\t"+account.getAccount_price()+"\t"+account.getAccount_date());
}
}
}
就是修改的下面private的4个变量,之前是 String a_id;String a_desc;String a_price;String a_date;
public class Account {
private String account_id;
private String account_desc;
private String account_price;
private String account_date;
public String getAccount_id() {
return account_id;
}
public void setAccount_id(String account_id) {
this.account_id = account_id;
}
public String getAccount_desc() {
return account_desc;
}
public void setAccount_desc(String account_desc) {
this.account_desc = account_desc;
}
public String getAccount_price() {
return account_price;
}
public void setAccount_price(String account_price) {
this.account_price = account_price;
}
public String getAccount_date() {
return account_date;
}
public void setAccount_date(String account_date) {
this.account_date = account_date;
}
}
下面是操作数据库的代码
public class AccountService {
BaseDao sd=new BaseDao();
public List<Account> query() throws SQLException {
QueryRunner qr=new QueryRunner();
String sql="select * from account";
Object[] params={};
List<Account> list=qr.query(this.sd.getConnection(),sql, new BeanListHandler<Account>(Account.class));
return list;
}
下面是数据库的设计和数据