gpt4 book ai didi

java - Jdbc SQL参数问题

转载 作者:行者123 更新时间:2023-12-02 09:57:34 24 4
gpt4 key购买 nike

我正在从用户那里获取输入并存储在两个不同的变量中。我将参数与我的 sql 语句绑定(bind)。当我运行代码时,它在连接查询部分时出现问题。

String CityA= null;
String CityB= null;
try {
CityA = readEntry(in, "Enter Origin City : ");
CityB = readEntry(in, "Enter Destination City : ");

// We treat this drop table specially to allow it to fail
// as it will the very first time we run this program

try {
String q = "SELECT f.FLNO,f.DISTANCE,TIMEDIFF(f.arrives,f.departs)
as Duration FROM FLIGHTS F"
+ " WHERE F.ORIGIN = "+CityA;
+ "AND f.DESTINATION = "+CityB;

System.out.println(q);
rset = stmt.executeQuery(q);
while (rset.next()) {
System.out.println(rset.getInt("FLNO") + ","
+ rset.getInt("Distance") + ","
+ rset.getTime("Duration"));
}
System.out.println("Done");
}
catch (SQLException e) {
// assume not there yet, so OK to continue
}
finally {
stmt.close();
}

最佳答案

请查找查询代码:-基本上你错过了 CityAAND

之间的空格
String q = "SELECT f.FLNO,f.DISTANCE,TIMEDIFF(f.arrives,f.departs) as Duration FROM FLIGHTS F"
+ " WHERE F.ORIGIN = '"+CityA+"' ";
+ "AND f.DESTINATION = '"+CityB+"'";

关于java - Jdbc SQL参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55887799/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com