0
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
// date here is a string of format yyyy-MM-dd
 java.util.Date date_1 = df.parse(date) ;
java.sql.Date sqldate = new java.sql.Date(date_1.getTime());
sql = "select * from fgs_stock_report where Report_date = ? ";

PreparedStatement two = con.prepareStatement(sql);
two.setDate(1,sqldate);ResultSet rs ;
rs = two.executeQuery(sql) ;

Here I get a Java Sql Exception asking for the right syntax near? . I am a beginner and I searched a lot for a solution but couldnt find. Please help me.

2
  • Post the complete stack trace of the exception, so that we can know what the problem is. Commented Jun 29, 2014 at 17:00
  • does your table contains date in this foramt 2014-06-29 Commented Jun 29, 2014 at 17:14

1 Answer 1

3

I think I see the problem, you are using a Statement.executeQuery(String) but you want PreparedStatement.executeQuery() - that is.

PreparedStatement two = con.prepareStatement(sql); // <-- Prepare a Statement.
two.setDate(1,sqldate);                            // <-- bind the parameter.
ResultSet rs ;
rs = two.executeQuery(sql) ;                       // <-- throw it away and use raw sql

What you want is,

 ResultSet rs = two.executeQuery();                // <-- I'd use one line
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.