Java中的MS Access连接

时间:2013-05-11 15:32:31

标签: java sqlexception

我正在java中为flashcards编写一个程序。我有一个启动主JFrame的init类。从那里我有按钮访问框架来执行添加卡,学习和帮助等任务。我已经编写了AddCards类来添加卡片。在这里我试图连接到MS Acess数据库来存储卡片,但我的程序返回“错误”。我无法找到错误。请帮我指出。我在NetBeans中使用我的代码。我的AddCards代码:

  package fc;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.*;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class AddCards  implements ActionListener
{
    JFrame f1=new JFrame("Add Cards");
    JLabel l1=new JLabel("Enter question");
    JLabel l2=new JLabel("Enter answer");
    JTextField t1=new JTextField();
    JTextField t2=new JTextField();
    JButton b1=new JButton("ADD");
    JButton b2=new JButton("CLEAR");
    String ques,ans;
    Statement st;


 AddCards() throws SQLException
 {
    f1.setSize(600,500);
    f1.setLayout(null);
    f1.add(l1);
    f1.add(l2);
    f1.add(t1);
    f1.add(t2);
    f1.add(b1);
    f1.add(b2);
    l1.setBounds(70, 60, 100,20);
    t1.setBounds(170, 60, 300, 100);
    l2.setBounds(70, 200, 100,20);
    t2.setBounds(170, 200, 300, 100);
    b1.setBounds(200, 350, 100, 30);
    b2.setBounds(350, 350, 100, 30);
    f1.setVisible(true);
    b1.addActionListener(this);
    b2.addActionListener(this);
    f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
 }


    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==b1)
        {
            Connection con=null;
            Statement st=null;

            try 
            {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               con=DriverManager.getConnection("jdbc:odbc:fc");

                ques=t1.getText();
                ans=t2.getText();
                String addRow="INSERT INTO data(ques,ans) values('"+ques+"','"+ans+")";    
                st.executeUpdate(addRow);
                con.close();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(AddCards.class.getName()).log(Level.SEVERE, null, ex);
            }
                catch (SQLException ex) 
                {
                    System.out.println("Error");
                }
        } 
        if(e.getSource()==b2)
        {
            t1.setText(" ");
            t2.setText(" ");
        }

    }
   }

使用堆栈跟踪后生成的整个错误如下:

java.sql.SQLException: [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
    at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6956)
    at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7113)
    at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3072)
    at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
    at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:243)
    at fc.AddCards.actionPerformed(AddCards.java:58)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

1 个答案:

答案 0 :(得分:2)

System.out.println("Error");替换为ex.printStackTrace();

Logger.getLogger(AddCards.class).log(Level.SEVERE, "SQL exception while inserting into data", ex);

,你会得到一个有用的错误信息和堆栈跟踪,让你知道什么是错的。

错误很明显:您在查询中忘记了单引号:

String addRow="INSERT INTO data(ques,ans) values('"+ques+"','"+ans+")"
                                                             here --^

您还应该学会使用prepared statements,这可以避免此类错误,不会使您的代码成为SQL injection attacks的主题,并且如果问题不会使您的代码中断或答案包含单引号。