-
January 15th, 2012, 02:32 PM
#1
MySql Error in Java program
Hello,
I'm not really sure if this is the right area i should be putting this(so i'm going to post it both in java section and this section) but here is the problem.
I am getting the following error
Code:
Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '> 2010-01-15 ORDER BY date ASC LIMIT 1' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
at Main.Database.execute(Database.java:153)
at Analysis.Analysis_A.main(Analysis_A.java:40)
The problem is, my code seems right because when i plug it into the actual SQL query box in MySql it runs perfectly fine.
Here is the code that is erroring up:
Code:
Date date = (Date) RS.getObject(1);
double percentChange = RS.getDouble("percentChange");
ResultSet RS2 = newConnection.execute("SELECT date, percentChange FROM " + tickerMaster[x] + "WHERE date > " + date + " ORDER BY date ASC LIMIT 1");
I am obtaining the box contents using an object, however i am parsing it as an SQL Date. I have also tried using getDate(1).
If you could help, it would be greatly appreciated
-Alex
Last edited by Alexz003; January 16th, 2012 at 09:18 AM.
-
January 15th, 2012, 03:38 PM
#2
Re: MySql Error in Java program
In the query for RS2, to use the value of your date variable as a date literal you need to surround it with quotes:
Code:
ResultSet RS2 = newConnection.execute(
"SELECT date, percentChange FROM " + tickerMaster[x] +
" WHERE date > '" + date + "' ORDER BY date ASC LIMIT 1");
-
January 15th, 2012, 05:28 PM
#3
Re: MySql Error in Java program
Hmm really? Im just curious because when I ran the program and inserted the date directly, it didn't require quotes around it. Could that be because I was using an actual value instead of a variable to replace the value? Ill check it and get back. Thank you for responding.
-Alex
-
January 15th, 2012, 05:34 PM
#4
Re: MySql Error in Java program
Sorry. I tested it and it didn't work. Returning the same error.
-
January 15th, 2012, 05:58 PM
#5
Re: MySql Error in Java program
Sorry, I'm not an expert on MySQL and understood date literals needed to be quoted, like '2010-01-15'.
OTOH, did you notice there is no space between the double quote and WHERE in:
Code:
... FROM " + tickerMaster[x] + "WHERE ...
I presume tickerMaster[x] has the name of the table (or are there more than one table involved?). Is there a space after the table name? so it's separated from WHERE?
Have you tried printing the whole SELECT to check what is actually being sent to the connection to execute?
-
January 16th, 2012, 09:18 AM
#6
Re: MySql Error in Java program
Thanks that worked
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|