CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2009
    Posts
    105

    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.

  2. #2
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    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");

  3. #3
    Join Date
    Jul 2009
    Posts
    105

    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

  4. #4
    Join Date
    Jul 2009
    Posts
    105

    Re: MySql Error in Java program

    Sorry. I tested it and it didn't work. Returning the same error.

  5. #5
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    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?

  6. #6
    Join Date
    Jul 2009
    Posts
    105

    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
  •  





Click Here to Expand Forum to Full Width

Featured