简体   繁体   中英

Retriving data from different tables in database sending SQL code to be executed, returning data?

I am making a class in Java to connect and otherwise talk to a database, that I have set up elsewhere. I want to use the executeQuery method for a statement, and recieve a ResultSet, from where I will retrieve the information received in the ResultSet.

My issue is with the SQL command, as in; the query I'm sending to the database. Something appears to be wrong, and I get an SQLException at the very point where I send the command, meaning I must have done something wrong. Maybe I'm writing something wrong? I can't tell, since I've simply tried to follow the example guidance provided for this course the best way I can.

Here is the query I'm trying to send:

"SELECT fr.DepartureLocation, fr.Destination, d.Date, d.Time FROM `FlightRoute` fr,
`Departure` d WHERE d.FlightRouteId = fr.Id AND d.Date > " + dateFrom + " AND
d.Date < " + dateTo + " AND fr.Destination = `" + destination + "`;"

The "dateFrom", "dateTo" and "destination" are all parameters for the method I'm calling, and I'm trying to limit the results I get from this statement, to those within a certain date-span as well as having a specific destination. I might add that the dates are integers lined up such as this: 20131205 This should make it so that later dates are a higher number.

Is the way that I use the parameters with the SQL code wrong somehow, or did I make generally faulty SQL code here?

Thanks a lot, to anyone who might be able to provide a correct SQL code for me to use, so that I may see what I did wrong (since I'll have to make a few more similarly working SQL statements)! Thanks in advance! :)

EDIT: Here is the requested code where I am announcing the query and trying to execute it:

try
{
    ArrayList<Departure> departures = new ArrayList<>();
    Statement stmt = con.createStatement();
    System.out.println("derp0");
    String query = "SELECT fr.DepartureLocation, fr.Destination, d.Date, d.Time FROM FlightRoute fr, Departure d WHERE d.FlightRouteId = fr.Id AND d.Date > " + dateFrom + " AND d.Date < " + dateTo + " AND fr.Destination = `" + destination + "`;";
    System.out.println("derp0.1");
    ResultSet rs = stmt.executeQuery(query);
    System.out.println("derp1");

As a short explaination, the souts are to check where the SQLException occured, and I can only say that the exception happens right after "derp0.1".

I suggest the following change:

PreparedStatement ps = connection.prepareStatement(
  "SELECT fr.DepartureLocation, fr.Destination, d.Date, d.Time FROM `FlightRoute` fr,"+
  "`Departure` d WHERE d.FlightRouteId = fr.Id AND d.Date > ? AND d.Date < ? "+
  " AND fr.Destination = ?");

ps.setDate(1, dateFrom);
ps.setDate(2, dateTo);
ps.setString(3, destination); //assuming destination is a String 

ResultSet rs = ps.executeQuery();

In JDBC you should always use parametrisation, as above. It might also solve your problem, which could be caused by an invalid date format.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM