⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jdbc.java

📁 这个系统的功能是模拟ATM机的登陆、取款、更改密码等功能
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
import java.sql.*;
public class JDBC extends WindowAdapter implements ActionListener
{	Frame win1,win2;
	MenuBar myMenu;
	Menu fileMenu,helpMenu;
	MenuItem fileMenuItem1,helpMenuItem1;
	MyDialog dialog1;Dialog dialog2;
	TextField tx1,tx2;
	Panel myP=new Panel();	Panel myP2=new Panel();Panel myP3=new Panel();
	Label logo,lb;
	Button btn1,btn2,btn3,btn4,btn5,btn6,btn;
	String username;
	int password;
	boolean flag=false;
	String url="jdbc:odbc:account";
	JDBC()
	{	win1=new Frame("简易ATM系统测试版");win2=new Frame("简易ATM系统测试版");
		win1.setLayout(new BorderLayout());win2.setLayout(new BorderLayout());
		//设置菜单
		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);
		fileMenuItem1.addActionListener(new ActionListener1());
		helpMenuItem1.addActionListener(new ActionListener1());
		win1.setMenuBar(myMenu);                  //或win1.add(myMenu)
		//设置对话框(帮助菜单)
		dialog1=new MyDialog(win1,"关于简易ATM系统测试版",true);//必须得先关
		dialog1.addWindowListener(this);
                
		dialog2=new Dialog(win2,"错误!!!",true);//必须得先关
		dialog2.addWindowListener(this);
		//用户名及密码
		tx1=new TextField(10);tx1.setBackground(Color.yellow);
                tx1.setForeground(Color.blue);
		tx2=new TextField(10);tx2.setEchoChar('*'); tx2.setBackground(Color.yellow);
                tx2.setForeground(Color.blue);
		//设置各个Button
		win2.add(myP,BorderLayout.CENTER);
		myP.setLayout(new FlowLayout());myP.setBackground(Color.red);
		myP.add(new Label("用户名:"));
		myP.add(tx1);
		myP.add(new Label("密码:    "));
		myP.add(tx2);
		btn1=new Button("登陆");
		btn2=new Button("清除");
		btn1.addActionListener(new ActionListener1());
		btn2.addActionListener(new ActionListener1());
		
		btn3=new Button("账户余额");
		btn3.setForeground(Color.black);
		btn3.setBackground(Color.WHITE);
		btn3.addActionListener(new ActionListener1());
		
		btn4=new Button("我要取款");
		btn4.setForeground(Color.black);
		btn4.setBackground(Color.WHITE);
		btn4.addActionListener(new ActionListener1());
		
		btn5=new Button("修改密码");
		btn5.setForeground(Color.black);
		btn5.setBackground(Color.WHITE);
		btn5.addActionListener(new ActionListener1());
		
		btn6=new Button("用户退出");
		btn6.setForeground(Color.black);
		btn6.setBackground(Color.WHITE);
		btn6.addActionListener(new ActionListener1());

                btn=new Button("确定");
		//btn.addActionListener(new ActionListener1());
                btn.addActionListener(this);
		myP.add(btn1);
		myP.add(btn2);
		
		win1.add(myP2,BorderLayout.EAST);win1.add(myP3,BorderLayout.CENTER);
		myP2.setLayout(new GridLayout(4,1));myP2.setBackground(Color.red);
		myP2.add(btn3);
		myP2.add(btn4);
		myP2.add(btn5);
		myP2.add(btn6);
				
		logo=new Label("欢迎使用ATM系统!");
		logo.setFont(new Font("Serif",Font.PLAIN,15));
                lb=new Label("             请检查你的密码!         ");
                 lb.setBackground(Color.red);lb.setForeground(Color.blue);
                 dialog2.add(lb);dialog2.setBackground(Color.yellow);
                 dialog2.add(btn);
                 dialog2.setSize(200, 100);dialog2.setLayout(new FlowLayout());
		
		win1.add(logo,BorderLayout.NORTH);win1.setBackground(Color.red);
		win1.setSize(600,400);win2.setSize(190,150);
		win1.setVisible(false);win2.setVisible(true);
		win1.addWindowListener(this);win2.addWindowListener(this);
	}
	
	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)
		{
			win1.dispose();/*win1.setVisible(false);*/win2.setVisible(true);
                        tx1.setText("");
			tx2.setText("");  
		}
                if(e.getSource()==win2)
		{
			System.exit(0);
		}
		if(e.getSource()==dialog1)
		{
			dialog1.dispose();
		}
		if(e.getSource()==dialog2)
		{
			dialog2.dispose();
		}
                /*if (e.getSource()==btn) 
                {
                        dialog2.dispose();  
                 }*/
             
		
	}

          public void actionPerformed(ActionEvent ae) 
                 { if (ae.getActionCommand().equals("确定"))
                    dialog2.dispose();tx2.setText(""); 
                  }
	
	public void login(String name,int pword)
	{
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con=DriverManager.getConnection(url);
			CallableStatement cs=con.prepareCall("{call query1(?)}");
			cs.setString(1,name);
			ResultSet rs=cs.executeQuery();
			while(rs.next())
			{
				long i=rs.getLong("password");
				//int balance=rs.getInt("balance");
				//System.out.println(balance);
				if(pword==i)
				{
					flag=true;
					myP3.removeAll();
					Label login=new Label("恭喜你,你已经登陆,可使用右面的按钮");
					myP3.add(login);myP3.setBackground(Color.red);
					win1.setVisible(true);win2.setVisible(false);
				}
				else
				{
					dialog2.setVisible(true);
				}
					
			}
			
			cs.close();
			con.close();
			
		}
		catch(SQLException e)
		{
			System.out.println("SQLException caught");
		}
		catch(ClassNotFoundException ex)
		{
			System.out.println("error");
		}
	}
	
	public void balance()
	{
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con=DriverManager.getConnection(url);
			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);
					
				myP3.removeAll();
				Label account=new Label("你的余额为:");
				TextField tx3=new TextField(15);
				tx3.setText(""+balance);
				myP3.add(account);
				myP3.add(tx3);
				win1.setVisible(true);
					
			}
			
			cs.close();
			con.close();
			
		}
		catch(SQLException e)
		{
			System.out.println("SQLException caught");
		}
		catch(ClassNotFoundException ex)
		{
			System.out.println("error");
		}
	
	}
	
 public void takeMoney(int money)
 {
 	try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con=DriverManager.getConnection(url);
			CallableStatement cs=con.prepareCall("{call query1(?)}");
			cs.setString(1,username);
			ResultSet rs=cs.executeQuery();
			while(rs.next())
			{
			
			     int balance=rs.getInt("balance");
				 if(money<=balance)
				{

				 CallableStatement cs1=con.prepareCall("{call query2(?)}");
			     cs1.setInt(1,money);
			     cs1.executeUpdate();
			     cs1.close();
				 myP3.removeAll();
				 Label money1=new Label("交易成功");
				 myP3.add(money1);
				 win1.setVisible(true);
				}
			}
			
			//cs1.close();
			cs.close();
			con.close();
		}
		catch(SQLException e)
		{
			balance();
		}
		catch(ClassNotFoundException ex)
		{
			System.out.println("error");
		}
 }

public void updatePassword(int pass)
{
	try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			Connection con=DriverManager.getConnection(url);
			CallableStatement cs=con.prepareCall("{call query1(?)}");
			cs.setString(1,username);
			ResultSet rs=cs.executeQuery();
			while(rs.next())
			{
			
			     CallableStatement cs2=con.prepareCall("{call query3(?)}");
			     cs2.setInt(1,pass+1);
			     cs2.executeUpdate();
			     cs2.close();
			         myP3.removeAll();
				 Label pass1=new Label("密码修改成功");
				 myP3.add(pass1);
				 win1.setVisible(true);
			}
			
			//cs1.close();
			cs.close();
			con.close();
		}
		catch(SQLException e)
		{
			balance();
		}
		catch(ClassNotFoundException ex)
		{
			System.out.println("error");
		}
	}
	
class ActionListener1 implements ActionListener
{
	int money3,password1;
	Button btn7,btn8;
	String x,y;
	TextField tx5,tx6;
	
	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)
		   {
			
			   myP3.removeAll();
			   win1.dispose();
			   new JDBC();
			   
		    }
			
		}
		if(e.getSource()==btn2)
		{
			tx1.setText("");
			tx2.setText("");
		}
		if(e.getSource()==btn1)
		{
			username=tx1.getText();
			password=Integer.parseInt(tx2.getText());
			login(username,password);
			
		}
		if(e.getSource()==btn3)
		{	/*if(flag==true)
			{*/
			   myP3.removeAll();
			   balance();			 
		}
		if(e.getSource()==btn4)
		{
			if(flag==true)
			{
				myP3.removeAll();
				
				Label money2=new Label("输入多少钱:");
				tx5=new TextField(10);
				myP3.add(money2);
				myP3.add(tx5);
				btn7=new Button("OK");
		        btn7.addActionListener(new ActionListener2());
				myP3.add(btn7);
				win1.setVisible(true);
				
//System.out.println(money3);//Integer i2=new Integer(i2);//int //money3=i2.intValue();//takeMoney(60);
				
			  }
		}
		if(e.getSource()==btn5)
	        {
		      if(flag==true)
			  {
				myP3.removeAll();
				Label pass3=new Label("输入新密码:");
				tx6=new TextField(10);
				myP3.add(pass3);
				myP3.add(tx6);
				btn8=new Button("OK");
		        btn8.addActionListener(new ActionListener2());
				myP3.add(btn8);
				win1.setVisible(true);
			  }
	        }
	}
	class ActionListener2 implements ActionListener
	{
		public void actionPerformed(ActionEvent ee)
		{
			//System.out.println(money3);
			if(ee.getSource()==btn7)
			{
				x=tx5.getText();
				money3=Integer.parseInt(x);
				takeMoney(money3);
			}
			if(ee.getSource()==btn8)
			{
				y=tx6.getText();
				password1=Integer.parseInt(y);
				updatePassword(password1);
			}
		}
	}
}
class MyDialog extends Dialog
{
	Label label1=new Label("ATM简易测试版");
	Label label2=new Label("Author:姚建仁 ");
	Label label3=new Label("e_mail:elva@mail.zjtcm.net");
	MyDialog(Frame frame1,String title,boolean bool)
	{
		super(frame1,title,bool);
		BorderLayout layout=new BorderLayout();
		setLayout(layout);
		add(label1,BorderLayout.NORTH);
		add(label2,BorderLayout.CENTER);
                add(label3,BorderLayout.SOUTH);
	}

}

}
//-----------------------------------------------------------
class Logo extends JWindow implements Runnable
{
	String filename;
	
	public Logo(String name)
	{
		filename=name;
	}
	public Logo()
	{
	}
	public void run()
	{
		URL imgURL = getClass().getResource(filename);
		ImageIcon ig=new ImageIcon(imgURL);
		JButton btn=new JButton(ig);
		getContentPane().add(btn);
		Toolkit kit=Toolkit.getDefaultToolkit();
		Dimension screenSize=kit.getScreenSize();
		setLocation(screenSize.width/8,screenSize.height/8);
		setSize(ig.getIconWidth(),ig.getIconHeight());
		toFront();
		thread1 thread2=new thread1();
		thread2.start();
		//setVisible(false);
	}
	public void setNotVisible()
	{
		
		setVisible(false);
	}

	class thread1 extends Thread
	{
		public void run()
		{
			try
			{
				setVisible(true);
				sleep(4000);
			    setNotVisible();
			}
			catch(InterruptedException e)
			{
				System.out.print("error occours");
			}
		}
	}
	
}
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -