import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class ATM extends JFrame{

private JTextArea messageArea;
private JPanel buttonPanel;
private JPanel commandPanel;
private String buttonName[]={"1","2","3","4","5","6","7","8","9","0"};
private JButton buttonNumber[]= new JButton[10];
private JButton queryButton;
private JButton withdrawButton;
private JButton enterButton;
private JButton exitButton;
private double balance;
private double accountAmount;
private String accountNumber="",accountPin="";
private String s="";
private int enterState=0;

private Connection myCon;
private Statement myStatement;
private ResultSet myResultSet;

public ATM(){
String driverClass = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:account";

try{
Class.forName(driverClass);
}catch (ClassNotFoundException e){
System.out.println("cann not found driver"+driverClass);
System.exit(-1);
}

try {
myCon = DriverManager.getConnection(url,"account","");
myStatement=myCon.createStatement();
//System.out.println("连接成功");

}catch(SQLException sqe){
sqe.printStackTrace();
}
CreateUI();
}
private void CreateUI()
{
Container contentPane =getContentPane();
contentPane.setLayout(null);
messageArea= new JTextArea();
messageArea.setBounds(20, 16, 250, 80);
messageArea.setText("欢迎使用");
messageArea.setEditable(false);
contentPane.add(messageArea);

buttonPanel=new JPanel();
buttonPanel.setBounds(24, 110, 140, 120);
buttonPanel.setLayout(new GridLayout(4,3,5,5));
contentPane.add(buttonPanel);

commandPanel=new JPanel();
commandPanel.setBounds(180,110,80,120);
commandPanel.setLayout(new GridLayout(4,1,5,5));
contentPane.add(commandPanel);

for(int i=0;i<10;i++)
{
buttonNumber[i]=new JButton(buttonName[i]);
buttonNumber[i].addActionListener(new buttonNumber());
buttonPanel.add(buttonNumber[i]);
}

queryButton=new JButton("查询");
queryButton.addActionListener(new queryButtonAction());
commandPanel.add(queryButton);

withdrawButton=new JButton("取款");
withdrawButton.addActionListener(new withdrawButtonAction());
commandPanel.add(withdrawButton);

enterButton=new JButton("确认");
enterButton.addActionListener(new enterButtonAction());
commandPanel.add(enterButton);

exitButton=new JButton("退出");
exitButton.addActionListener(new exitButtonAction());
commandPanel.add(exitButton);

setTitle("ATM");
setSize(300,280);
setVisible(true);

addWindowListener(new WindowAdapter()
{
  public void windowClosing(WindowEvent e){
  try{
  myStatement.close();
  myCon.close();
  }catch(SQLException exp){exp.printStackTrace();}
  }
});
}
class buttonNumber implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(enterState==0)
{   
s=s+e.getActionCommand().charAt(0);
messageArea.append(e.getActionCommand());
}
else if(enterState==1)
{
s=s+e.getActionCommand().charAt(0);
messageArea.append("*");
}
else if(enterState==2)
{
s=s+e.getActionCommand().charAt(0);
messageArea.append(e.getActionCommand());
}
}
}

class queryButtonAction implements ActionListener{
public void actionPerformed(ActionEvent e){

messageArea.setText("当前余额:"+accountAmount);
messageArea.append("\n\n\n需要继续服务按确定,否则请退出");
enterState=3;
}

}

class withdrawButtonAction implements ActionListener{

public void actionPerformed(ActionEvent e){
messageArea.setText("请输入取款金额:\n");
enterState=2;
s="";
}

}

class exitButtonAction  implements ActionListener{
public void actionPerformed(ActionEvent e){
enterState=0;
s="";
accountAmount=0;
accountNumber="";
accountPin="";
messageArea.setText("欢迎使用ATM,请输入账号:\n");

}

}

class enterButtonAction implements ActionListener{
public void actionPerformed(ActionEvent e){
enterState++;
enterState=enterState%5;
if(enterState==1){
accountNumber=s;
s="";

try{
myResultSet=myStatement.executeQuery("SELECT * FROM account WHERE accountNumber='"+accountNumber+"'");
if(myResultSet.next())
{
messageArea.setText("请输入密码:\n");
}

else
{
messageArea.setText("账号错误,请重新输入");
enterState=0;
}
}catch(SQLException exp){exp.printStackTrace();}
}

if(enterState==2){
accountPin=s;
s="";
try{
if(myResultSet.getString("accountPin").compareTo(accountPin)==0)
{
accountAmount=myResultSet.getDouble("accountAmount");
messageArea.setText("请选择服务种类\n");
}
else {messageArea.setText("密码错误,请重新输入");
enterState=1;
}
}catch(SQLException exp){exp.printStackTrace();}

}


if(enterState==3){
balance=Double.parseDouble(s);
s="";
if(accountAmount>balance){
accountAmount=accountAmount-balance;
messageArea.setText("请等待\n");

try{
myStatement.executeUpdate("UPDATE account SET accountAmount="
+accountAmount+"WHERE accountNumber='"+accountNumber+"'");
messageArea.setText("\n\n请取走钞票\n继续服务请按确定,退出请按取消!\n");

}catch(SQLException exp){exp.printStackTrace();}
}
else {
messageArea.setText("对不起,余额不足!请重新输入金额");
enterState=2;

}
}

if(enterState==4)
{
enterState=2;
s="";
messageArea.setText("请选择服务种类:\n");

}

}
}
public static void main(String[] args){
ATM atm=new ATM();
}
}错误提示:java.sql.SQLException: [Microsoft][ODBC Microsoft Access 驱动程序] Microsoft Jet 数据库引擎找不到输入表或查询 'grade'。 确定它是否存在,以及它的名称的拼写是否正确。
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(Unknown Source)
at Teacher.<init>(Work.java:178)
at Enter$EnterTButton.actionPerformed(Work.java:53)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
是不是我的数据源配置错误啊,代码是照书全抄的,win7的系统,求大大们帮帮忙,小弟太菜了

解决方案 »

  1.   

    应该是数据类型问题 检查一下grade的类型
      

  2.   

    错了,错误指出的是java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Microsoft Office Access 数据库引擎找不到输入表或查询“account”。请确定它存在且其名称拼写正确。
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
    at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
    at ATM$enterButtonAction.actionPerformed(ATM.java:167)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
      

  3.   

    字段‘account’找不到。看看对应的表里是否有‘account’这个字段。
      

  4.   

    如果你的account是个表,说明这个表你写错了或者在ODBC数据源配置的时候没配置对
    如果它是个字段,检查他的写法对不对