Results 1 to 8 of 8
- 02-10-2009, 03:38 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Java JDBC/MySQL appropriate Syntax
Hi
I am having a problem trying to get the correct syntax for a java sql query of a mysql database.
Currently I have:-
result = statement.executeQuery("select account.account_number" +
"from account where account_number =" +accountNumberIn);
I keep getting a syntax error message:-
com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where account_number =11111111' at line 1
where 11111111 is the value of accountNumberIn.
I would appreciate help from anyone who might know the correct syntax for such a query.
Thanks
- 02-10-2009, 03:57 PM #2
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
try this code:
Java Code:result = statement.executeQuery("select account_number" + "from account where account_number = '" +accountNumberIn+"'");
- 02-10-2009, 04:01 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Hi wolfcro
Thanks for your quick reply, I have tried the line you gave me but it did'nt work still got the syntax error as follows:-
com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where account_number = '11111111'' at line 1
- 02-10-2009, 04:18 PM #4
Member
- Join Date
- Feb 2009
- Location
- Italy
- Posts
- 51
- Rep Power
- 0
let's try with:
if it doesn't work here you can see the syntax yourself and try:Java Code:result = statement.executeQuery("select account_number" + "from account where account_number = "+accountNumberIn+";");
dev.mysql.com/doc/refman/5.1/en/select.html
- 02-10-2009, 04:21 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Hi Wolfcro
Thanks again for the quick reply, it didn't work, thanks for the link Ill check the syntax there.
Regards
theLinuxGuy
- 02-10-2009, 04:37 PM #6
You will hate the answer to this one. Look at the end of your first literal and the beginning of the second one. There is no blank! Your query string looks like
select account_numberfrom account where account_number = ...
The parser things account_numberfrom is a column name and account is its alias. Then, it looks for the "from" clause and doesn't find one.
I saw this instantly, having done it to myself so many times. What I do is always *start* a new literal with a blank, unless no blank should be used. I found putting a blank at the beginning is easier to spot if it leave it out.
- 02-10-2009, 04:54 PM #7
Member
- Join Date
- Dec 2008
- Posts
- 44
- Rep Power
- 0
Hello Steve11235
Thank you for your reply. You were absoultely right. Problem solved, Thanks again.
Sincerely
theLinuxGuy
- 02-10-2009, 07:57 PM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
As a note, the semicolon at the end is not needed, and, when providing "arguments" (i.e. that accountnumber variable) you are much better using PreparedStatement and it's varying set... methods, as these will properly escape anything that needs to be escaped, as well as being able to redo the query, with different values, without having to recreate the query string. (And the first part of that will also help to prevent SQL Injection attacks.)
Similar Threads
-
ClassNotFoundException com.mysql.jdbc.Driver
By Heather in forum JDBCReplies: 4Last Post: 03-31-2010, 12:08 PM -
Java Syntax for making 2 divs same height dynamically.
By jatrant in forum New To JavaReplies: 4Last Post: 06-25-2008, 08:09 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
help ... JDBC or PHP/MySQL
By bluebarca in forum Advanced JavaReplies: 1Last Post: 11-16-2007, 10:05 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks