0

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)
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Pooja
  • 500
  • 2
  • 6
  • 13
  • 2
    Replace `System.out.println("Error");` by `ex.printStackTrace();`, and you'll have a useful error message and stack trace allowing you to know what's wrong. – JB Nizet May 11 '13 at 15:34
  • To add to that, "swallowing" exceptions like this is a bad idea unless you know there's never going to be an error. A common wordaround is to rethrow as a RuntimeException `throw new RuntimeException(e.getMessage(), e)` so you don't lose the stack-trace. This way the application can handle/log as it choses. – Daniel B. Chapman May 11 '13 at 15:39
  • Even if you know there will never be an error, you'd better throw a runtime exception wrapping the exception because, you know, shit happens, and "never" often really means "almost never". – JB Nizet May 11 '13 at 15:42
  • 1
    1) Don't set the size of top level containers. Instead layout the content & call `pack()`. 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. For a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space, to organize the components. – Andrew Thompson May 11 '13 at 15:53
  • thank you all. But I am very new to java and not able to understand whatever you are telling me. So, what should I do? – Pooja May 11 '13 at 16:39
  • Looks like a 64 bit <> 32 bit issue – Mark Rotteveel May 11 '13 at 16:42
  • Try to use ucanaccess instead of odbc just follow this [ http://stackoverflow.com/questions/21955256/manipulating-an-access-database-from-java-without-odbc][1] – user3808015 Sep 11 '14 at 09:52

1 Answers1

2

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.

JB Nizet
  • 633,450
  • 80
  • 1,108
  • 1,174
  • +1 for prepared statement. Never build queries this way. What if `ques` has a quote in it ("What's the capital of the UK?"). – Boris the Spider May 11 '13 at 15:40
  • After using ex.printStackTrace(); , I am getting the following error:The specified DSN contains an architecture mismatch between the Driver and Application – Pooja May 11 '13 at 15:45
  • That looks like an ODBC configuration error. The stack trace would help confirming that. – JB Nizet May 11 '13 at 15:49
  • thank you all. But I am very new to java and not able to understand whatever you are telling me. So, what should I do? – Pooja May 11 '13 at 15:56
  • I don't know shit about MS Access and ODBC. Start by checking that you can connect to your database using an ODBC client, using the `fc` data source that you're using in your Java code. – JB Nizet May 11 '13 at 16:00