Service实现转账流程
在数据库创建一个表account,用来存储账户数据
Dao层实现修改和查询单个的方法
service层通过对Dao层方法的多次调用实现转账功能。
实体类Account:
package com.qf.account;
/**
* 创建一个实体类,类名等于表名,属性名等于列名。
*
*/
public class Account {
private String cardNo;
private String password;
private String name;
private double balance;
//无参构造
public Account() {
}
//有参构造
public Account(String cardNo, String password, String name, double balance) {
this.cardNo = cardNo;
this.password = password;
this.name = name;
this.balance = balance;
}
public String getCardNo() {
return cardNo;
}
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
package com.qf.account;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class AccountDaoImpl {
public int insert(Account account){
return 0;
}
public int delete(String cardNo){
return 0;
}
//修改
public int update(Account account){
Connection connection = null;
PreparedStatement preparedStatement = null;
String sql = "update account set password=?,name=?,balance=? where cardNo=?";
try {
connection = DBUtils.getConnection();
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,account.getPassword());
preparedStatement.setString(2,account.getName());
preparedStatement.setDouble(3, account.getBalance());
preparedStatement.setString(4,account.getCardNo());
int result = preparedStatement.executeUpdate();
return result;
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtils.closeAll(connection,preparedStatement,null);
}
return 0;//代表当前程序结束
}
//查询单个
public Account select(String cardNo){
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String sql="select * from account where cardNo=?";
Account account =null;
try {
connection = DBUtils.getConnection();
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1,cardNo);
resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
String cardNos=resultSet.getString("cardNo");
String password =resultSet.getString("password");
String name = resultSet.getString("name");
double balance = resultSet.getDouble("balance");
account = new Account(cardNos,password,name,balance);
}
return account;
} catch (SQLException e) {
e.