Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: MS Access Error: Too Few Parameters. Expected 2?

  1. #1
    Junior Member Kimiko Sakaki's Avatar
    Join Date
    Mar 2011
    Location
    Toronto
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy MS Access Error: Too Few Parameters. Expected 2?

    Hi all, I am trying to add a new row into my table, in which the name depends on the String variable "InstrumentName". The values are listed in order and I'm pretty sure I haven't missed a column. Though when I try to run it, it says I have "too few parameters". What's more confusing is that the lines where my errors are occuring are in blank lines.

    Here is where I think where my coding is wrong:

    //starts up the connection between Java and MSAccess
                Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
                String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Instrumental.mdb;";
                Connection conn = DriverManager.getConnection(database, "", "");
                Statement s = conn.createStatement();
     
                //generates a barcode for the new instrument
                String barcode = generateBarcode(section, instrumentName, ID);
                //converting int-ID into a string-ID
                String stringID = "" + ID;
                System.out.println (barcode);
                System.out.println (stringID);
     
                //adding a new for the new instrument
                String addRow = "INSERT INTO " + instrumentName +  " VALUES (" + stringID + "," + company + "," + model + "," + serial + "," + barcode + "," + "''" + "," + "''" + ")";
                System.out.println (addRow);
                s.execute(addRow);
     
                s.close();
                conn.close();

    Here is the full error message:
    HTML Code:
    [exception]java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.
    	at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
    	at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
    	at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3110)
    	at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
    	at VersionTwo.AddInstrument(VersionTwo.java:70)
    	at VersionTwo.main(VersionTwo.java:45)
    	at __SHELL7.run(__SHELL7.java:6)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at bluej.runtime.ExecServer$3.run(ExecServer.java:724[/exception]
    Am I missing something in my code? If I am, I can't quite seem to catch it. Please help and I thank you in advance!


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: MS Access Error: Too Few Parameters. Expected 2?

    Which line is throwing the exception? Since we don't have the full code we cannot tell which line the error message is referring to.

  3. #3
    Junior Member Kimiko Sakaki's Avatar
    Join Date
    Mar 2011
    Location
    Toronto
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MS Access Error: Too Few Parameters. Expected 2?

    For the "at VersionTwo.AddInstrument(VersionTwo.java:70", it's referring to "System.out.println (addRow);". For the "at VersionTwo.main(VersionTwo.java:45)", it's referring to the line where I call the method AddInstrument in the main method.

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: MS Access Error: Too Few Parameters. Expected 2?

    How many fields does your database contain?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: MS Access Error: Too Few Parameters. Expected 2?

    Recommend using a PreparedStatement.
    Using Prepared Statements (The Java™ Tutorials > JDBC(TM) Database Access > JDBC Basics)
    You are trying to call a SQL query by passing non-quoted Strings, which is not standard SQL syntax (presuming the equivalent values in your database are text values). A PreparedStatement gets around this as well as protects against SQL injection.

  6. #6
    Junior Member Kimiko Sakaki's Avatar
    Join Date
    Mar 2011
    Location
    Toronto
    Posts
    5
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: MS Access Error: Too Few Parameters. Expected 2?

    PreparedStatements is so much better. Thank you for your help!

Similar Threads

  1. '{' expected error please help
    By erdy_rezki in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 9th, 2012, 10:01 AM
  2. un-expected error
    By arvindbis in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2011, 09:02 AM
  3. Replies: 2
    Last Post: September 23rd, 2011, 07:09 AM
  4. [SOLVED] Error: Class,interface or enum expected
    By FJIW in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 18th, 2011, 03:08 PM
  5. Why the error said "few parameters expected 7" while i only have 6 fields?
    By Hafiz Mughni in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 9th, 2011, 10:45 AM

Tags for this Thread