CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  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 mysql 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

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: MySql Error in Java program

    This is an SQL problem and not a Java problem.

    I suggest you change your code to output the actual sql string you are executing to the console and then cut and paste the output sql text into the MySql command line and see if it runs.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: MySql Error in Java program

    Quote Originally Posted by Alexz003 View Post
    The problem is, my code seems right because when i plug it into the actual SQL query box in MySql it runs perfectly fine.
    You're saying that when you do this:

    Code:
    String sql = "SELECT date, percentChange FROM " + tickerMaster[x] + "WHERE date > " + date + " ORDER BY date ASC LIMIT 1";
    System.out.println(sql);
    ResultSet RS2 = newConnection.execute(sql);
    ...and copy the output from the System.out.println(sql) command & paste it directly into the SQL query box in MySql it runs perfectly fine??

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

    Re: MySql Error in Java program

    Also posted (and answered) in the Database forum (here). The problem was a missing space between the table name and WHERE:
    Code:
    ... FROM " + tickerMaster[x] + "WHERE ...

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: MySql Error in Java program

    Thanks for the update jcaccia.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

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