目录
一、系统介绍
1.开发环境
开发工具:IDEA2018.2
JDK版本:jdk1.8
Mysql版本:8.0.13
2.技术选型
使用Java+Swing开发,用Mysql作为数据库。
3.系统功能
1. 注册
2.登录
3.取款
4.存款
5.转账
6.查询余额
4.数据库
/*
Navicat Premium Data Transfer
Source Server : Mysql
Source Server Type : MySQL
Source Server Version : 80013
Source Host : localhost:3306
Source Schema : swing_atm
Target Server Type : MySQL
Target Server Version : 80013
File Encoding : 65001
Date: 11/07/2021 22:04:12
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for account
-- ----------------------------
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`accountNo` int(11) NOT NULL,
`pwd` int(1) NOT NULL,
`aName` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`balance` double(255, 2) NULL DEFAULT NULL,
PRIMARY KEY (`accountNo`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES (1, 1, '张三', 99.00);
INSERT INTO `account` VALUES (2, 2, '李四', 346.00);
SET FOREIGN_KEY_CHECKS = 1;
5.工程截图
二、系统展示
1.注册页面
2.登录页面
3.主页面
4.取款
5.存款
6.转账
7.余额查询
三、部分代码
AccountDAO.java
package dao;
import java.sql.ResultSet;
import db.DBUtil;
import entity.Account;
public class AccountDAO {
public Account accountDao(int accountNo) {
return findAccByNo(accountNo);
}
// 如果用户找不到,返回false
public boolean accountDaoForUI(int accountNo) {
if (findAccByNo(accountNo) != null)
return true;
else
return false;
}
// 根据账户查询用户,并返回一个Account对象
public Account findAccByNo(int accountNo) {
DBUtil db = new DBUtil();
Account account = null;
try {
// 获取数据库连接
db.getConnection();
// 使用PreparedStatement发送sql语句
String sql = "SELECT * FROM account WHERE accountNo=?";
// 设置参数
Object[] param = new Object[] { accountNo };
// 执行查询
ResultSet rs = db.executeQuery(sql, param);
if (rs.next()) {
// 将结果集中的数据封装到对象中
account = new Account(rs.getInt(1), rs.getInt(2), rs.getString(3), rs.getDouble(4));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放数据库资源
db.closeAll();
}
// 返回用户对象
return account;
}
}
RegisterDAO.java
package dao;
import java.sql.ResultSet;
import db.DBUtil;
import entity.Account;
public class BalanceDAO {
public void balanceDao(int accountNo) throws Exception {
DBUtil db = new DBUtil();
// 连接数据库
db.getConnection();
// 查询语句:根据账户查询余额
String sql = "SELECT balance FROM account WHERE accountNo=" + accountNo;
// 执行查询语句并且保存结果集
ResultSet rs = db.executeQuery(sql, null);
// 遍历输出查询结果
while (rs.next()) {
System.out.println("您好!您的账户目前余额为:" + rs.getDouble(1) + "元!");
}
// 关闭数据库连接,释放资源
db.closeAll();
}
// 查询余额返回daoUI界面,返回String类型的值
public String balanceDaoForUI(Account account) throws Exception {
DBUtil db = new DBUtil();
// 连接数据库
db.getConnection();
// 查询语句:根据账户查询余额
String sql = "SELECT balance FROM account WHERE accountNo=" + account.getAccountNo();
// 执行查询语句并且保存结果集
ResultSet rs = db.executeQuery(sql, null);
String balance = null;
// // 遍历输出查询结果
while (rs.next()) {
// System.out.println("您好!您的账户目前余额为:" + rs.getDouble(1) + "元!");
balance = rs.getDouble(1) + "";
}
// 关闭数据库连接,释放资源
db.closeAll();
return balance;
}
}
TransactionDAO.java
package dao;
import db.DBUtil;
import entity.Account;
public class TransactionDAO {
public void transactionDao(Account account) throws Exception {
DBUtil db = new DBUtil();
// 连接数据库
db.getConnection();
// 使用PreparedStatement发送sql语句
String sql = "UPDATE account SET balance=? WHERE accountNo=?";
// 设置参数
Object[] param = new Object[] { account.getBalance(), account.getAccountNo(), };
db.executeUpdate(sql, param);
// 关闭数据库连接,释放资源
db.closeAll();
}
}
BalanceDAO.java
package dao;
import java.sql.ResultSet;
import db.DBUtil;
import entity.Account;
public class BalanceDAO {
public void balanceDao(int accountNo) throws Exception {
DBUtil db = new DBUtil();
// 连接数据库
db.getConnection();
// 查询语句:根据账户查询余额
String sql = "SELECT balance FROM account WHERE accountNo=" + accountNo;
// 执行查询语句并且保存结果集
ResultSet rs = db.executeQuery(sql, null);
// 遍历输出查询结果
while (rs.next()) {
System.out.println("您好!您的账户目前余额为:" + rs.getDouble(1) + "元!");
}
// 关闭数据库连接,释放资源
db.closeAll();
}
// 查询余额返回daoUI界面,返回String类型的值
public String balanceDaoForUI(Account account) throws Exception {
DBUtil db = new DBUtil();
// 连接数据库
db.getConnection();
// 查询语句:根据账户查询余额
String sql = "SELECT balance FROM account WHERE accountNo=" + account.getAccountNo();
// 执行查询语句并且保存结果集
ResultSet rs = db.executeQuery(sql, null);
String balance = null;
// // 遍历输出查询结果
while (rs.next()) {
// System.out.println("您好!您的账户目前余额为:" + rs.getDouble(1) + "元!");
balance = rs.getDouble(1) + "";
}
// 关闭数据库连接,释放资源
db.closeAll();
return balance;
}
}
LoginFrame.java
package view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import dao.AccountDAO;
import entity.Account;
//import com.qst.dms.entity.User;
//import com.qst.dms.service.UserService;
//登录窗口
public class LoginFrame extends JFrame {
// 主面板
private JPanel p;
// 标签
private JLabel lblName, lblPwd;
// 用户名,文本框
private JTextField txtAccount;
// 密码,密码框
private JPasswordField txtPwd;
// 确认、取消和注册,按钮
private JButton btnOk, btnCancle, btnRegist;
// 登录用户
private static Account account;
public static Account getAccount() {
return account;
}
public static void setAccount(Account account) {
LoginFrame.account = account;
}
// 账户DAO类
private AccountDAO accountDao;
// 构造方法
public LoginFrame() {
super("ATM自助存取款机");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 实例化用户业务类对象
accountDao = new AccountDAO();
// 实例化组件
p = new JPanel();
// 使用null布局
p.setLayout(null);
lblName = new JLabel("账户:");
lblPwd = new JLabel("密码:");
txtAccount = new JTextField(20);
txtPwd = new JPasswordField(20);
txtPwd.setEchoChar('*');
btnOk = new JButton("登录");
btnOk.addActionListener(new LoginListener());
btnCancle = new JButton("重置");
btnCancle.addActionListener(new ResetListener());
btnRegist = new JButton("注册");
btnRegist.addActionListener(new RegistListener());
lblName.setBounds(30, 40, 95, 25);
lblPwd.setBounds(30, 105, 83, 40);
txtAccount.setBounds(140, 40, 250, 35);
txtPwd.setBounds(140, 108, 250, 35);
btnOk.setBounds(75, 191, 75, 35);
btnCancle.setBounds(211, 191, 75, 35);
btnRegist.setBounds(351, 191, 75, 35);
p.add(lblName);
p.add(txtAccount);
p.add(lblPwd);
p.add(txtPwd);
p.add(btnOk);
p.add(btnCancle);
p.add(btnRegist);
// 主面板放入窗体中
getContentPane().add(p);
// 设置窗体大小和位置
this.setSize(488, 343);
// 设置窗口在屏幕中央
this.setLocationRelativeTo(null);
// 设置窗体初始可见
this.setVisible(true);
}
// 监听类,负责处理登录按钮
public class LoginListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理逻辑
public void actionPerformed(ActionEvent e) {
// 根据账户查询用户
account = accountDao.findAccByNo(Integer.parseInt(txtAccount.getText().trim()));
// 判断用户是否存在
if (account != null) {
// 判断输入的密码是否正确
if (account.getPwd() == Integer.parseInt(new String(txtPwd.getPassword()))) {
// 登录成功,隐藏登录窗口
LoginFrame.this.setVisible(false);
// 显示主窗口
new MainFrame(account).setVisible(true);
} else {
// 输出提示信息
// System.out.println("密码错误!请重新输入!");
JOptionPane.showMessageDialog(null, "密码错误!请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);
// 清空密码框
txtPwd.setText("");
}
} else {
// 输出提示信息
// System.out.println("该用户不存在,请先注册!");
JOptionPane.showMessageDialog(null, "该用户不存在,请先注册!", "错误提示", JOptionPane.ERROR_MESSAGE);
}
}
}
// 监听类,负责处理重置按钮
public class ResetListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
@Override
public void actionPerformed(ActionEvent e) {
// 清空文本框
txtAccount.setText("");
txtPwd.setText("");
}
}
// 监听类,负责处理注册按钮
public class RegistListener implements ActionListener {
// 重写actionPerFormed()方法,事件处理方法
public void actionPerformed(ActionEvent e) {
// 创建注册窗口
new RegistFrame().setVisible(true);
}
}
// 主程序,整个应用程序的入口
public static void main(String[] args) {
new LoginFrame();
}
}
RegistFrame.java
package view;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.synth.SynthSeparatorUI;
import dao.AccountDAO;
import dao.RegisterDAO;
import entity.Account;
public class RegistFrame extends JFrame {
private JPanel contentPane;
private JTextField textAccount;
private JPasswordField textPwd;
private JPasswordField textPwd1;
private JTextField textName;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
RegistFrame frame = new RegistFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public RegistFrame() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setFont(new Font("宋体", Font.PLAIN, 12));
setTitle("注册");
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 561, 414);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel label = new JLabel("账 户:");
label.setFont(new Font("宋体", Font.PLAIN, 18));
label.setBounds(50, 35, 81, 21);
panel.add(label);
textAccount = new JTextField();
textAccount.setBounds(146, 30, 243, 32);
panel.add(textAccount);
textAccount.setColumns(10);
JLabel label_1 = new JLabel("密 码:");
label_1.setFont(new Font("宋体", Font.PLAIN, 18));
label_1.setBounds(50, 147, 81, 21);
panel.add(label_1);
JLabel label_2 = new JLabel("确认密码:");
label_2.setFont(new Font("宋体", Font.PLAIN, 18));
label_2.setBounds(50, 206, 101, 32);
panel.add(label_2);
JButton btnNewButton = new JButton("注册");
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 18));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String accountNo = textAccount.getText();
String name = textName.getText();
String pwd = new String(textPwd.getPassword());
String pwd1 = new String(textPwd1.getPassword());
if (null == accountNo || "".equals(accountNo)) {
JOptionPane.showMessageDialog(null, "账户不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (null == name || "".equals(name)) {
JOptionPane.showMessageDialog(null, "账户姓名不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (null == pwd || "".equals(pwd) || null == pwd1 || "".equals(pwd1)) {
JOptionPane.showMessageDialog(null, "密码不能为空!", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
} else if (!(pwd.equals(pwd1))) {
JOptionPane.showMessageDialog(null, "两次输入密码不一样!", "输入错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 进行数据库注册
Account account1 = new Account(Integer.parseInt(accountNo), Integer.parseInt(pwd), name, 0);
// 创建注册账户DAO对象
RegisterDAO rs = new RegisterDAO();
// 调用注册方法registerDao(),进行数据库中的新增账户操作
try {
// DAO方法返回true才提示注册成功
boolean temp = rs.registerDao(account1);
if (temp) {
JOptionPane.showMessageDialog(null, "注册成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 注册成功,关闭注册窗口
dispose();
} else {
JOptionPane.showMessageDialog(null, "账户已经存在!注册失败!", "错误提示", JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(60, 280, 123, 29);
panel.add(btnNewButton);
JButton button = new JButton("重置");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textAccount.setText(null);
textPwd.setText(null);
textPwd1.setText(null);
}
});
button.setFont(new Font("宋体", Font.PLAIN, 18));
button.setBounds(300, 280, 123, 29);
panel.add(button);
textPwd = new JPasswordField();
textPwd.setBounds(148, 144, 243, 32);
panel.add(textPwd);
textPwd1 = new JPasswordField();
textPwd1.setBounds(148, 209, 243, 32);
panel.add(textPwd1);
JLabel label_3 = new JLabel("\u59D3 \u540D\uFF1A");
label_3.setFont(new Font("宋体", Font.PLAIN, 18));
label_3.setBounds(50, 90, 81, 21);
panel.add(label_3);
textName = new JTextField();
textName.setColumns(10);
textName.setBounds(146, 87, 243, 32);
panel.add(textName);
}
}
四、其他
1.其他系统实现
1.JavaWeb系统系列实现
Java+SSH+Bootstrap实现在线考试系统(含论文)
Java+Springboot+Mybatis+Bootstrap+Maven实现网上商城系统
2.JavaSwing系统系列实现
2.获取源码
点击以下链接获取源码,数据库文件在sql文件下面。
3.运行项目
请点击以下链接,部署你的项目。
4.备注
如有侵权请联系我删除。
5.支持博主
如果您觉得此文对您有帮助,请点赞加关注。祝您生活愉快!想要获取其他资源可关注左侧微信公众号获取!