源码:
db包:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlHelp {
//定义操作数据库连接
PreparedStatement ps=null;
Connection ct=null;
ResultSet rs=null;
//查询数据库的操作
public ResultSet query(String sql,String []paras)
{
try{
//1加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2得到连接
ct=DriverManager.getConnection
("jdbc:sqlserver://localhost:1433;databaseName=Atm","sa","liujikun");
//3创建ps
ps=ct.prepareStatement(sql);
//给ps的问号(?)赋值
for(int i=0;i<paras.length;i++)
{
ps.setString(i+1,paras[i] );
}
rs=ps.executeQuery();
}catch(Exception e){
e.printStackTrace();
}finally{
}
return rs;
}
//修改,查询,删除
public boolean iduExcute(String sql,String []paras)
{
boolean b=true;
try{
//1加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2得到连接
ct=DriverManager.getConnection
("jdbc:sqlserver://localhost:1433;databaseName=Atm","sa","liujikun");
//3创建ps
ps=ct.prepareStatement(sql);
//给ps的问号(?)赋值
for(int i=0;i<paras.length;i++)
{
ps.setString(i+1,paras[i] );
}
//4执行操作
ps.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
this.close();
}
return b;
}
//关闭数据库资源
public void close()
{
try {
if(ps!=null)
{
ps.close();
}
if(ct!=null)
{
ct.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
model包:
/**
package model;
import java.sql.*;
import java.util.Vector;
import javax.swing.JTextField;
import javax.swing.table.*;
import view.Deposit;
import db.SqlHelp;
public class BankModel {
//取款
Deposit dp;
public boolean isertStu(String uid,String money)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
dp=new Deposit(uid);
String sql="update usersLogin set usemoney=usemoney+ ? where useId= ?";
String []paras={money,uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//(通个id号来修改)100元
public boolean updatStu(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin set usemoney=usemoney-'100' where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//200元
public boolean updatStu1(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin set usemoney=usemoney-'200' where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//300
public boolean updatStu2(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin set usemoney=usemoney-'300' where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//500
public boolean updatStu3(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin set usemoney=usemoney-'500' where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//1000
public boolean updatStu4(String uid)
{
boolean b=true;
SqlHelp shl=new SqlHelp();
String sql="update usersLogin set usemoney=usemoney-'1000' where useId= ?";
String []paras={uid};
try{
b=shl.iduExcute(sql, paras);
}catch(Exception e){
e.printStackTrace();
}finally{
shl.close();
}
return b;
}
//查询
public String checkUser(String uid)
{
String usemoney=null;
SqlHelp sq1=null;
try{
//组织sql语句和参数列表
String sql="select usemoney from usersLogin where useId=?";
String paras[]={uid};
sq1=new SqlHelp();
ResultSet rs=sq1.query(sql, paras);
if(rs.next())
{
//取出钱
usemoney=rs.getString(1);
System.out.println(usemoney);
}
}catch(Exception e){
e.printStackTrace();
}finally{
sq1.close();
}
return usemoney;
}
}
**
* 用户登录时的后台逻辑部分
* */
package model;
import java.sql.*;
import db.SqlHelp;
public class UseModel {
//
public String checkUser(String uid,String p)
{
String user=null;
SqlHelp sq1=null;
try{
//组织sql语句和参数列表
String sql="select usersLogin.useName from users,usersLogin where users.useId=usersLogin.useId and users.useId=? and users.passwd=?";
String paras[]={uid,p};
sq1=new SqlHelp();
ResultSet rs=sq1.query(sql, paras);
if(rs.next())
{
//取出名字
user=rs.getString(1);
System.out.println(user);
//return user;
}
}catch(Exception e){
e.printStackTrace();
}finally{
sq1.close();
}
return user;
}
}
view包:
/**
* 查询页面
* */
package view;
import javax.swing.*;
import model.BankModel;
import java.awt.*;
import java.awt.event.*;
public class ChaXun extends JFrame implements ActionListener{
JButton jb;
JTextField jtf;
JPanel jp;
String uid;
BankModel bm;
public ChaXun(String uid){
this.uid=uid;
bm =new BankModel();
//bm.checkUser(uid);
jtf=new JTextField("您的余额为:"+bm.checkUser(uid));
jb=new JButton("返回");
jb.addActionListener(this);
//System.out.print();
jp=new JPanel(new BorderLayout());
jp.add(jtf,"West");
jp.add(jb,"East");
this.add(jp);
this.setUndecorated(true);
this.setSize(300,200);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public ChaXun()
{
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("返回"))
{
new Window(uid);
this.dispose();
}
}
}
/**存钱
* */
package view;
import javax.swing.*;
import model.BankModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Deposit extends JFrame implements ActionListener{
/**
* @param args
*/
// public static void main(String[] args) {
// // TODO Auto-generated method stub
//
// Deposit dp=new Deposit();
// }
JLabel jl;
JTextField jtf;
JButton jb1,jb2;
JPanel jp;
String uid;
BankModel bm;
public Deposit(String uid)
{
this.uid=uid;
jp=new JPanel(new BorderLayout());
jl=new JLabel("请放入钞票:");
jtf=new JTextField();
jb1=new JButton("确定");
jb1.addActionListener(this);
jb2=new JButton("返回");
jb2.addActionListener(this);
jp.add(jl,"West");
jp.add(jtf,"Center");
jp.add(jb1,"East");
jp.add(jb2,"South");
this.add(jp);
this.setSize(300,100);
this.setUndecorated(true);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb2)
{
new Window(uid);
this.dispose();
}else if(e.getSource()==jb1)
{
System.out.print(uid);
bm=new BankModel();
String money=jtf.getText();
bm.isertStu(uid, money);
//bm.isertStu(uid);
//System.out.println(bm.isertStu(uid));
System.out.println("Deposit="+jtf.getText());
JOptionPane.showMessageDialog(this, "存款成功");
}
}
}
/**
* 用户登录
* */
package view;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import model.UseModel;
public class UserLogin extends JFrame implements ActionListener{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserLogin us=new UserLogin();
}
JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb10,jb11;
JLabel jl1,jl2,jl3;
JPanel jp,jp1,jp2;
JTextField jName;
JPasswordField jPasswd;
//String jtf="123";
//String jpd="123456";
public UserLogin()
{
jp=new JPanel();
jl1=new JLabel("用户名:");
jName=new JTextField(10);
jName.setBorder(BorderFactory.createLoweredBevelBorder());//设置下凹感觉
jl2=new JLabel("密 码:");
jPasswd=new JPasswordField(10);
jPasswd.setBorder(BorderFactory.createLoweredBevelBorder());//设置下凹感觉
jb1=new JButton("登录");
jb1.addActionListener(this);
jb2=new JButton("取消");
jb2.addActionListener(this);
jp.add(jl1);
jp.add(jName);
jp.add(jl2);
jp.add(jPasswd);
jp.add(jb1);
jp.add(jb2);
this.add(jp);
this.setUndecorated(true);
this.setVisible(true);
this.setSize(200,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-150,height/2-150);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jb1)
{
//取出id和密码
String uid=this.jName.getText().trim();
String p=new String(this.jPasswd.getPassword());
System.out.println(uid+p);
UseModel um=new UseModel();
String str=um.checkUser(uid,p);
//System.out.println("login="+str);
System.out.println(str.length()!=0);
if(str.length()!=0)
{
System.out.println("通过");
new Window(uid);
this.dispose();
JOptionPane.showMessageDialog(this, "欢迎光临");
}
}else if(e.getSource()==jb2)
{
System.out.print("取消中");
this.dispose();
}
}
}
/**主界面
* */
package view;
import javax.swing.*;
import model.BankModel;
import java.awt.*;
import java.awt.event.*;
public class Window extends JFrame implements ActionListener{
/**
* @param args
*/
// public static void main(String[] args) {
// // TODO Auto-generated method stub
//
// Window wd=new Window(id);
// }
JButton jb1,jb2,jb3,jb4,jb5;
JLabel jl1,jl2;
JPanel jp,jp1,jp2,jp3;
BankModel bm;
String uid;
public Window(String uid)
{
this.uid=uid;
//Container ct=this.getContentPane();
jp=new JPanel(new GridLayout(3,3));
jb1=new JButton("查询");
jb1.addActionListener(this);
jb2=new JButton("存款");
jb2.addActionListener(this);
jp1=new JPanel(new BorderLayout());
jp1.add(jb1,"West");
jp1.add(jb2,"East");
jb3=new JButton("取款");
jb3.addActionListener(this);
jb4=new JButton("转账");
jb4.addActionListener(this);
jp2=new JPanel(new BorderLayout());
jp2.add(jb3,"West");
jp2.add(jb4,"East");
jp3=new JPanel(new BorderLayout());
jb5=new JButton("取卡");
jb5.addActionListener(this);
jp3.add(jb5,"East");
this.setUndecorated(true);
jp.add(jp1);
jp.add(jp2);
jp.add(jp3);
this.add(jp);
this.setSize(500,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("查询"))
{
System.out.println("用户想查询");
//因为把对表的数据封装到StuModel中,所以我们可以比较简单的完成查询任务
//trim()过滤空字符串
//String Id=this.jtf.getText().trim();
//写一个sql语句
System.out.println("查询编号"+uid);
new ChaXun(uid);
//bm =new BankModel();
//bm.checkUser(uid);
//System.out.println(bm);
//String str=bm.checkUser(uid, p);
}
else if(e.getActionCommand().equals("取卡"))
{
this.dispose();
JOptionPane.showMessageDialog(this, "祝您使用愉快");
new UserLogin();
}else if(e.getActionCommand().equals("取款"))
{
new WithdrawMoney(uid);
System.out.print(uid);
}else if(e.getActionCommand().equals("存款"))
{
new Deposit(uid);
}else if(e.getActionCommand().equals("转账"))
{
JOptionPane.showMessageDialog(this, "对不起,该业务已暂停服务");
}
}
}
/**
* 取钱
* */
package view;
import javax.swing.*;
import model.BankModel;
import java.awt.*;
import java.awt.event.*;
public class WithdrawMoney extends JFrame implements ActionListener{
/**
* @param args
*/
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// WithdrawMoney wm=new WithdrawMoney(uid);
// }
JButton jb1,jb2,jb3,jb4,jb5,jb6;
JPanel jp,jp1,jp2,jp3;
String uid;
BankModel bm;
public WithdrawMoney(String uid)
{
this.uid=uid;
jp=new JPanel(new GridLayout(3,3));
jb1=new JButton("100");
jb1.addActionListener(this);
jb2=new JButton("200");
jb2.addActionListener(this);
jp1=new JPanel(new BorderLayout());
jp1.add(jb1,"West");
jp1.add(jb2,"East");
jb3=new JButton("300");
jb3.addActionListener(this);
jb4=new JButton("500");
jb4.addActionListener(this);
jp2=new JPanel(new BorderLayout());
jp2.add(jb3,"West");
jp2.add(jb4,"East");
jp3=new JPanel(new BorderLayout());
jb5=new JButton("1000");
jb5.addActionListener(this);
//jb5.addActionListener(this);
jb6=new JButton("返回");
jb6.addActionListener(this);
jp3.add(jb5,"West");
jp3.add(jb6,"East");
this.setUndecorated(true);
jp.add(jp1);
jp.add(jp2);
jp.add(jp3);
this.add(jp);
this.setSize(500,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
int width=Toolkit.getDefaultToolkit().getScreenSize().width;
int height=Toolkit.getDefaultToolkit().getScreenSize().height;
this.setVisible(true);
this.setLocation(width/2-250,height/2-250);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("返回"))
{
new Window(uid);
this.dispose();
}else if(e.getActionCommand().equals("100"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("200"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu1(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("300"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu2(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("500"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu3(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}else if(e.getActionCommand().equals("1000"))
{
bm =new BankModel();
//bm.updatStu(uid);
System.out.println(uid);
System.out.print(bm.updatStu4(uid));
JOptionPane.showMessageDialog(this, "取款成功");
}
}
}
本人在写改代码时用了mvc模式,把数据库的链接单独封了一个包,把银行中存钱,查询,取钱封到了后台逻辑中model包中,然后其次就是图形用户界面有查询、取款、存款的界面封到了view包中,因为是新手,所以代码瑕疵很多,请各位大神多多包含给一定的指导评论,谢谢