第一次写博客,有点小紧张哈!本人私下学习过程中写下的一个基于Mysql数据库开发的ATM取款操作页面,拿出来跟大家一起分享,
在这里得到了很多!话不多说,先看图。
这是最新的改进篇,可以一起参考着看
http://blog.csdn.net/u011958281/article/details/75207810
1. 程序开发环境
1. javaSE-1.8
2. Mysql 5.7
3. eclipse for se
2. 开发软件功能介绍
1.账号注册 2.账户登录 3.账户查询
4.账户取款 5.账户存款 6.账户转账
package com.shao.model;
public class user {
private String name;
private String password;
private double balance;
public user() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getbalance() {
return balance;
}
public void setbalance(double balance) {
this.balance = balance;
}
}
2.数据库逻辑代码的编写
数据库主要是核心的几个数据库方法的编写,将数据库配置信息以及连接代入到重写的executeUpdate(),executeQuery()方法中,每次调用时即可连接数据库,进行数据库的操作。
package com.shao.DAO;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.PreparedStatement;
import com.shao.model.user;
public class ExecuteSQL {
protected static String dbClassName = "com.mysql.jdbc.Driver";
protected static String dbUrl = "jdbc:mysql://localhost:3306/atm";
protected static String dbUser = "root";
protected static String dbPwd = "root";
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 {
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();
}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 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;
}
}
3. 界面编写
本人使用eclipse里面自带swing插件进行界面的基本编写,为了专注功能的实现,界面的美化留待后期处理。
loginframe界面
登录用户名密码以后,确认,在数据库中做一次用户登录check,未找到注册的用户名,则提示用户注册,已经注册成功的用户可以直接进入atm操作页面。
//登录事件监听器
class loginAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
user = ExecuteSQL.check(u_name_Field.getText(), u_password_field.getText());
if (user.getName() != null) {
if (user.getPassword().equals(u_password_field.getText())) {
try {
atmFrame frame = new atmFrame(user.getName());
frame.setTitle(user.getName());
frame.setVisible(true);
setVisible(false);
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(null, "密码不正确!请重新输入");
u_name_Field.setText("");
u_password_field.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "找不到该用户,请先注册!");
u_name_Field.setText("");
u_password_field.setText("");
}
}
}
//注册事件监听器
class signAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
SignFrame frame = new SignFrame();
frame.setVisible(true);
setVisible(false);
}
}
signFrame界面
点击注册以后进入注册界面,用户填写用户名,密码,如果用户名已经注册,提示用户重新注册,密码确认两次,如果不符,则需要重新填写,针对于用户名,密码填写格式设置,我并未做过多限制,读者可以在后期继续深入限制,更期更符合实际要求。
//注册按钮监听器
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, "未输入用户名!");
}
}
}
atmFrame界面
此界面未做过多时间,主要是为四个按钮添加引导功能,导向各操作界面。
depositFrame界面
存款限额10 0000,哈哈,很有钱,银行一般不允许这么多,不怕,自己随意的,超过限额,提示,否则,直接将钱存入,返回i(数据库更新返回1),看数据库更新成功没?最后提示现在交易成功以后的余额!
这里用到两个方法query(),modifyMoney(),query()先根据输入用户名查询数据库得到对应的余额balance,然后将需要存的钱与余额想加得到一个更新以后的余额temp,最后直接将temp存入对应的账户就可以了,显示给用户现在的余额。
OKButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(Float.parseFloat(inputField.getText())<100000){
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, "输入金额大于1000000,请重新输入!");
inputField.setText("");
}
}
});
withdrawFrame界面
功能差不多,再次就不多说了,直接看源码。
OKButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (Float.parseFloat(outputField.getText()) < 100000) {
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, "输入金额大于100000,请重新输入!");
outputField.setText("");
}
}
});
transferFrame界面
A–>B A先生转300给B先生
转账,有了前面页面的铺垫,这个就好理解了,填完信息,点击确认,会弹出再一次确认信息,用户需要再一次确认,增强保护。再次确认,先检测对方账户名B填写正确,转账额度300是否大于限额,A账户余额是否大于300,否则,此次交易都不可能成功!
然后分别查询A B 余额balance_A,balance_B,然后将转账金额相加相减得到更改以后得余额temp_a,temp_b,最后进行一次modifyMoney(),修改各自账户。
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) {
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);
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_withdraw.getbalance());
out_moneyField.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "转账金额必须10000,请重新输入!");
out_moneyField.setText("");
}
} else {
JOptionPane.showMessageDialog(null, "找不到该转账用户!");
out_nameField.setText("");
}
} else {
return;
}
}
});
第一次,写的有点乱啊,后续如果有反馈或者讨论,会及时更新,谢谢大家的关注。
转载请注处http://blog.csdn.net/u011958281/article/details/73499642,希望大家多多给建议,让我有动力继续这么码下去。
源码在这里下载http://download.csdn.net/detail/u011958281/9875809