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;
}
@Override
public String toString() {
return "Account{" +
"cardNo='" + cardNo + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", balance=" + balance +
'}';
}
}
Dao层AccountDaoImpl:
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.printStackTrace();
}finally {
DBUtils.closeAll(connection,preparedStatement,resultSet);
}
return null;
}
}
业务层AccountServiceImpl:
package com.qf.account;
public class AccountServiceImpl {
public void transfer(String fromNo,String pwd,String toNo,double money){//1.接收参数
//2.组织完善转账业务功能
//2.1验证fromNo是否存在,即转账的初始账号是否存在。
AccountDaoImpl accountDao = new AccountDaoImpl();
try {
Account account = new Account();
account= accountDao.select(fromNo);
if (account==null){
throw new RuntimeException("卡号不存在!");
}
//2.2验证pwd密码是否正确,即验证初始账号的密码是否正确。
if(!account.getPassword().equals(pwd)){
throw new RuntimeException("密码不正确!");
}
//2.3验证余额是否充足。
if(account.getBalance() < money){
throw new RuntimeException("余额不足!");
}
//2.4验证forNo是否存在 ,即对方账号是否存在。
Account toAccount = new Account();
toAccount = accountDao.select(toNo);
if (toAccount==null){
throw new RuntimeException("对方卡号不存在!");
}
//2.5减少forNo余额。
account.setBalance(account.getBalance()-money);
accountDao.update(account);
//2.6增加toNo余额。
toAccount.setBalance(toAccount.getBalance()+money);
accountDao.update(toAccount);
System.out.println("转账成功!");
} catch (RuntimeException e) {
System.out.println("转账失败!");
e.printStackTrace();
}
}
}
工具类:
package com.qf.account;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtils {
//1.提供一个私有的静态常量,存储配置文件的map
private static final Properties PROPERTIES = new Properties();
static {
//2.拿到一个字节流,通过DBUtils类对象的一个方法拿到
InputStream is = DBUtils.class.getResourceAsStream("/db.properties");
try {
PROPERTIES.load(is);//3.通过流,将配置文件内容以键值对的形式存储到PROPERTIES集合
Class.forName(PROPERTIES.getProperty("driver"));//通过PROPERTIES.getProperty去拿那个键
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection connection = null;
try {
connection= DriverManager.getConnection(PROPERTIES.getProperty("url"),PROPERTIES.getProperty("name"),PROPERTIES.getProperty("password"));
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void closeAll(Connection connection, Statement statement,ResultSet resultSet){
try {
if(resultSet!=null){
resultSet.close();
}
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
测试类:
package com.qf.account;
public class TestAccount {
public static void main(String[] args) {
AccountServiceImpl accountService = new AccountServiceImpl();
accountService.transfer("6002","1234","6003",1000);
}
}
配置文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc?serverTimezone=UTC
name=root
password=123456
运行结果:
转账成功!
标签:转账,account,String,Service,cardNo,流程,password,public,name From: https://www.cnblogs.com/zhangtiedangg/p/17089987.html