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

📄 conn.java

📁 ATM模拟系统,不是本人的作品,是从CSDN上交换来的,主要是模拟ATM的模拟,可以作为学习材料,内容比较简单,但模拟的很到位.大家看了就知道了.
💻 JAVA
字号:
/*
 * @(#)JDBC.java 1.0 03/07/17
 *
 * Author:weidl
 * 
 * 简易的JAVA连接数据库的例子
 */


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;
	int password;
	boolean flag=false;

	
	String url="jdbc:odbc:account";
	
	JDBC()
	{
		win1=new Frame("简易ATM系统测试版");
		win1.setLayout(new BorderLayout());
		
		//设置菜单
		myMenu=new MenuBar();
		fileMenu=new Menu("File");
		helpMenu=new Menu("Help");
		myMenu.add(fileMenu);
		myMenu.add(helpMenu);
		fileMenuItem1=new MenuItem("Exit");
		helpMenuItem1=new MenuItem("Version");
		myMenu.add(fileMenu);
		myMenu.add(helpMenu);
		fileMenu.add(fileMenuItem1);
		helpMenu.add(helpMenuItem1);
		fileMenuItem1.addActionListener(new ActionListener1());
		helpMenuItem1.addActionListener(new ActionListener1());
		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());
		
		myP.add(new Label("username:"));
		myP.add(tx1);
		myP.add(new Label("password:"));
		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());
		
		
		
		myP.add(btn1);
		myP.add(btn2);
		
		win1.add(myP2,BorderLayout.EAST);
		myP2.setLayout(new GridLayout(4,1));
		myP2.add(btn3);
		myP2.add(btn4);
		myP2.add(btn5);
		myP2.add(btn6);
		
		
		
		
		
		
		logo=new Label("欢迎来到本系统!");
		logo.setFont(new Font("Serif",Font.PLAIN,15));
		
		win1.add(logo,BorderLayout.NORTH);
		win1.setSize(600,400);
		win1.setVisible(true);
		win1.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)
		{
			System.exit(0);
		}
		if(e.getSource()==dialog1)
		{
			dialog1.dispose();
		}
		
	}
	//登陆
	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;
					myP.removeAll();
					Label login=new Label("恭喜你,你已经登陆,可使用右面的按钮");
					myP.add(login);
					win1.setVisible(true);
				}
				else
				{
					tx2.setText("密码错误");
				}
					
					
				 
				
				
			}
			
			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);
					
				myP.removeAll();
				Label account=new Label("你的余额为:");
				TextField tx3=new TextField(15);
				tx3.setText(""+balance);
				myP.add(account);
				myP.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();
				 myP.removeAll();
				 Label money1=new Label("交易成功");
				 myP.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);
			     cs2.executeUpdate();
			     cs2.close();
			     myP.removeAll();
				 Label pass1=new Label("密码修改成功");
				 myP.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)
		   {
			
			   myP.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)
			{
				
			   myP.removeAll();
			   balance();
			 }
			 
		}
		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");
		        btn7.addActionListener(new ActionListener2());
				
				myP.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)
			  {
				myP.removeAll();
				
				Label pass3=new Label("输入新密码:");
				tx6=new TextField(10);
				myP.add(pass3);
				myP.add(tx6);
				btn8=new Button("OK");
		        btn8.addActionListener(new ActionListener2());
				
				myP.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:weidl    Version: 1.0");
	
	MyDialog(Frame frame1,String title,boolean bool)
	{
		super(frame1,title,bool);
		BorderLayout layout=new BorderLayout();
		setLayout(layout);
		add(label1,BorderLayout.NORTH);
		add(label2,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 + -