JSP虚拟主机,jsp空间,java空间,java虚拟空间JSP虚拟主机,jsp空间,java空间,java虚拟空间

ATM银行取款机模拟程序(可连接数据库)



作者:    文章来源:
发布日期:2007年01月09日
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.sql.*;
import java.net.URL;

public class DBAccess extends JFrame implements ActionListener
{


//JDBC驱动器名称(JDBC-ODBC桥)
private String strDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
//数据库的URL地址
private String strUrl = "jdbc:odbc:atm1";
//指定要访问的数据库名称
private String strDBName = "atm1";
//数据库用户名称
private String strUserName = "testUser";
//数据库用户密码
private String strPassWord = "12345";
//数据库连接对象,初始化为空
private Connection conTemp = null;
//连接的容器对象,初始化为空
private Statement stmtTemp = null;
//结果集对象,初始化为空
private ResultSet rsTemp = null;
//操作记录行数
private int intOperateNum = 0;


public static void main(String[] args)
{

DBAccess d=new DBAccess();

d.setSize(400,300);
d.setLocation(300,200);
d.setTitle("欢迎光临杰宇阁银行");
d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
d.show();
// f.show();

}

/**-----------------------------------------
*
功能概述:构造方法,加载指定缺省的驱动程序
-------------------------------------------*/
public DBAccess(){
try{
Class.forName(strDBDriver);
}
catch(Exception e){
e.printStackTrace();
}
showCardLayout();
}//DBAccess()

/**-----------------------------------------
*
功能概述:和数据库建立连接
-------------------------------------------*/
public boolean createConnection(){
boolean blConnect = false;
try{
conTemp = DriverManager.getConnection(strUrl,strUserName,strPassWord);
conTemp.setCatalog(strDBName);
conTemp.setAutoCommit( false );
blConnect = true;
}
catch(Exception e){
e.printStackTrace();
}
return blConnect;
}//createConnection()

/**-----------------------------------------
*
功能概述:获得查询结果集
-------------------------------------------*/
public ResultSet executeQuery(String strSQL){
try{
if(conTemp==null){
createConnection();
}
stmtTemp = conTemp.createStatement();
rsTemp = stmtTemp.executeQuery(strSQL);
return rsTemp;
}
catch(Exception e){
e.printStackTrace();
return null;
}
}//executeQuery()

/**-----------------------------------------
*
功能概述:对数据库执行更新、插入操作
-------------------------------------------*/
public boolean executeUpdate(String strSQL){
try{
if(conTemp==null){
createConnection();
}
stmtTemp = conTemp.createStatement();
intOperateNum = stmtTemp.executeUpdate(strSQL);
return true;
}
catch(Exception e){
e.printStackTrace();
return false;
}
}//executeUpdate()

/**-----------------------------------------
*
功能概述:返回操作记录行数
-------------------------------------------*/
public int getDBOperateNum(){
return intOperateNum;
}//getDBOperateNum()

/**-----------------------------------------
*
功能概述:提交事务
-------------------------------------------*/
public void commit(){
try{
conTemp.commit();
}
catch( Exception e ){
e.printStackTrace();
}
}//commit()

/**-----------------------------------------
*
功能概述:断开和数据库建立的连接
-------------------------------------------*/
public void closeDBConnection(){
try{
if(rsTemp!= null){
rsTemp = null;
}
if(conTemp!=null){
conTemp = null;
}
if(stmtTemp!=null){
stmtTemp = null;
}
}
catch(Exception e){
e.printStackTrace();
}
}//closeDBConnection()


//查询余额
public long getmoney(String getname)
{

DBAccess d=new DBAccess();
try{
String strSQLSelect = "SELECT balance FROM atm1 WHERE cardname = '"+getname+"'";
ResultSet rsSQLSelect = d.executeQuery(strSQLSelect);
if(rsSQLSelect.next())

money = rsSQLSelect.getLong("balance");
d.commit();
}catch(Exception e){};
return money;
}



//登陆
public boolean login(String name,int pword)
{

DBAccess d=new DBAccess();
try{
String strSQLSelect = "SELECT password FROM atm1 WHERE cardname = '"+name+"'";
ResultSet rsSQLSelect = d.executeQuery(strSQLSelect);
if(rsSQLSelect.next())
{
int pwd = rsSQLSelect.getInt("password");
d.commit();
if(pword==pwd)
return true;
}
}catch(Exception e){};
return false;

}


//修改密码
public boolean alterpassword(int pword,String name)
{
DBAccess d=new DBAccess();

String strSQLUpdate = "UPDATE atm1 Set password ="+pword+" WHERE cardname = '"+name+"'";
boolean bol=d.executeUpdate(strSQLUpdate);
d.commit();
return bol;
}


//取款
public boolean setmoney(Long moneys,String name)
{

DBAccess d=new DBAccess();

long m;
m=getmoney(name);
if(m>moneys)
{
m=m-moneys;
String strSQLUpdate = "UPDATE atm1 Set balance ="+m+" WHERE cardname = '"+name+"'";
d.executeUpdate(strSQLUpdate);
d.commit();
return true;
}
else
return false;
}

//查询用户名
public boolean getname(String name)
{
String aname="";

DBAccess d=new DBAccess();
try{
String strSQLSelect = "SELECT cardname FROM atm1 ";
ResultSet rsSQLSelect = d.executeQuery(strSQLSelect);
if(rsSQLSelect.next())
{
aname = rsSQLSelect.getString("cardname");
// d.commit();
if(name==aname)
return true;

}
}catch(Exception e){};


return false;

}


//注册
public boolean regist(String names,int pword)
{

DBAccess d=new DBAccess();

boolean bol;
bol=getname(names);
if(bol)
{
return false;
}
else
{
String strSQLInsert = "INSERT INTO atm1 VALUES('"+names+"',"+pword+",1515520)";
d.executeUpdate(strSQLInsert);
d.commit();
return true;
}
}



String username;
int password;
boolean flag=false,m=false;

long money,balance; String myid;

JLabel b1=new JLabel("欢迎光临杰宇阁银行");
JLabel b2=new JLabel("请输入您的帐号");
JLabel b3=new JLabel("请输入您的密码");
JLabel b4=new JLabel("请输入您要的取款数");
JLabel b5=new JLabel(" 请输入您的新密码");
JLabel b6=new JLabel("请再次输入您的新密码");
JLabel b7=new JLabel(" ");
JLabel b8=new JLabel(" ");
JLabel b9=new JLabel("请选择您所需要的服务");
JLabel b10=new JLabel(" ");
JLabel b11=new JLabel("请输入您的帐号");
JLabel b12=new JLabel("请输入您的密码");
JLabel b13=new JLabel(" ");

JButton t1=new JButton("返回");
JButton t12=new JButton("返回");
JButton t13=new JButton("返回");
JButton t14=new JButton("返回");
JButton t15=new JButton("返回");
JButton t16=new JButton("返回");
JButton t17=new JButton("返回");

JButton t2=new JButton("进入");
JButton t3=new JButton("退出");
JButton t4=new JButton("确定");
JButton t5=new JButton("取款");
JButton t6=new JButton("修改密码");
JButton t7=new JButton("信息查询");
JButton t8=new JButton("确认");
JButton t9=new JButton("确认");
JButton t10=new JButton("注册新用户");
JButton t11=new JButton("注册");


JTextField d1=new JTextField(6); // 用户帐号
JPasswordField d2=new JPasswordField(6); // 用户密码
JTextField d3=new JTextField(6); // 取款金额
JPasswordField d4=new JPasswordField(6); // 新密码输入
JPasswordField d5=new JPasswordField(6); // 新密码确认
JTextArea d6=new JTextArea(5,17);
JTextField d7=new JTextField(6);
JPasswordField d8=new JPasswordField(6);

JPanel p1=new JPanel(); // 主面板
JPanel a1=new JPanel();
JPanel a2=new JPanel();
JPanel a3=new JPanel();

JPanel p2=new JPanel(); // 登陆面板
JPanel bb1=new JPanel();
JPanel bb2=new JPanel();
JPanel bb3=new JPanel();
JPanel bb4=new JPanel();


JPanel p3=new JPanel(); // 功能选择面板
JPanel c1=new JPanel();
JPanel c2=new JPanel();
JPanel c3=new JPanel();


JPanel p4=new JPanel(); // 取款面板
JPanel dd1=new JPanel();
JPanel dd2=new JPanel();
JPanel dd3=new JPanel();
JPanel dd4=new JPanel();


JPanel p5=new JPanel(); // 修改密码面板
JPanel e1=new JPanel();
JPanel e2=new JPanel();
JPanel e3=new JPanel();
JPanel e4=new JPanel();

JPanel p6=new JPanel(); // 信息查询面板

JPanel p7=new JPanel(); //用户注册
JPanel g1=new JPanel();
JPanel g2=new JPanel();
JPanel g3=new JPanel();
JPanel g4=new JPanel();


Container c=getContentPane();
CardLayout f1=new CardLayout();
GridLayout f2=new GridLayout(5,1);
public void showCardLayout()
{

d6.setLineWrap(true);
d6.setEditable(false);

c.setLayout(f1);
p1.setLayout(f2);
p2.setLayout(f2);
p3.setLayout(f2);
p4.setLayout(f2);
p5.setLayout(f2);
p6.setLayout(f2);
p7.setLayout(f2);


p1.add(a1);p1.add(a2);p1.add(a3);
a1.add(b1); a2.add(t2); a2.add(t3); a3.add(t10);


p2.add(bb1);p2.add(bb2);p2.add(bb3);p2.add(bb4);
bb1.add(b2); bb1.add(d1); bb2.add(b3); bb2.add(d2); bb3.add(t4); bb3.add(t16); bb4.add(b7);


p3.add(c1);p3.add(c2);p3.add(c3);
c1.add(b9); c2.add(t5); c2.add(t6); c2.add(t7); c3.add(t14);

p4.add(dd1);p4.add(dd2);p4.add(dd3);p4.add(dd4);
dd1.add(b4); dd2.add(d3); dd3.add(t8); dd3.add(t12); dd4.add(b8);


p5.add(e1);p5.add(e2);p5.add(e3);p5.add(e4);
e1.add(b5); e1.add(d4); e2.add(b6); e2.add(d5); e3.add(t9); e3.add(t1); e4.add(b10);


p6.add(d6); p6.add(t15);


p7.add(g1);p7.add(g2);p7.add(g3);p7.add(g4);
g1.add(b11); g1.add(d7); g2.add(b12); g2.add(d8); g4.add(t11);g4.add(t17); g3.add(b13);


c.add(p1,"1");
c.add(p2,"2");
c.add(p3,"3");
c.add(p4,"4");
c.add(p5,"5");
c.add(p6,"6");
c.add(p7,"7");
f1.show(c,"1");






t1.addActionListener(this);
t2.addActionListener(this);
t3.addActionListener(this);
t4.addActionListener(this);
t5.addActionListener(this);
t6.addActionListener(this);
t7.addActionListener(this);
t8.addActionListener(this);
t9.addActionListener(this);
t10.addActionListener(this);
t12.addActionListener(this);
t13.addActionListener(this);
t14.addActionListener(this);
t15.addActionListener(this);
t16.addActionListener(this);
t17.addActionListener(this);
t11.addActionListener(this);
}













public void actionPerformed(ActionEvent e)
{


if(e.getSource()==t2)
{
f1.show(c,"2");
b7.setText("");
}
//////////////////////////////////////
if(e.getSource()==t3)
System.exit(0);
//////////////////////////////////////
if(e.getSource()==t4)
{
username=d1.getText().trim();
password=Integer.parseInt(d2.getText().trim());
login(username,password);

if(!login(username,password))
{
b7.setText("您所输入的帐号或密码错误。请确认后重新输入!");
d1.setText("");
d2.setText("");
}
else
{
b7.setText("");
f1.show(c,"3");
d1.setText("");
d2.setText("");

}
}
///////////////////////////////////////
if(e.getSource()==t5)
f1.show(c,"4");
//////////////////////////////////////
if(e.getSource()==t6)
f1.show(c,"5");
//////////////////////////////////////
if(e.getSource()==t7)
f1.show(c,"6");
/////////////////////////////////////
if(e.getSource()==t8)
{

long text3=Integer.parseInt(d3.getText().trim());
if(setmoney(text3,username))
b8.setText("取款成功!");

else
b8.setText("您的余额不足,无法取款,请重新输入取款数!");
}
/////////////////////////////////////
if(e.getSource()==t9)
{
int text1=Integer.parseInt(d4.getText().trim());
int text2=Integer.parseInt(d5.getText().trim());

if(text1!=text2)
b10.setText("修改密码失败!");
else
{
alterpassword(text1,username);
b10.setText("密码修改成功!");

}
d4.setText("");
d5.setText("");
}
////////////////////////////////////
if(e.getSource()==t10)
f1.show(c,"7");
/////////////////////////////////////
if(e.getSource()==t11)
{
String usename=d7.getText().trim();
int mima=Integer.parseInt(d8.getText().trim());
if(regist(usename,mima))
{
b13.setText("注册成功");

}
else
b13.setText("你输入的用户名以被使用,请从新注册");
}
//////////////////////////////////////
if(e.getSource()==t14)
{

f1.show(c,"2");
b7.setText("");

}
//////////////////////////////////////////
if(e.getSource()==t1||e.getSource()==t12||e.getSource()==t13||e.getSource()==t15)
{
f1.show(c,"3");
b10.setText("");
b8.setText("");
d3.setText("");
}
//////////////////////////////////////////
if(e.getSource()==t16||e.getSource()==t17)
f1.show(c,"1");
///////////////////////////////////////////
d6.setText("尊敬的用户,感谢您使用杰宇阁银行,您的帐号为"+username+",你的余额为 "+getmoney(username)+" 请牢记您的密码,欢迎您下次在使用!"+"网站支持 http://MicroAnt.blogchina.com ");
}


}

Copyright © 2002-2012 JSPCN.net. All rights reserved.
JSP中文网    备案号:粤ICP备09171188号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号