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

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+"'";

07-28 14:00