简体   繁体   English

从数据库中的不同表中检索数据以发送要执行的SQL代码,并返回数据?

[英]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. 我正在用Java创建一个类来连接数据库,或者与之交谈,这是我在其他地方设置的。 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. 我想对语句使用executeQuery方法,并接收一个ResultSet,从中检索ResultSet中收到的信息。

My issue is with the SQL command, as in; 我的问题是SQL命令,如下所示: 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. 似乎有问题,并且在发送命令的那一刻出现了SQLException,这意味着我必须做错了什么。 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. “ dateFrom”,“ dateTo”和“ 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. 我可能会补充说,日期是这样排列的整数:20131205应该这样做,以便以后的日期是一个更大的数字。

Is the way that I use the parameters with the SQL code wrong somehow, or did I make generally faulty SQL code here? 我将参数与SQL代码一起使用的方式是否有些错误,还是在这里使SQL代码普遍出错?

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)! 非常感谢任何能够提供正确的SQL代码供我使用的人,以便我可以看看我做错了什么(因为我将不得不制作一些类似的SQL语句)! 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". 作为简短的解释,souts将检查SQLException的发生位置,我只能说该异常发生在“ 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. 如上所述,在JDBC中,您应该始终使用参数化。 It might also solve your problem, which could be caused by an invalid date format. 它还可能会解决您的问题,这可能是由无效的日期格式引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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