乘客数据库表:
行李数据库表:
和我的代码: -
private void InsertLuggageInfo() {
try {
String query_passenger = "INSERT INTO passenger (Firstname,Lastname,Email,"
+ "PhoneNr,Address,Zipcode,City,Country_name) "
+ "VALUES (?,?,?,?,?,?,?,?)";
stmt = conn.prepareStatement(query_passenger);
stmt.setString(1, txtFirstname.getText());
stmt.setString(2, txtLastname.getText());
stmt.setString(3, txtEmail.getText());
stmt.setString(4, txtPhoneNr.getText());
stmt.setString(5, txtAddress.getText());
stmt.setString(6, txtZipCode.getText());
stmt.setString(7, txtCity.getText());
stmt.setString(8, txtCountry.getText());
stmt.execute();
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
try {
String query_luggage = "INSERT INTO passenger (DateFound,TimeFound,"
+ "LuggageType_id,Brand_id,MainColor_id,Status_id,Size_id,"
+ "Weight_id,SecondColor_id,LuggageTag,Image,Location_Airport_id,"
+ "Airport_id,Passenger_id) "
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
stmt = conn.prepareStatement(query_luggage);
stmt.setString(1, txtDatum.getText());
stmt.setString(2, txtTimeFound.getText());
stmt.setInt(3, get_LuggageType_id());
stmt.setInt(4, get_Brand_id());
stmt.setInt(5, get_MainColor_id());
stmt.setInt(6, get_Status_id());
stmt.setInt(7, get_Size_id());
stmt.setInt(8, get_Weight_id());
stmt.setInt(9, get_SecondColor_id());
stmt.setString(10, txtLuggageTag.getText());
stmt.setString(11, imgLuggage.toString());
stmt.setInt(12, get_LocationAirport_id());
stmt.setInt(13, get_Airport_id());
stmt.setInt(14, get_Passenger_id());
stmt.execute();
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
}
我获取身份证的方法: -
public int get_LuggageType_id() {
int id = 0;
try {
String query = "Select LuggageType_id from luggagetype where LuggageType ='" + ddlLuggageType.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("LuggageType_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_Brand_id() {
int id = 0;
try {
String query = "Select Brand_id from brand where Brand ='" + ddlMerk.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Brand_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_MainColor_id() {
int id = 0;
try {
String query = "Select MainColor_id from maincolor where Color ='" + ddlMainColor.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("MainColor_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_Status_id() {
int id = 0;
try {
String query = "Select Status_id from status where Status ='" + ddlStatus.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Status_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_Size_id() {
int id = 0;
try {
String query = "Select Size_id from size where Size ='" + ddlMerk.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Size_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_Weight_id() {
int id = 0;
try {
String query = "Select Weight_id from weight where Weight ='" + ddlWeight.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Weight");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_Flight_id() {
int id = 0;
try {
String query = "Select Flight_id from flight where Flight_number ='" + ddlWeight.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Flight_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_SecondColor_id() {
int id = 0;
try {
String query = "Select SecondColor_id from secondcolor where Color ='" + ddlSecondColor.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("SecondColor_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_LocationAirport_id() {
int id = 0;
try {
String query = "Select Location_Airport_id from location_airport where Location_name ='" + ddlLocationFound.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Location_Airport_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_Airport_id() {
int id = 0;
try {
String query = "Select Airport_id from airport where Airport_name ='" + ddlLuchthaven.getSelectionModel().getSelectedItem().toString() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Airport_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
public int get_Passenger_id() {
int id = 0;
try {
String query = "Select Passenger_id from passenger where Firstname ='" + txtFirstname.getText() + "'";
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while (rs.next()) {
id = rs.getInt("Passenger_id");
}
} catch (SQLException ex) {
Logger.getLogger(Verloren_bagageController.class.getName()).log(Level.SEVERE, null, ex);
}
return id;
}
答案 0 :(得分:0)
首先,您应该更改获取生成ID的方式。您可以使用stmt.getGeneratedKeys(),如下所示:
stmt.executeUpdate()
这将保证您获得新插入的数据ID(您的乘客FK)。
另外,请尝试对两个插页使用select_if
。