基于java+mysql的swing+mysql银行atm管理系统(java+mysql+swing)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于java的Swing+MySQL银行ATM管理系统(java+mysql+swing)
启动类:LoginFrame
登录用户:
user1 123456
user2 123456
user3 123456
功能:查询余额、取款、存款、转账
int i = 0;
String sql = "insert into bank(name,password,balance)"
+ "values('" + name + "','" + password + "','" + 0 + "')";
i = ExecuteSQL.executeUpdate(sql);
ExecuteSQL.close();
return i;
}
//查询用户信息
public static user query(String name) {
user u = new user();
String sql = "select name,balance from bank where name = '" + name + "'";
ResultSet rs = ExecuteSQL.executeQuery(sql);
try {
while (rs.next()) {
u.setName(rs.getString("name"));
u.setbalance(rs.getDouble("balance"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ExecuteSQL.close();
return u;
}
// 修改账户的余额
public static int modifyMoney(String name, double balance) {
user u = new user();
String sql = "update bank set balance = '" + balance + "' where name ='" + name + "'";
int i = ExecuteSQL.executeUpdate(sql);
ExecuteSQL.close();
return i;
}
}
contentPane.add(lblNewLabe);
outputField = new JTextField();
outputField.setBounds(199, 84, 117, 24);
contentPane.add(outputField);
outputField.setColumns(10);
JButton OKButton = new JButton("\u786E\u5B9A");
OKButton.setFont(new Font("新宋体", Font.BOLD, 15));
OKButton.setBounds(84, 159, 113, 27);
contentPane.add(OKButton);
OKButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("withdrawFrame.withdrawFrame(...).new ActionListener() {...}.actionPerformed()"+outputField.getText());
if (Float.parseFloat(outputField.getText()) < 100000) {
if(Float.parseFloat(outputField.getText())%100==0) {
user_query = ExecuteSQL.query(name);
if (user_query.getbalance() > Double.parseDouble(outputField.getText())) {
double temp = user_query.getbalance() - Double.parseDouble(outputField.getText());
DecimalFormat df = new DecimalFormat( "0.00 ");
int i = ExecuteSQL.modifyMoney(name, temp);
if(i>0){
setVisible(false);
atmFrame frame = new atmFrame(name);
frame.setVisible(true);
JOptionPane.showMessageDialog(null, "取钱交易成功!"+"\n" + "剩余余额为:" + df.format(temp));
}else{
JOptionPane.showMessageDialog(null, "取钱交易失败!"+"\n" + "剩余余额为:" + df.format(temp));
}
} else {
JOptionPane.showMessageDialog(null, "余额不足,请重新输入!" + "\n" + "当前余额为:" + user_query.getbalance());
outputField.setText("");
}
}else {
JOptionPane.showMessageDialog(null, "系统不支持非100元整钞,请重新输入!");
outputField.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "输入金额大于100000,请重新输入!");
outputField.setText("");
}
}
// TODO Auto-generated catch block
e.printStackTrace();
}
ExecuteSQL.close();
return u;
}
// 修改账户的余额
public static int modifyMoney(String name, double balance) {
user u = new user();
String sql = "update bank set balance = '" + balance + "' where name ='" + name + "'";
int i = ExecuteSQL.executeUpdate(sql);
ExecuteSQL.close();
return i;
}
}
public class LoginFrame extends JFrame {
private JPanel contentPane;
private JTextField u_name_Field;
private JPasswordField u_password_field;
private user user;
public class SignFrame extends JFrame {
private JPanel contentPane;
private JTextField u_name;
private JPasswordField u_password;
private JPasswordField u_password_1;
private user user;
//注册按钮监听器
class OKButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (!u_name.getText().equals("")) {
if (!u_password.getText().equals("")) {
if (!u_password_1.getText().equals("")) {
if (u_password.getText().equals(u_password_1.getText())) {
user = ExecuteSQL.check(u_name.getText(), u_password.getText());
if (!u_name.getText().equals(user.getName())) {
ExecuteSQL.addUser(u_name.getText(), u_password.getText());
setVisible(false);
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
JOptionPane.showMessageDialog(null, "注册成功,可以继续登录!");
} else {
JOptionPane.showMessageDialog(null, "用户名已经存在!");
}
} else {
JOptionPane.showMessageDialog(null, "密码确认不符!");
}
} else {
JOptionPane.showMessageDialog(null, "未输入确认密码!");
}
} else {
JOptionPane.showMessageDialog(null, "未输入密码!");
}
} else {
JOptionPane.showMessageDialog(null, "未输入用户名!");
}
}
}
/**
* Create the frame.
public depositFrame(final String name) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 492, 354);
setTitle(name);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u8F93\u5165\u91D1\u989D\uFF1A");
lblNewLabel.setFont(new Font("新宋体", Font.BOLD, 20));
lblNewLabel.setBounds(80, 92, 121, 27);
contentPane.add(lblNewLabel);
inputField = new JTextField();
inputField.setBounds(225, 95, 133, 24);
contentPane.add(inputField);
inputField.setColumns(10);
JButton OKButton = new JButton("\u786E\u8BA4");
OKButton.setFont(new Font("新宋体", Font.BOLD, 15));
OKButton.setBounds(102, 188, 113, 27);
contentPane.add(OKButton);
OKButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// if(money1%100==0) {
System.out.println(inputField.getText()+"存款");
if(Float.parseFloat(inputField.getText())<100000){
if(Float.parseFloat(inputField.getText())%100==0) {
user_query = ExecuteSQL.query(name);
double temp = user_query.getbalance()+Double.parseDouble(inputField.getText());
DecimalFormat df = new DecimalFormat( "0.00 ");
int i = ExecuteSQL.modifyMoney(name,temp);
if(i>0){
setVisible(false);
atmFrame frame = new atmFrame(name);
frame.setVisible(true);
JOptionPane.showMessageDialog(null, "交易成功!"+"\n" + "当前余额为:" + df.format(temp));
}else{
JOptionPane.showMessageDialog(null, "交易失败!"+"\n" + "当前余额为:" + df.format(temp));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabe = new JLabel("\u53D6\u6B3E\u91D1\u989D\uFF1A");
lblNewLabe.setFont(new Font("新宋体", Font.BOLD, 20));
lblNewLabe.setBounds(84, 75, 117, 38);
contentPane.add(lblNewLabe);
outputField = new JTextField();
outputField.setBounds(199, 84, 117, 24);
contentPane.add(outputField);
outputField.setColumns(10);
JButton OKButton = new JButton("\u786E\u5B9A");
OKButton.setFont(new Font("新宋体", Font.BOLD, 15));
OKButton.setBounds(84, 159, 113, 27);
contentPane.add(OKButton);
OKButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("withdrawFrame.withdrawFrame(...).new ActionListener() {...}.actionPerformed()"+outputField.getText());
if (Float.parseFloat(outputField.getText()) < 100000) {
if(Float.parseFloat(outputField.getText())%100==0) {
user_query = ExecuteSQL.query(name);
if (user_query.getbalance() > Double.parseDouble(outputField.getText())) {
double temp = user_query.getbalance() - Double.parseDouble(outputField.getText());
DecimalFormat df = new DecimalFormat( "0.00 ");
int i = ExecuteSQL.modifyMoney(name, temp);
if(i>0){
setVisible(false);
atmFrame frame = new atmFrame(name);
frame.setVisible(true);
JOptionPane.showMessageDialog(null, "取钱交易成功!"+"\n" + "剩余余额为:" + df.format(temp));
}else{
JOptionPane.showMessageDialog(null, "取钱交易失败!"+"\n" + "剩余余额为:" + df.format(temp));
}
} else {
JOptionPane.showMessageDialog(null, "余额不足,请重新输入!" + "\n" + "当前余额为:" + user_query.getbalance());
outputField.setText("");
}
}else {
JOptionPane.showMessageDialog(null, "系统不支持非100元整钞,请重新输入!");
outputField.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "输入金额大于100000,请重新输入!");
outputField.setText("");
}
}
} else
return;
} catch (Exception ee) {
ee.printStackTrace();
}
}
//重写executeQuer方法
//返回ResultSet结果集
private static ResultSet executeQuery(String sql) {
try {
if (conn == null)
new ExecuteSQL();
return conn.createStatement().executeQuery(sql);//ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
}
}
//重写executeUpdate方法
private static int executeUpdate(String sql) {
try {
if (conn == null)
new ExecuteSQL();
return conn.createStatement().executeUpdate(sql);
} catch (SQLException e) {
System.out.println(e.getMessage());
return -1;
} finally {
}
}
public static void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
inputField.setText("");
}
}else{
JOptionPane.showMessageDialog(null, "输入金额大于1000000,请重新输入!");
inputField.setText("");
}
}
});
JButton backButton_1 = new JButton("\u8FD4\u56DE");
backButton_1.setFont(new Font("新宋体", Font.BOLD, 15));
backButton_1.setBounds(268, 188, 113, 27);
contentPane.add(backButton_1);
backButton_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
atmFrame af = new atmFrame(name);
setVisible(false);
af.setVisible(true);
}
});
JLabel lblNewLabel_1 = new JLabel("*\u9650\u989D100000");
lblNewLabel_1.setForeground(Color.RED);
lblNewLabel_1.setFont(new Font("新宋体", Font.PLAIN, 15));
lblNewLabel_1.setBounds(369, 98, 105, 18);
contentPane.add(lblNewLabel_1);
}
}
JLabel u_nameLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
u_nameLabel.setFont(new Font("新宋体", Font.BOLD, 18));
u_nameLabel.setBounds(92, 49, 88, 37);
contentPane.add(u_nameLabel);
JLabel u_passwordLabel_1 = new JLabel("\u5BC6\u7801\uFF1A");
u_passwordLabel_1.setFont(new Font("新宋体", Font.BOLD, 18));
u_passwordLabel_1.setBounds(92, 130, 88, 27);
contentPane.add(u_passwordLabel_1);
u_name_Field = new JTextField();
u_name_Field.setBounds(184, 57, 157, 29);
contentPane.add(u_name_Field);
u_name_Field.setColumns(10);
u_password_field = new JPasswordField();
u_password_field.setBounds(184, 133, 157, 27);
contentPane.add(u_password_field);
JLabel lblNewLabel = new JLabel("\u7528\u6237\u6307\u5357");
lblNewLabel.setForeground(Color.RED);
lblNewLabel.setBounds(402, 307, 72, 31);
contentPane.add(lblNewLabel);
}
}
public static void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
//登录测试账号 密码
public static user check(String name, String password) {
int i = 0;
user u = new user();
String sql = "select name, password from bank where name = '" + name + "'";
ResultSet rs = ExecuteSQL.executeQuery(sql);
try {
while (rs.next()) {
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
// u.setbalance(rs.getFloat("balance"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ExecuteSQL.close();
return u;
}
//注册,添加用户信息
public static int addUser(String name, String password) {
int i = 0;
String sql = "insert into bank(name,password,balance)"
+ "values('" + name + "','" + password + "','" + 0 + "')";
i = ExecuteSQL.executeUpdate(sql);
ExecuteSQL.close();
return i;
}
}
}
public class depositFrame extends JFrame {
private JPanel contentPane;
private JTextField inputField;
private user user_query;
private user user_modMoney;
/**
* Create the frame.
*/
public depositFrame(final String name) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 492, 354);
setTitle(name);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u8F93\u5165\u91D1\u989D\uFF1A");
lblNewLabel.setFont(new Font("新宋体", Font.BOLD, 20));
lblNewLabel.setBounds(80, 92, 121, 27);
contentPane.add(lblNewLabel);
inputField = new JTextField();
inputField.setBounds(225, 95, 133, 24);
contentPane.add(inputField);
inputField.setColumns(10);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 527, 358);
setTitle(name);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("\u8F6C\u8D26\u7528\u6237\u540D\uFF1A");
lblNewLabel.setFont(new Font("新宋体", Font.BOLD, 20));
lblNewLabel.setBounds(105, 72, 126, 30);
contentPane.add(lblNewLabel);
out_nameField = new JTextField();
out_nameField.setBounds(245, 77, 150, 24);
contentPane.add(out_nameField);
out_nameField.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("\u8F6C\u8D26\u91D1\u989D\uFF1A");
lblNewLabel_1.setFont(new Font("新宋体", Font.BOLD, 20));
lblNewLabel_1.setBounds(105, 141, 112, 30);
contentPane.add(lblNewLabel_1);
out_moneyField = new JTextField();
out_moneyField.setBounds(243, 146, 152, 24);
contentPane.add(out_moneyField);
out_moneyField.setColumns(10);
JButton OKButton = new JButton("\u786E\u8BA4");
OKButton.setFont(new Font("新宋体", Font.BOLD, 17));
OKButton.setBounds(104, 225, 113, 27);
contentPane.add(OKButton);
OKButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int res = JOptionPane.showConfirmDialog(null, "确认此次转账?", "转账确认", JOptionPane.YES_NO_OPTION);
if (res == JOptionPane.YES_OPTION) {
user_query_deposit = ExecuteSQL.query(out_nameField.getText());//转出钱
if (out_nameField.getText().equals(user_query_deposit.getName())) {
if (Float.parseFloat(out_moneyField.getText()) < 100000) {
if(Float.parseFloat(out_moneyField.getText())%100==0) {
user_query_withdraw = ExecuteSQL.query(name);
// user_1 = ExecuteSQL.query(out_nameField);
if (user_query_withdraw.getbalance() > Double.parseDouble(out_moneyField.getText())) {
double temp = user_query_withdraw.getbalance() - Double.parseDouble(out_moneyField.getText());
double temp_1 = user_query_deposit.getbalance() + Double.parseDouble(out_moneyField.getText());
DecimalFormat df = new DecimalFormat("0.00 ");
int i_withdraw = ExecuteSQL.modifyMoney(name, temp);
int i_deposit = ExecuteSQL.modifyMoney(out_nameField.getText(), temp_1);
if(i_withdraw>0 && i_deposit>0){
setVisible(false);
atmFrame frame = new atmFrame(name);
}
} else {
JOptionPane.showMessageDialog(null, "输入金额大于100000,请重新输入!");
outputField.setText("");
}
}
});
JButton backButton_1 = new JButton("\u8FD4\u56DE");
backButton_1.setFont(new Font("新宋体", Font.BOLD, 15));
backButton_1.setBounds(233, 159, 113, 27);
contentPane.add(backButton_1);
backButton_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
atmFrame af = new atmFrame(name);
setVisible(false);
af.setVisible(true);
}
});
JLabel remindLabel = new JLabel("*\u5355\u7B14\u6700\u5927\u53EF\u53D610000");
remindLabel.setForeground(Color.RED);
remindLabel.setBounds(199, 126, 138, 18);
contentPane.add(remindLabel);
}
}
public class SignFrame extends JFrame {
private JPanel contentPane;
private JTextField u_name;
backButton_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
atmFrame af = new atmFrame(name);
setVisible(false);
af.setVisible(true);
}
});
}
}
public class ExecuteSQL {
protected static String dbClassName = "com.mysql.jdbc.Driver";
protected static String dbUrl = "jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull";
protected static String dbUser = "root";
protected static String dbPwd = "123456";
private static Connection conn = null;
private ExecuteSQL() {
try {
if (conn == null) {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} else
return;
} catch (Exception ee) {
ee.printStackTrace();
}
}
public class atmFrame extends JFrame {
private JPanel contentPane;
private user user;
// private LoginFrame lf;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
atmFrame frame = new atmFrame(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public atmFrame(final String name) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 518, 434);
contentPane = new JPanel();
setTitle(name);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setLocationRelativeTo(null);
setContentPane(contentPane);
contentPane.setLayout(null);
JButton queryButton = new JButton("\u67E5\u8BE2");
queryButton.setBounds(99, 101, 113, 27);
contentPane.add(queryButton);
queryButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
user = ExecuteSQL.query(name);
DecimalFormat df = new DecimalFormat( "0.00 ");
JOptionPane.showMessageDialog(null,
"您的账户名:"+user.getName()+"\n"+"当前余额:¥"+user.getbalance()+"\n","信息提示",
JOptionPane.INFORMATION_MESSAGE);
}
});
JButton depositButton = new JButton("\u5B58\u6B3E");
depositButton.setBounds(99, 223, 113, 27);
contentPane.add(depositButton);
return -1;
} finally {
}
}
public static void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn = null;
}
}
//登录测试账号 密码
public static user check(String name, String password) {
int i = 0;
user u = new user();
String sql = "select name, password from bank where name = '" + name + "'";
ResultSet rs = ExecuteSQL.executeQuery(sql);
try {
while (rs.next()) {
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
// u.setbalance(rs.getFloat("balance"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ExecuteSQL.close();
return u;
}
//注册,添加用户信息
public static int addUser(String name, String password) {
int i = 0;
String sql = "insert into bank(name,password,balance)"
+ "values('" + name + "','" + password + "','" + 0 + "')";
i = ExecuteSQL.executeUpdate(sql);
ExecuteSQL.close();
}
public class ExecuteSQL {
protected static String dbClassName = "com.mysql.jdbc.Driver";
protected static String dbUrl = "jdbc:mysql://localhost:3306/project?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull";
protected static String dbUser = "root";
protected static String dbPwd = "123456";
private static Connection conn = null;
private ExecuteSQL() {
try {
if (conn == null) {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} else
return;
} catch (Exception ee) {
ee.printStackTrace();
}
}
//重写executeQuer方法
//返回ResultSet结果集
private static ResultSet executeQuery(String sql) {
try {
if (conn == null)
new ExecuteSQL();
return conn.createStatement().executeQuery(sql);//ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE
} catch (SQLException e) {
e.printStackTrace();
return null;
} finally {
}
}
//重写executeUpdate方法
private static int executeUpdate(String sql) {
try {
inputField.setColumns(10);
JButton OKButton = new JButton("\u786E\u8BA4");
OKButton.setFont(new Font("新宋体", Font.BOLD, 15));
OKButton.setBounds(102, 188, 113, 27);
contentPane.add(OKButton);
OKButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
// if(money1%100==0) {
System.out.println(inputField.getText()+"存款");
if(Float.parseFloat(inputField.getText())<100000){
if(Float.parseFloat(inputField.getText())%100==0) {
user_query = ExecuteSQL.query(name);
double temp = user_query.getbalance()+Double.parseDouble(inputField.getText());
DecimalFormat df = new DecimalFormat( "0.00 ");
int i = ExecuteSQL.modifyMoney(name,temp);
if(i>0){
setVisible(false);
atmFrame frame = new atmFrame(name);
frame.setVisible(true);
JOptionPane.showMessageDialog(null, "交易成功!"+"\n" + "当前余额为:" + df.format(temp));
}else{
JOptionPane.showMessageDialog(null, "交易失败!"+"\n" + "当前余额为:" + df.format(temp));
}
}else {
JOptionPane.showMessageDialog(null, "系统不支持非100元整钞,请重新输入!");
inputField.setText("");
}
}else{
JOptionPane.showMessageDialog(null, "输入金额大于1000000,请重新输入!");
inputField.setText("");
}
}
});
JButton backButton_1 = new JButton("\u8FD4\u56DE");
backButton_1.setFont(new Font("新宋体", Font.BOLD, 15));
backButton_1.setBounds(268, 188, 113, 27);
contentPane.add(backButton_1);