We help IT Professionals succeed at work.

Why the SQL Error?

Gregg
Gregg asked
on
Medium Priority
263 Views
Last Modified: 2012-05-12
Hello Experts,

[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 3.

Appears i need 3 values for my Prepared Statement. I only have 2 in sql statement. What am i doing wrong?

Thank you

public boolean testUpdateStatement(Contact c)
	{
		String strSQL = "UPDATE contactAddress set first_name = ? WHERE contactID = ?";
			
		try
		{
			PreparedStatement ps = connection.prepareStatement(strSQL);
			
			ps.setString(1, c.getFirstName());
			ps.setInt(2, c.getContactID());
			
			ps.executeUpdate();
			
			ps.close();
			
		}
		catch(SQLException sqle)
		{
			sqle.printStackTrace();
			return false;
		}
		
		return true;
	}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
try square barckets with filed names like in there:
http://www.fmsinc.com/microsoftaccess/query/snytax/update-query.html#UPDATE_Query_SQL_Syntax
UPDATE tblOrders
SET [OrderAmount] = [OrderAmount] * 1.1,
    [Freight] = [Freight] * 1.03
WHERE [ShipCountryRegion] = 'US'

Open in new window


I guess SQL with Access may have
some sepecial issues
Awarded 2011
Awarded 2011

Commented:

Maybe this way:
"UPDATE contactAddress set [first_name] = ? WHERE [contactID] = ?"

Open in new window


I think Access has some strange behaviors with sql syntax



Awarded 2011
Awarded 2011

Commented:
Actually without embeeded spaces it should not require barckets
Awarded 2011
Awarded 2011
Commented:

check this page:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/ce65a0e7-05af-4733-b7f7-b26d0e21587d/

--
Odds are that either UserName or Password is not a column in the Users table. When a SQL statement run against Access has a badly named column, Access thinks you've defined a named parameter and is looking for something in the Parameters collection.

If that is not the case, then try putting square brackets around 'Users' in the statement. Something in my past niggles that Users might be a key word.

Hope that helps.
_

and check column names

Author

Commented:
I just tried the brackets, the same issue. Personally i wouldnt use Access. This is part of project where access was chosen by instructor. I am trying t load that link - it slow at the moment.

Thanks.
Awarded 2011
Awarded 2011

Commented:
Yes, I know, no one uses access because it brings joy - it is such a pain.

They are suggesting that such error may hppen if there is a misprint in your column names

Author

Commented:
Nice call for_yan! I cant believe i didnt check the column names! ...just a little bit embarrassing.

It was ID, not ContactID. Is it time for sleep?
Awarded 2011
Awarded 2011

Commented:
Great!

Happy you found it.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.