System.out.println("对不起,账户余额不足!
");
}else{
balance-=money;
}
}
}
}
DBoper.java
packageDB;
importjava.sql.*;
importjava.util.ArrayList;
importAccount.accounts;
publicclassDBoper{
privateConnectionconn=null;
privateStatementst=null;
privatePreparedStatementpstmt=null;
privateResultSetrs=null;
privateArrayListresult;
//连接数据库
publicConnectiongetConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:
mysql:
//localhost:
3306/bankmanager?
useUnicod
e=true&characterEncoding=utf8","root","xuewei");
}catch(Exceptione){
System.out.println("数据库连接失败");
}
returnconn;
}
//修改删除用户数据
publicbooleanaccountDataUpdate(Stringsql){
conn=getConnection();
try{
pstmt=conn.prepareStatement(sql);
pstmt.executeUpdate();
//System.out.println("数据更新成功");
conn.close();
returntrue;
}catch(SQLExceptione){
e.printStackTrace();
//System.out.println("更新失败");
returnfalse;
}
}
//依据id来修改记录
publicbooleandataupdateid(accountsuser,intid){
conn=getConnection();
try{
Stringsql="updateaccountset
username=?
userpwd=?
personId=?
accountType=?
balance=?
ceiling=?
whereid="
+id;
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,user.getName());
pstmt.setString(2,user.getPassword());
pstmt.setString(3,user.getPersonId());
pstmt.setInt(4,user.getAccountType());
pstmt.setDouble(5,user.getBalance());
pstmt.setDouble(6,user.getCeiling());
pstmt.executeUpdate();
System.out.println("操作成功");
returntrue;
}
catch(SQLExceptione){
e.printStackTrace();
System.out.println("操作失败");
returnfalse;
}
}
//插入用户数据
publicbooleanaccountDataInsert(accountsaccount){
conn=getConnection();
try{
pstmt=conn.prepareStatement("insertintoaccount
(username,userpwd,personId,accountType,balance,ceiling)values(?
?
?
?
?
?
)");
pstmt.setString(1,account.getName());
pstmt.setString(2,account.getPassword());
pstmt.setString(3,account.getPersonId());
pstmt.setInt(4,account.getAccountType());
pstmt.setDouble(5,account.getBalance());
pstmt.setDouble(6,account.getCeiling());
pstmt.executeUpdate();
System.out.println("新用户数据插入成功");
conn.close();
returntrue;
}catch(SQLExceptione){
e.printStackTrace();
System.out.println("插入失败");
returnfalse;
}
}
//查询数据
publicResultSetdataquery(Stringsql){
conn=getConnection();
try{
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
}
catch(SQLExceptione){
e.printStackTrace();
System.out.println("检索失败");
}
returnrs;
}
publicResultSettestlist(){
try{
Stringsql="select*fromaccountwhereid=1001";
pstmt=conn.prepareStatement(sql);
System.out.println(sql);
rs=pstmt.executeQuery();
}catch(SQLExceptione){
e.printStackTrace();
System.out.println("检索失败");
}
returnrs;
}
publicArrayListtestt(intid){
ArrayListlist=newArrayList();
conn=getConnection();
try{
Stringsql="select*fromaccountwhereid="+id;
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
accountsuser=newaccounts();
user.setName(rs.getString("username"));
user.setPassword(rs.getString("userpwd"));
user.setPersonId(rs.getString("personId"));
user.setAccountType(rs.getInt("accountType"));
user.setBalance(rs.getDouble("balance"));
user.setCeiling(rs.getDouble("ceiling"));
list.add(user);
}
}
catch(SQLExceptione){
e.printStackTrace();
System.out.println("检索失败");
}
returnlist;
}
//根据卡号和密码验证用户是否存在
publicbooleanverify(intid,Stringpassword){
conn=getConnection();
try{
pstmt=conn.prepareStatement("select*fromaccountwhereid=?
and
userpwd=?
");
pstmt.setInt(1,id);
pstmt.setString(2,password);
rs=pstmt.executeQuery();
if(rs.next()){
returntrue;
}else{
returnfalse;
}
}catch(SQLExceptione){
e.printStackTrace();
returnfalse;
}
}
/**
*登录验证
*/
publicaccountsverifyAccount(intid,Stringpassword){
booleanflag=verify(id,password);
if(flag){
accountsaccount=queryByIDandPassword(id,password);
returnaccount;
}
else
{
System.out.println("用户不存在!
");
returnnull;
}
}
//根据卡号和密码查询信息
publicaccountsqueryByIDandPassword(intid,Stringpassword){
conn=getConnection();
try{
pstmt=conn.prepareStatement("select*fromaccountwhereid=?
and
userpwd=?
");
pstmt.setInt(1,id);
pstmt.setString(2,password);
rs=pstmt.executeQuery();
accountsaccount=newaccounts();
while(rs.next()){
account.setId(rs.getInt("id"));
account.setName(rs.getString("username"));
account.setPassword(rs.getString("userpwd"));
account.setPersonId(rs.getString("personId"));
account.setAccountType(rs.getInt("accountType"));
account.setBalance(rs.getDouble("balance"));
account.setCeiling(rs.getDouble("ceiling"));
}
returnaccount;
}catch(SQLExceptione){
e.printStackTrace();
returnnull;
}
}
//根据卡号查询信息
publicaccountsqueryByID(intid){
conn=getConnection();
try{
pstmt=conn.prepareStatement("select*fromaccountwhereid=?
");
pstmt.setInt(1,id);
rs=pstmt.executeQuery();
accountsaccount=newaccounts();
while(rs.next()){
account.setId(rs.getInt("id"));
account.setName(rs.getString("username"));
account.setPassword(rs.getString("userpwd"));
account.setPersonId(rs.getString("personId"));
account.setAccountType(rs.getInt("accountType"));
account.setBalance(rs.getDouble("balance"));
account.setCeiling(rs.getDouble("ceiling"));
}
returnaccount;
}catch(SQLExceptione){
e.printStackTrace();
returnnull;
}
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
DBoperop=newDBoper();
ArrayListlist=op.testt(1001);
for(inti=0;iaccountsuser=newaccounts();
user=list.get(i);
System.out.println(user.getName());
System.out.println(user.getPassword());
System.out.println(user.getPersonId());
System.out.println(user.getAccountType());
System.out.println(user.getBalance());
System.out.println(user.getCeiling());
}
}
}
//图像界面文件夹
DN_SYSTEM
Default.java//主界面
packageDN_SYSTEM;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.WindowConstants;
importAccount.accounts;
importDB.DBoper;
publicclassDefaultextendsJFrameimplementsActionListener{
accountsaccount=DN_ATM.client;
DBoperdb=newDBoper();
privateJFramejf;
privateJButtonbtn0,btn1,btn2,btn3,btn4,btn5;
privateContainerc;
publicvoiddefaltDemo(){
//添加主界面卡片
//DN_ATM.id;
intid=DN_ATM.id;
//account=db.queryByID(id);
jf=newJFrame("DEFAULT");
c=jf.getContentPane();
c.setLayout(null);
c.setBackground(newColor(119,119,253));
JLabellblwel=newJLabel("欢迎"+account.getName()+"用达能银行ATM系统");
lblwel.setBounds(20,15,350,30);
c.add(lblwel);
JLabellblwel1=newJLabel("请选择您需要的交易类型");
lblwel1.setBounds(120,45,350,30);
c.add(lblwel1);
btn1=newJButton("取款服务");
btn1.addActionListener(this);
btn1.setBounds(70,90,100,30);
c.add(btn1);
btn2=newJButton("存款服务");
btn2.addActionListener(this);
btn2.setBounds(210,90,100,30);
c.add(btn2);
btn3=newJButton("查询余额");
btn3.addActionListener(this);
btn3.setBounds(70,150,100,30);
c.add(btn3);
btn4=newJButton("转账服务");
btn4.addActionListener(this);
btn4.setBounds(210,150,100,30);
c.add(btn4);
btn5=newJButton("注销账户");
btn5.addActionListener(this);
btn5.setBounds(70,210,100,30);
c.add(btn5);
btn0=newJButton("退出操作");
btn0.addActionListener(this);
btn0.setBounds(210,210,100,30);
c.add(btn0);
jf.setVisible(true);
jf.setResizable(false);
jf.setSize(400,300);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
//事件处理
@Override
publicvoidactionPerformed(ActionEvente){
//取款服务
if(e.getSource().equals(btn1)){
OutMoneyoutmoney=newOutMoney();
outmoney.outMoney();
jf.dispose();
}
//存款服务
if(e.getSource().equals(btn2)){
InMoneyinmoney=newInMoney();
inmoney.InMoney();
jf.dispose();
}
//查询余额
if(e.getSource().equals(btn3)){
GetBalancegetbalanc