我是靠谱客的博主 整齐可乐,最近开发中收集的这篇文章主要介绍银行管理系统--使用JAVA+JDBC对数据库的数据进行增加、修改、删除、查询等操作,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述


 BankAccountDaoImpl部分:

package dao.impl;
import dao.IBankAccountDao;
import dao.util.BaseDao;
import model.BankAccount;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class BankAccountDaoImpl implements IBankAccountDao {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String currentTime = sdf.format(now);
@Override
public int insertAccount(String name, String pwd, int advanceMoney,int cardNum) {
int insertSuccess = 0;
Connection connection = BaseDao.getConnection();
PreparedStatement ps = null;
try {
String sql = "insert into bankaccount values(0,?,?,null,?,?,?)";
ps = connection.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,pwd);
ps.setInt(3,advanceMoney);
ps.setString(4,currentTime);
ps.setInt(5,cardNum);
insertSuccess = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return insertSuccess;
}
@Override
public List<BankAccount> getAllAccount() {
List<BankAccount> accountList = new ArrayList<>();
Connection connection = BaseDao.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "select cardID, customerName, password, oldPassword, balance, openDate,cardNum from bankaccount";
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()){
BankAccount bankAccount = new BankAccount();
bankAccount.setCardID(rs.getInt("cardID"));
bankAccount.setCustomerName(rs.getString("customerName"));
bankAccount.setPassword(rs.getString("password"));
bankAccount.setCustomerName(rs.getString("oldPassword"));
bankAccount.setBalance(rs.getInt("balance"));
bankAccount.setOpenDate(rs.getString("openDate"));
bankAccount.setCardNum(rs.getInt("cardNum"));
accountList.add(bankAccount);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(connection,ps,rs);
}
return accountList;
}
@Override
public int addMoney(int deposotMoney,int cardNum) {
Connection connection = BaseDao.getConnection();
PreparedStatement ps = null;
int addMoneySuccess = 0;
try {
String sql = "update bankaccount set balance=balance + ? where cardNum = ?";
ps = connection.prepareStatement(sql);
ps.setInt(1,deposotMoney);
ps.setInt(2,cardNum);
addMoneySuccess = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(connection,ps,null);
}
return addMoneySuccess;
}
@Override
public int reduceMoney(int takeMoney, int cardNum) {
Connection connection = BaseDao.getConnection();
PreparedStatement ps = null;
int reduceMoneySuccess = 0;
try {
String sql = "update bankaccount set balance=balance - ? where cardNum = ?";
ps = connection.prepareStatement(sql);
ps.setInt(1,takeMoney);
ps.setInt(2,cardNum);
reduceMoneySuccess = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(connection,ps,null);
}
return reduceMoneySuccess;
}
@Override
public int changePwd(int cardNum, String newPwd) {
Connection connection = BaseDao.getConnection();
PreparedStatement ps = null;
int changeSuccess = 0;
try {
String sql = "update bankaccount set password = ? where cardNum = ?";
ps = connection.prepareStatement(sql);
ps.setString(1,newPwd);
ps.setInt(2,cardNum);
changeSuccess = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(connection,ps,null);
}
return changeSuccess;
}
@Override
public int closeAccount(int cardNum) {
Connection connection = BaseDao.getConnection();
PreparedStatement ps = null;
int closeSuccess = 0;
try {
String sql = "delete from bankaccount where cardNum = ?";
ps = connection.prepareStatement(sql);
ps.setInt(1,cardNum);
closeSuccess = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(connection,ps,null);
}
return closeSuccess;
}
@Override
public BankAccount selectAccount(int cardNum) {
Connection connection = BaseDao.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
BankAccount bankAccount = null;
try {
String sql = "select cardID,customerName,password,balance,openDate,cardNum from bankaccount where cardNum = ?";
ps = connection.prepareStatement(sql);
ps.setInt(1,cardNum);
rs = ps.executeQuery();
while(rs.next()){
bankAccount = new BankAccount();
bankAccount.setCardID(rs.getInt("cardID"));
bankAccount.setCustomerName(rs.getString("customerName"));
bankAccount.setPassword(rs.getString("password"));
bankAccount.setBalance(rs.getInt("balance"));
bankAccount.setOpenDate(rs.getString("openDate"));
bankAccount.setCardNum(rs.getInt("cardNum"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(connection,ps,rs);
}
return bankAccount;
}
}

实现:

package service.impl;
import dao.IBankAccountDao;
import dao.IBankRecordDao;
import dao.impl.BankAccountDaoImpl;
import dao.impl.BankRecordDaoImpl;
import model.BankAccount;
import service.IBankAccountService;
import service.IBankRecordService;
import java.util.List;
import java.util.Scanner;
public class BankAccountServiceImpl implements IBankAccountService {
Scanner input = new Scanner(System.in);
IBankAccountDao bankAccountDao = new BankAccountDaoImpl();
int cardNum;
IBankRecordDao bankRecordDao = new BankRecordDaoImpl();
IBankRecordService bankRecordService = new BankRecordServiceImpl();
BankAccount bankAccount = null;
/**
* 登录菜单页面
* @return 返回用户的选择
*/
public int menu(){
System.out.println("*********************************************");
System.out.println("tttt银行管理系统");
System.out.println("*********************************************");
System.out.println("tttt1.开户");
System.out.println("tttt2.登录");
System.out.println("tttt3.退出");
System.out.println("*********************************************");
System.out.println("请选择服务种类:");
int firstmenuChoose = input.nextInt();
return firstmenuChoose;
}
/**
* 子菜单
* @return 返回用户的选择
*/
public int secondMenu(){
System.out.println("tttt1.存钱");
System.out.println("tttt2.取钱");
System.out.println("tttt3.查询账户余额");
System.out.println("tttt4.查询交易记录");
System.out.println("tttt5.转账");
System.out.println("tttt6.修改密码");
System.out.println("tttt7.注销账户");
System.out.println("tttt8.退出");
System.out.println("请选择要进行的业务:");
int secondMenuChoose = input.nextInt();
return secondMenuChoose;
}
public void start() {
int firstmenuChoose = menu();
switch (firstmenuChoose) {
case 1:
addAccount();
start();
break;
case 2:
cardNum = login();
start02();
break;
case 3:
System.exit(0);
break;
default:
System.out.println("输入错误,请重新输入!");
start();
}
}
public void start02(){
int secondMenuChoose = secondMenu();
switch(secondMenuChoose){
case 1:
addMoney(cardNum);
start02();
break;
case 2:
outputMoney(cardNum);
start02();
break;
case 3:
selectBalance(cardNum);
start02();
break;
case 4:
bankRecordService.showAllRecord(cardNum);
start02();
break;
case 5:
transferAccount(cardNum);
start02();
break;
case 6:
changePwd(cardNum);
start();
break;
case 7:
closeAccount(cardNum);
start();
break;
case 8:
start();
break;
default:
System.out.println("输入错误,请重新输入:");
start();
}
}
@Override
public void addAccount() {
boolean flag = false;
String password = null;
System.out.println("*********************************************");
System.out.println("请输入姓名:");
String name = input.next();
while (!flag ) { //验证两次输入密码是否一致
System.out.println("请输入密码:");
password = input.next();
System.out.println("请再次输入密码:");
String password_define = input.next();
if (password.equals(password_define)) {
System.out.println("请缴纳预存金额(元)");
int advanceMoney = input.nextInt();
int cardNum1 = (int) (Math.random() * 100 + 1);
int addSuccess = bankAccountDao.insertAccount(name,password,advanceMoney,cardNum1);
bankRecordDao.openAccountRecord(cardNum1,advanceMoney);
if(addSuccess > 0){
System.out.println("开户成功!");
System.out.println("您的卡号为:" + cardNum1);
flag = true;
}
} else {
System.out.println("您两次输入的密码不匹配");
}
}
}
@Override
public int login() {
System.out.println("**************用户登录界面*****************");
System.out.println("请输入卡号:");
int cardNumInput = input.nextInt();
System.out.println("输入密码:");
String loginPassword = input.next();
bankAccount = bankAccountDao.selectAccount(cardNumInput);
if(bankAccount != null){
if(bankAccount.getPassword().equals(loginPassword)){
System.out.println("登陆成功!");
System.out.println("欢迎您 "+bankAccount.getCustomerName());
}else{
System.out.println("用户名或密码错误,请重新输入!");
login();
}
}else{
System.out.println("卡号不存在,请重新输入:");
login();
}
return cardNumInput;
}
@Override
public void addMoney(int cardNum) {
System.out.println("请输入要存的金额:");
int deposotMoney = input.nextInt();
int addMoneySuccess = bankAccountDao.addMoney(deposotMoney,cardNum);
if( addMoneySuccess > 0){
System.out.println("存款成功");
bankRecordService.recordAddMoney(cardNum,deposotMoney);
//
System.out.println("您存储的金额为:"+deposotMoney+"元");
//
int balance = bankAccountDao.selectBalance(cardID);
//
System.out.println("您账号中现有金额为:"+balance+"元");
}
}
@Override
public void outputMoney(int cardNum) {
System.out.println("请输入要取的金额:");
int takeMoney = input.nextInt();
if (takeMoney % 100 == 0
&& takeMoney >= 100){
bankAccount = bankAccountDao.selectAccount(cardNum);
if(takeMoney <= bankAccount.getBalance()){
int reduceMoneySuccess = bankAccountDao.reduceMoney(takeMoney,cardNum);
if(reduceMoneySuccess > 0){
System.out.println("取款成功");
bankRecordService.recordTakeMoney(cardNum,takeMoney);
}
}else {
System.out.println("余额不足!");
outputMoney(cardNum);
}
}else{
System.out.println("金额不得小于100,且需为100的整数倍");
outputMoney(cardNum);
}
}
@Override
public void selectBalance(int cardNum) {
bankAccount = bankAccountDao.selectAccount(cardNum);
System.out.println("账户余额为:"+bankAccount.getBalance());
}
@Override
public void transferAccount(int cardNum) {
System.out.println("请输入要转到的账户:");
int receiveCardID = input.nextInt();
System.out.println("请输入要转的金额:");
int Money = input.nextInt();
bankAccount = bankAccountDao.selectAccount(cardNum);
if(bankAccount != null){
bankAccountDao.reduceMoney(Money,cardNum);
bankAccountDao.addMoney(Money,receiveCardID);
System.out.println("转账成功");
bankRecordService.recordTransferMoney(cardNum,Money);
}else{
System.out.println("抱歉!您输入的账户不存在");
transferAccount(cardNum);
}
}
@Override
public void changePwd(int cardNum) {
System.out.println("请输入旧密码:");
String oldPwd = input.next();
bankAccount = bankAccountDao.selectAccount(cardNum);
if(bankAccount.getPassword().equals(oldPwd)){
System.out.println("请输入新密码:");
String newPwd01 = input.next();
System.out.println("请确认新密码:");
String newPwd = input.next();
if(newPwd01.equals(newPwd)){
bankAccountDao.changePwd(cardNum,newPwd);
bankRecordService.recordChangePwd(cardNum);
System.out.println("修改密码成功");
System.out.println("请重新登录");
}else{
System.out.println("您输入的密码不正确,请重新输入:");
changePwd(cardNum);
}
}else {
System.out.println("您输入的密码不正确,请重新输入:");
changePwd(cardNum);
}
}
@Override
public void closeAccount(int cardNum) {
bankAccount = bankAccountDao.selectAccount(cardNum);
int closeSuccess = bankAccountDao.closeAccount(cardNum);
if(closeSuccess > 0 ){
System.out.println("账户注销成功,退返金额:"+bankAccount.getBalance());
}
}
}

最后

以上就是整齐可乐为你收集整理的银行管理系统--使用JAVA+JDBC对数据库的数据进行增加、修改、删除、查询等操作的全部内容,希望文章能够帮你解决银行管理系统--使用JAVA+JDBC对数据库的数据进行增加、修改、删除、查询等操作所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部