简体   繁体   中英

MS Access connectivity in java

I am writing a program in java for flashcards.I have an init class which launches the main JFrame. From there I have buttons to access frames to do tasks like add cards, study and help.I have written the AddCards class for adding cards.In this I am trying to connect to an MS Acess database to store cards in it but my programs returns "error". I am not able to find the error.Please help me to point it out.I am wrirting my code in NetBeans.My code for 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(" ");
        }

    }
   }

The whole error which is generated is as follows after using stack trace:

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)

Replace System.out.println("Error"); by ex.printStackTrace(); or by

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

, and you'll have a useful error message and stack trace allowing you to know what's wrong.

The error is obvious though: you forgot a single quote in your query:

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

You should also learn to use prepared statements , which would avoid this kind of bug, wouldn't make your code a subject of SQL injection attacks , and wouldn't make your code break in case the question or answer contains a single quote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM