import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.sql.*;
public class JDBC extends WindowAdapter
{
Frame win1;
MenuBar myMenu;
Menu fileMenu;
Menu helpMenu;
MenuItem fileMenuItem1;
MenuItem helpMenuItem1;
MyDialog dialog1;
TextField tx1;
TextField tx2;
Panel myP=new Panel();
Panel myP2=new Panel();
//Panel myP3=new Panel();
Label logo;
Button btn1;
Button btn2;
Button btn3;
Button btn4;
Button btn5;
Button btn6;
String username;
String password;
boolean flag=false;
String url = "jdbc:sqlserver://localhost;databaseName=ATM;user=sa;password=sa";
JDBC()
{
win1=new Frame("ATM系统 第二组程序");
win1.setLayout(new BorderLayout(1,50));
//设置菜单
myMenu=new MenuBar();
fileMenu=new Menu("文件");
helpMenu=new Menu("帮助");
myMenu.add(fileMenu);
myMenu.add(helpMenu);
fileMenuItem1=new MenuItem("退出");
helpMenuItem1=new MenuItem("版权");
myMenu.add(fileMenu);
myMenu.add(helpMenu);
fileMenu.add(fileMenuItem1);
helpMenu.add(helpMenuItem1);
ActionListener listener=new ActionListener1();
fileMenuItem1.addActionListener(listener);
helpMenuItem1.addActionListener(listener);
win1.setMenuBar(myMenu);
//设置对话框(help菜单)
dialog1=new MyDialog(win1,"ATM系统",true);
dialog1.addWindowListener(this);
//用户名及密码
tx1=new TextField(10);
tx2=new TextField(10);
//设置各个Button
win1.add(myP,BorderLayout.CENTER);
myP.setLayout(new FlowLayout(10));
myP.add(new Label("输入账户:"));
myP.add(tx1);
myP.add(new Label("输入密码:"));
myP.add(tx2);
btn1=new Button("登陆");
btn2=new Button("清除");
btn1.addActionListener(listener);
btn2.addActionListener(listener);
btn3=new Button("账户余额");
btn3.setForeground(Color.magenta);
btn3.setBackground(Color.white);
btn3.addActionListener(listener);
btn4=new Button("我要取款");
btn4.setForeground(Color.blue);
btn4.setBackground(Color.white);
btn4.addActionListener(listener);
btn5=new Button("修改密码");
btn5.setForeground(Color.red);
btn5.setBackground(Color.white);
btn5.addActionListener(listener);
btn6=new Button("退出系统");
btn6.setForeground(Color.green);
btn6.setBackground(Color.white);
btn6.addActionListener(listener);
myP.add(btn1);
myP.add(btn2);
win1.add(myP2,BorderLayout.EAST);
myP2.setLayout(new GridLayout(1,0));
myP2.add(btn3);
myP2.add(btn4);
myP2.add(btn5);
myP2.add(btn6);
logo=new Label("欢迎来到第二组的ATM系统!");
logo.setFont(new Font("Serif",Font.PLAIN,40));
win1.add(logo,BorderLayout.NORTH);
win1.setSize(600,400);
win1.setVisible(true);
win1.addWindowListener(this);
username=tx1.getText(); //``````````````````````````````````````````````
password=tx2.getText(); //`````````````````````````````````````````````````````
}
public static void main(String args[])
{
new JDBC();
//显示logo图片
Thread thread;
Logo lg=new Logo("LOGO.GIF");
thread=new Thread(lg);
thread.start();
}
public void windowClosing(WindowEvent e)
{
if(e.getSource()==win1)
{
System.exit(0);
}
if(e.getSource()==dialog1)
{
dialog1.dispose();
}
}
//登陆
public void login(String name,String paword)//如何完成登陆的功能,例如username=lwl,password=123,存款1000元,取款1000元?
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection(url);
java.sql.Statement stat=con.createStatement();
String sql = "select count(*) from atm where user='" + username + "' and pad='" + password+ "'";
System.out.println(sql);
int result=stat.executeUpdate(sql);
if(result>0)
{
flag=true;
myP.removeAll();
Label login=new Label("恭喜你,你已经登陆,可使用右面的按钮");
myP.add(login);
win1.setVisible(true);
}
else
{
tx2.setText("密码错误");
}
stat.close();
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
System.out.println("SQLException caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("error");
}
}
public void balance(String name)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection(url);
java.sql.Statement stat=con.createStatement();
String sql="select balance from atm where user=name ";
ResultSet rs=stat.executeQuery(sql);
//CallableStatement cs=con.prepareCall("{call query1(?)}");
//cs.setString(1,username);
//ResultSet rs=cs.executeQuery();
while(rs.next())
{
int balance=rs.getInt("balance");
//System.out.println(balance);
myP.removeAll();
Label account=new Label("你的余额为:");
TextField tx3=new TextField(15);
tx3.setText(""+balance);
myP.add(account);
myP.add(tx3);
win1.setVisible(true);
}
stat.close();
con.close();
}
catch(SQLException e)
{
System.out.println("SQLException caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("error");
}
}
public void takeMoney(int money,String name)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection(url);
java.sql.Statement stat=con.createStatement();
String sql="select balance from atm where user=name ";
ResultSet rs=stat.executeQuery(sql);
while(rs.next())
{
int balance=rs.getInt("balance");
if(money<=balance)
{
sql="update atm set balance=(balance-money) where user=name";
int result=stat.executeUpdate(sql);
if(result>0)
{myP.removeAll();
Label money1=new Label("交易成功");
myP.add(money1);
win1.setVisible(true);
}
}
}
stat.close();
con.close();
}
catch(SQLException e)
{
balance(username);
}
catch(ClassNotFoundException ex)
{
System.out.println("error");
}
}
public void updatePassword(String oldpassword,String newpassword,String name)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con=DriverManager.getConnection(url);
String sql="select password from atm where user=name and password=oldpassword ";
java.sql.Statement stat=con.createStatement();
ResultSet rs=stat.executeQuery(sql);
while(rs.next())
{
sql="update atm set password=oldpassword where user=name";
rs=stat.executeQuery(sql);
stat.executeUpdate(sql);
myP.removeAll();
Label pass1=new Label("密码修改成功");
myP.add(pass1);
win1.setVisible(true);
}
stat.close();
con.close();
}
catch(SQLException e)
{
balance(username);
}
catch(ClassNotFoundException ex)
{
System.out.println("error");
}
}
private class ActionListener1 implements ActionListener
{
int money3,password1;
Button btn7,btn8;
String x,y,z;
TextField tx5,tx6,tx7;
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==fileMenuItem1)
{
System.exit(0);
}
if(e.getSource()==helpMenuItem1)
{
dialog1.setSize(200,100);
dialog1.setVisible(true);
//dialog1.addWindowListener(new WindowListener2());
}
if(e.getSource()==btn6)
{
if(flag==true)
{
myP.removeAll();
win1.dispose();
new JDBC();
}
}
if(e.getSource()==btn2)
{
tx1.setText("");
tx2.setText("");
}
if(e.getSource()==btn1)
{
login(username,password);
}
if(e.getSource()==btn3)
{
if(flag==true)
{
myP.removeAll();
balance(username);
}
}
if(e.getSource()==btn4)
{
if(flag==true)
{
myP.removeAll();
Label money2=new Label("输入取款金额");
tx5=new TextField(10);
myP.add(money2);
myP.add(tx5);
btn7=new Button("OK");
final int tempMoney=Integer.parseInt(tx5.getText());
btn7.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent ee)
{
takeMoney(tempMoney,username);
}
});
myP.add(btn7);
win1.setVisible(true);
}
}
if(e.getSource()==btn5)
{
if(flag==true)
{
myP.removeAll();
final Label pass3=new Label("输入新密码:");
final Label pass4=new Label("请输入原密码");
tx5=new TextField(10);
tx6=new TextField(10);
tx7=new TextField(10);
myP.add(pass4);
myP.add(tx5);
myP.add(pass3);
myP.add(tx6);
myP.add(tx7);
btn8=new Button("OK");
btn8.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent ee)
{
x=tx5.getText();
y=tx6.getText();
z=tx7.getText();
if(y==z)
{
password1=Integer.parseInt(y);
updatePassword(x,y,username);
}
else
{
pass3.setText("密码输入有错,请重新输入");
tx6.setText("");
tx7.setText("");
}
}
});
myP.add(btn8);
win1.setVisible(true);
}
}
}
}
}