We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

SQL Syntax Error from JAVA Prepared Statement

cdromes
cdromes asked
on
Medium Priority
267 Views
Last Modified: 2012-06-27
I've tested this SQL statement directly from the MySQL command line and had it work, so I'm at a bit of a loss as to what the problem might be.  The code is below.  The 'System.out.println' verified that the expected values are being passed.

Any help is, as always, greatly appreciated.
for(int a=0;a<AL.size();a++)
		{
			System.out.println("Name"+AL.get(a).get(1)+" Avg: "+SiteAvgTBL.get(a).get(0)+" Low: "+SiteAvgTBL.get(a).get(1)+" High: "+SiteAvgTBL.get(a).get(2));
			String Update = "UPDATE FSTable SET Avg='"+SiteAvgTBL.get(a).get(0)+"', Low='"+SiteAvgTBL.get(a).get(1)+"', High='"+SiteAvgTBL.get(a).get(2)+"' WHERE Name='"+AL.get(a).get(1)+"')";
			PreparedStatement UpdatePS = con.prepareStatement(Update);  
		       	UpdatePS.executeUpdate();
		}

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You're not using the PS properly. It you do, you'll find it'll probably work due to quoting problems being ocrrected
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
need to use a CallableStatement and change your sql

CERTIFIED EXPERT
Top Expert 2016

Commented:
Your PS string should be
String Update = "UPDATE FSTable SET Avg=?, Low=?, High=? WHERE Name=?";

Open in new window

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
ignore that, misread your sql :)

>                         String Update = "UPDATE FSTable SET Avg='"+SiteAvgTBL.get(a).get(0)+"', Low='"+SiteAvgTBL.get(a).get(1)+"', High='"+SiteAvgTBL.get(a).get(2)+"' WHERE Name='"+AL.get(a).get(1)+"')";

get rid of trailing )

                        String Update = "UPDATE FSTable SET Avg='"+SiteAvgTBL.get(a).get(0)+"', Low='"+SiteAvgTBL.get(a).get(1)+"', High='"+SiteAvgTBL.get(a).get(2)+"' WHERE Name='"+AL.get(a).get(1)+"'";

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
The prepared statement?  I basically copied it from the following query three lines above it that worked:

String FSD = "INSERT INTO RecTable (Chr, Name, Locus, Class, Mode, Start, Stop, Position, RRate, Hot, ChrAvg, GenAvg) VALUES ('"+RecombTBL.get(a).get(0)+"','"+RecombTBL.get(a).get(1)+"','"+RecombTBL.get(a).get(2)+"','"+RecombTBL.get(a).get(3)+"','"+RecombTBL.get(a).get(4)+"','"+RecombTBL.get(a).get(5)+"','"+RecombTBL.get(a).get(6)+"','"+TempInt+"','"+RecombTBL.get(a).get(8)+"','"+RecombTBL.get(a).get(9)+"','"+RecombTBL.get(a).get(10)+"','"+RecombTBL.get(a).get(11)+"')";
                  
PreparedStatement FSps = con.prepareStatement(FSD);  
FSps.executeUpdate();

I'm not sure what the issue would be, taking into account that the syntax it works in one place, but not another in the same program.  The quoting has also be checked.
CERTIFIED EXPERT
Top Expert 2016

Commented:
PreparedStatement is fine
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
the problem is that query has a opening ( and closing bracket )
when you have modified it you have removed the opening bracket

brackets do not appear to be needed in new query, try what I posted above

CERTIFIED EXPERT
Top Expert 2016

Commented:
See the following for the correct way to call a PS:

http://www.exampledepot.com/egs/java.sql/InsertPs.html

Author

Commented:
Oh, man.  The deadly trap of copying-and-pasting code snippets for reuse elsewhere bites me in the ass once again.  It was so obvious that I never would've seen it, so many thanks.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.