0

I am new to Java Swing programming, so please bear with me. I have connected my JFrame to MA Access data base using JDBC ODBC driver and here is the erroneous code.

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:MS Access Database");
        st = con.createStatement();
        String q = "select "+Name+",Date/Subject from "+$Class;
        R = st.executeQuery(q);

        int row=0;
        while(R.next())
        {
            Table.setValueAt(R.getString("Date/Sujbect"),row,0);
            Table.setValueAt(R.getString(Name),row,1);
            row++;
       }

when the st.executeQuery(q) is reached, it throws an exception saying.

"Too few Parameters. Expected 2". 

What I don't understand is this: when the query is written like

"select * from "+ $Class;

it works perfectly fine. Even in some other JFrames, I am doing the same thing. This exception persists as log as I mention the exact columns instead of the star(*).

Please also let me know if there is any workaround this, or any other method that I may use.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
tedd_selene
  • 395
  • 1
  • 4
  • 9
  • 1
    could you plese share your table structure? – Pirate Oct 08 '16 at 19:01
  • 2
    Does your table have a column named "Date/Subject"? If so, then you need to enclose it in square brackets, `[Date/Subject]`, otherwise the query parser will interpret `Date/Subject` as the `Date` column (or function) *divided by* the `Subject` column. If those columns don't exist then the Access Database Engine will interpret them as parameter placeholders, hence the "parameters expected" error. – Gord Thompson Oct 08 '16 at 19:40

1 Answers1

2

Please also let me know if there is any workaround this

The problem is your SQL statement. You have an error somewhere.

String q = "select "+Name+",Date/Subject from "+$Class;

If you are having problems with your SQL then don't use variables. First get the SQL to work by hardcoding the column names. Something like:

String q = "Select Column1, Column2 from TableName";

Then once you get that working you can try to make the statement dynamic and use variables to specify your column names or table name.

If you need more help then find an SQL tutorial and look up the proper syntax. Here is one to get you started: JDBC Database Access.

We don't know your table name or column names to we can't write the code for you. That is your job.

camickr
  • 321,443
  • 19
  • 166
  • 288