Link to home
Start Free TrialLog in
Avatar of lilanw
lilanw

asked on

Select statement

please correct this statement... this is not working,,,,



Statement statement;
ResultSet resultset;
                                                      
try
{
  query = "SELECT FlightNumber FROM FlightD where Date = '" +              _date +"'"     &&  `From = "'" +  _from +"'"  && To ="'" +  _to + "'" ;      
                                                            statement = connect.createStatement();
                                                            resultset = statement.executeQuery(query);
                                                            
                                          
                                                            
                                                            statement.close();
                                                            
                                                      }
                                                      catch(SQLException sqlex){sqlex.printStackTrace();}
                                          
                           
Avatar of Manikandan Thiagarajan
Manikandan Thiagarajan
Flag of India image

could you  please give me the variable declaration and value for date,from,to
query = "SELECT FlightNumber FROM FlightD where Date =  _date
     &&  `From = " +  _from +"  && To =" +  _to + " " ;    

date should be java.sql.date variable

try this for date.

String possibleValidDate ="something";
try {
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
     sdf.setLenient(false);
     Date d = sdf.parse(possibleValidDate);
}
catch(Exception e) {
     System.err.println("Invalid date " + possibleValidDate);
}


query = "SELECT FlightNumber FROM FlightD where Date = " +  _date + " AND  From = "  +  _from  +  " AND To = " + _to;
Avatar of CEHJ
Using a PreparedStatement would make the statement cleaner, be easier to code and proof the code against variable date formats, thus making the code more portable too
Here's a little tutorial about the different SQL statements you might find useful

http://www.stardeveloper.com/articles/display.html?article=2002030801&page=1
ASKER CERTIFIED SOLUTION
Avatar of Mujtaba_Alam_Khan
Mujtaba_Alam_Khan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> it is a reserved word and should not be use as a column.

actually, you can. however you must delimit it.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/acdata/ac_8_con_03_6e9e.asp

so your query would be,

query = "SELECT FlightNumber FROM FlightD where Date = " +  _date + " AND  [From] = "  +  _from  +  " AND To = " + _to;
Avatar of Mujtaba_Alam_Khan
Mujtaba_Alam_Khan

ok, I get it now,
From and To are places names, not dates, my mistake oops

-Muj  
No prob.