5 Replies Latest reply: Apr 13, 2012 5:05 PM by nantucket RSS

    SQL code not working inside Java

    930431
      Below is my code from java. here, i'm trying to execute below sql code inside my java coding. its preety straight -

      ResultSet rs = null;
      PreparedStatement prepStmt = null;

      String statement="select * from TIMINGTABLE WHERE COUNTRY = 'USA' AND RE_DATE >= TO_DATE('04/11/2012','MM/DD/YYYY') order by COUNTRY";

      prepStmt = con.prepareStatement(statement);
      rs = prepStmt.executeQuery();


      dats it. but, its giving this error ---- SQL syntax error: the token "(" was not expected here

      now, here, when i simply remove this part from the above sql line ----- AND RE_DATE >= TO_DATE('04/11/2012','MM/DD/YYYY') -----the code works perfectly.

      RE_DATE is a date field in the sql table.

      I even tried this option too - TO_DATE(RE_DATE, 'MM/DD/YYYY') >= TO_DATE('04/11/2012','MM/DD/YYYY')
      which is giving same error

      please help


      -prodyut
        • 1. Re: SQL code not working inside Java
          mycoffee
          927428 wrote:
          Below is my code from java. here, i'm trying to execute below sql code inside my java coding. its preety straight -

          ResultSet rs = null;
          PreparedStatement prepStmt = null;

          String statement="select * from TIMINGTABLE WHERE COUNTRY = 'USA' AND RE_DATE >= TO_DATE('04/11/2012','MM/DD/YYYY') order by COUNTRY";

          prepStmt = con.prepareStatement(statement);
          rs = prepStmt.executeQuery();


          dats it. but, its giving this error ---- SQL syntax error: the token "(" was not expected here

          now, here, when i simply remove this part from the above sql line ----- AND RE_DATE >= TO_DATE('04/11/2012','MM/DD/YYYY') -----the code works perfectly.

          RE_DATE is a date field in the sql table.

          I even tried this option too - TO_DATE(RE_DATE, 'MM/DD/YYYY') >= TO_DATE('04/11/2012','MM/DD/YYYY')
          which is giving same error

          please help


          -prodyut
          What kind of DB you use?
          How do you connect to it?
          (If you use ODBC for example, that won't work)
          • 2. Re: SQL code not working inside Java
            930431
            Hi,

            connection type is jdbc connection and the database is sql server


            - prodyut
            • 3. Re: SQL code not working inside Java
              aksarben
              Since the error clearly states you have SQL syntax error, this is not a Java question.
              • 4. Re: SQL code not working inside Java
                mycoffee
                927428 wrote:
                Hi,

                connection type is jdbc connection and the database is sql server


                - prodyut
                TO_DATE is Oracle function, not sqlserver ....

                Try this: CAST('11/22/2012' as datetime)
                • 5. Re: SQL code not working inside Java
                  nantucket
                  927428 wrote:
                  Below is my code from java. here, i'm trying to execute below sql code inside my java coding. its preety straight -
                  A Statement object, or in your case a PreparedStatement which is a subinterface of Statement, is used to send your SQL statement to the DBMS where it then executes. You do not execute any SQL code inside your java application.

                  The JDBC API deals with the problem of variations of SQL conformance among database vendors by allowing you to pass a String, any String to the DBMS by way of the Statement. So the burden rests with you, the developer, to know and understand any particulars with respect to the SQL functions and so forth that are supported by the DBMS that your application uses.