gpt4 book ai didi

java - 将整数值添加到数据库表错误

转载 作者:行者123 更新时间:2023-11-29 15:43:28 24 4
gpt4 key购买 nike

我正在用java创建一个航类应用程序,它应该做的事情之一就是向数据库添加一个新的航类。我可以毫无问题地添加航类名称,但在将航类的座位数添加到数据库中时遇到问题。数据库表设置为将座位数作为整数值。在我的代码中,我的函数将该值从用户界面添加到数据库中,将一个值作为整数参数。然后,在添加新航类的实际按钮的事件处理程序代码中,我将值从字符串(这是文本框中接受的字符串)转换为整数,以便可以将该值输入到数据库。但是,当我运行代码时,出现错误,指出座位数不能接受为空值。基本上,数据库将值视为空。我不确定我的代码中做错了什么。

public class Flight extends Customer {
private static ArrayList<String> FlightNames;
private static ArrayList<Integer> Seats;
private static PreparedStatement getFlightnames;
private static PreparedStatement getFlightSeats;
private static PreparedStatement addFlightName;
private static PreparedStatement addSeats;
private static ResultSet fresult;
private static ResultSet sresult;

public static ArrayList <String> getFlightnames(){
//recieve flight names from database
try{
FlightNames = new ArrayList();
getFlightnames = getConnection().prepareStatement("select name from flight ");
fresult = getFlightnames.executeQuery();
while(fresult.next()){
FlightNames.add(fresult.getString(1));

}

}
catch(SQLException result){
result.printStackTrace();
}
return FlightNames;

}
public ArrayList<Integer> getFlightSeats(){
try{
Seats = new ArrayList();
//getFlightSeats = getConnection().prepareStatement("SELECT * FROM Flight WHERE Seats LIKE ?");
getFlightSeats = getConnection().prepareStatement("select seats from flight ");
sresult = getFlightSeats.executeQuery();
while(sresult.next()){
Seats.add(sresult.getInt(1));

}
}

catch(SQLException result){
result.printStackTrace();
}
return Seats;
}
public void addFlight(String flight){
try{
addFlightName = getConnection().prepareStatement("insert into bookings (flight) values(?)");
addFlightName.setString(1, flight);
addFlightName.executeUpdate();

}
catch(SQLException result){
result.printStackTrace();
}
}
public void addNumber(String flightNumber){
try{
addFlightName = getConnection().prepareStatement("insert into flight (name) values(?)");
addFlightName.setString(1, flightNumber);
addFlightName.executeUpdate();

}
catch(SQLException result){
result.printStackTrace();
}
}
public void addSeats(int seats){
try{
addSeats = getConnection().prepareStatement("insert into flight (seats) values(?)");
addSeats.setInt(1, seats);
addSeats.executeUpdate();

}
catch(SQLException result){
result.printStackTrace();
}
}

}

private void AddFlightButtonActionPerformed(java.awt.event.ActionEvent evt) {
Flight addnumber = new Flight();
Flight addseats = new Flight();
addnumber.getConnection();
addseats.getConnection();
String flight = FlightTextBox.getText();
String seats = SeatsTextBox.getText();
int seatsInt = Integer.parseInt(seats);
addnumber.addNumber(flight);
addseats.addSeats(seatsInt);

AddFlightStatusLabel.setText("The flight " + flight + " has been added with " + seats + " seats" );
BookFlightComboBox.setModel(new javax.swing.DefaultComboBoxModel(Flight.getFlightnames().toArray()));
StatusFlightComboBox.setModel(new javax.swing.DefaultComboBoxModel(Flight.getFlightnames().toArray()));
WaitFlightComboBox.setModel(new javax.swing.DefaultComboBoxModel(Flight.getFlightnames().toArray()));
}

最佳答案

您似乎使用了错误的 table 来添加座位,或者为 table 添加了错误的索引。

编辑:检查表列后,您需要为这两个函数添加这样的查询,但建议仅使用这样的单一方法:

public void addFlightDetail(String flightNumber, int seats){

addFlightName = getConnection().prepareStatement("insert into flight (name, seats)
values(?,?)");
addFlightName.setString(1, flightNumber);
addFlightName.setInt(1, seats);
}

关于java - 将整数值添加到数据库表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57334810/

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