源码获取:博客首页 "资源" 里下载!
本项目是使用Java swing开发,可实现ATM系统/银行系统的基本登陆、转账、查询余额、存取款业务。界面设计比较简介
public class ExecuteSQL {
protected static String dbClassName = "com.mysql.jdbc.Driver";
protected static String dbUrl = "jdbc:mysql://localhost:3306/atm?useUnicode=true&characterEncoding=UTF-8&useSSL=false";
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;
}
}
public class LoginFrame extends JFrame {
private JPanel contentPane;
private JTextField u_name_Field;
private JPasswordField u_password_field;
private user user;
//登录事件监听器
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);
}
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginFrame() {
setTitle("\u7F51\u4E0A\u94F6\u884C");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 506, 385);
contentPane = new JPanel();
setLocationRelativeTo(null);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton LoginButton = new JButton("\u767B\u5F55");
LoginButton.setFont(new Font("新宋体", Font.BOLD, 20));
LoginButton.setBounds(92, 214, 120, 37);
contentPane.add(LoginButton);
LoginButton.addActionListener(new loginAction());
JButton signButton = new JButton("\u6CE8\u518C");
signButton.setFont(new Font("新宋体", Font.BOLD, 20));
signButton.setBounds(283, 214, 113, 37);
contentPane.add(signButton);
signButton.addActionListener(new signAction());
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 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);
depositButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
depositFrame df = new depositFrame(name);
setVisible(false);
df.setVisible(true);
}
});
JButton withdrawButton = new JButton("\u53D6\u6B3E");
withdrawButton.setBounds(288, 101, 113, 27);
contentPane.add(withdrawButton);
withdrawButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
withdrawFrame wf = new withdrawFrame(name);
setVisible(false);
wf.setVisible(true);
}
});
JButton transferButton = new JButton("\u8F6C\u8D26");
transferButton.setBounds(288, 223, 113, 27);
contentPane.add(transferButton);
transferButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
transferFrame wf = new transferFrame(name);
setVisible(false);
wf.setVisible(true);
}
});
}
}
源码获取:博客首页 "资源" 里下载!