我似乎无法让我准备好的语句执行。对于输出消息,我得到“成功更新了0个航班”,而不是“成功更新了1个航班”。我的数据库连接(在db.Conn包中)正在工作,并且Eclipse本身没有可见错误。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.*, dbConn.*" %>
String flightid=request.getParameter("id");
String aircraftid=request.getParameter("aircraftid");
String OAirport=request.getParameter("OAirport");
String OCountry=request.getParameter("OCountry");
String DAirport=request.getParameter("DAirport");
String DCountry=request.getParameter("DCountry");
String ecost1=request.getParameter("ecost");
String bccost1=request.getParameter("bccost");
String fccost1=request.getParameter("fccost");
String DDate=request.getParameter("DDate");
String DTime=request.getParameter("DTime");
String ADate=request.getParameter("ADate");
String ATime=request.getParameter("ATime");
String FT=request.getParameter("FT");
String DR=request.getParameter("DiscountRate");
Connection conn=database.getConnection();
String sql="Update flightschedule F ,promotion P set F.aircraftid=?, F.OAirport=?,F.OCountry=?,F.DAirport=?,F.DCountry=?, F.ECost=?, F.BCcost=?,F.FCcost=?, F.DDate=?,F.DTime=?,F.ADate=?,F.ATime=?,F.Flighttime=?, P.DiscountRate=? where F.flightid=P.flightid and F.flightid=? ";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, flightid);
pstmt.setInt(2, Integer.parseInt(aircraftid));
pstmt.setString(3, OAirport);
pstmt.setString(4, OCountry);
pstmt.setString(5, DAirport);
pstmt.setString(6, DCountry);
pstmt.setString(7, ecost1);
pstmt.setString(8, bccost1);
pstmt.setString(9, fccost1);
pstmt.setString(10, DDate);
pstmt.setString(11, DTime);
pstmt.setString(12, ADate);
pstmt.setString(13, ATime);
pstmt.setString(14, FT);
pstmt.setString(15,DR);
int count=pstmt.executeUpdate();
out.println(count+ " flight successfully updated");
conn.close();
%>
这是我的2个数据库:
https://s9.postimg.org/xcaujfq0f/promotion_table.jpg
https://s23.postimg.org/9bawk28u3/flight_schedule.jpg
我有一个FlightID的外键,从Promotion引用到FlightSchedule的Flight ID。
P.S我了解MVC架构,但是我想在使用Servlet之前先解决此问题。
最佳答案
您错误地订购/设置了PreparedStatement
参数,请尝试以下操作:
pstmt.setInt(1, Integer.parseInt(aircraftid));
pstmt.setString(2, OAirport);
pstmt.setString(3, OCountry);
pstmt.setString(4, DAirport);
pstmt.setString(5, DCountry);
pstmt.setString(6, ecost1);
pstmt.setString(7, bccost1);
pstmt.setString(8, fccost1);
pstmt.setString(9, DDate);
pstmt.setString(10, DTime);
pstmt.setString(11, ADate);
pstmt.setString(12, ATime);
pstmt.setString(13, FT);
pstmt.setString(14,DR);
pstmt.setString(15, flightid);