Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:58:00+00:00 2026-06-12T21:58:00+00:00

I am getting SQL syntax error in the following code snippet. What is the

  • 0

I am getting SQL syntax error in the following code snippet. What is the problem here? I am unable to figure it out. Please help.

String selectSQL = "SELECT * FROM cost_info where `date` > ? and `date` < ?  order by `date`";
        PreparedStatement preparedStatement = conn.prepareStatement(selectSQL);

        java.sql.Date sqlFromDate = java.sql.Date.valueOf(from_date);
        java.sql.Date sqlEndDate = java.sql.Date.valueOf(end_date);

        preparedStatement.setDate(1, sqlFromDate );
        preparedStatement.setDate(2, sqlEndDate );
        rs = preparedStatement.executeQuery(selectSQL);

I am using MySQL. Error is:

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 '? and `date` < ?  order by `date`' 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:2619)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2569)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1521)
at com.source.viralmo.rms.CostDisplay.initialize(CostDisplay.java:77)
at com.source.viralmo.rms.CostDisplay.<init>(CostDisplay.java:34)
at com.source.viralmo.rms.AdminPage.actionPerformed(AdminPage.java:141)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:58:02+00:00Added an answer on June 12, 2026 at 9:58 pm

    First, order by should go after where.

    Second, if you are using MySQL, date should be quoted since it’s a reserved word for the DATE type. The query should be:

    SELECT * FROM cost_info where `date` > ? and `date` < ?  order by `date`
    

    Third, prepared statements are executed with the executeQuery() method. The executeQuery(String) method that is being used here is for executing normal statements; for example it doesn’t substitute parameters so the ? is passed as-is to the database. You need:

    rs = preparedStatement.executeQuery(/* empty */);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone help me figure out what's wrong with the following T-SQL syntax? SELECT
Why am I getting a syntax error with the following line? statement = self.sql.execute(DESCRIBE
I am getting an SQl syntax error when running the following query and can
I keep getting the following error: You have an error in your SQL syntax;
Im getting the following error: Error: You have an error in your SQL syntax;
I'm getting the following error from the following code. Connection works with no errors,
I am getting a syntax error with the following PHP statement: mysql_query(INSERT INTO table1
running a vb application with the following code. I keep getting an error on
Can someone help me correct the following error I keep getting when trying to
Hi I'm getting the error while executing following code in MySQL : BEGIN DECLARE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.