一個人希望將新作業添加到數據庫中。 A Combobox
列出已添加到數據庫中的現有僱主以添加新的工作。但是,如果僱主不在場,客戶可以選擇點擊按鈕添加僱主。一旦添加,僱主應立即顯示在文本框中。立即更新到Java中的JCombobox
我想實現我的編碼和mysql數據庫出現上述情況,但不能認爲這樣做的邏輯...
表僱主
CREATE TABLE "Employer" ("employerID" INTEGER PRIMARY KEY NOT NULL ,
"name" CHAR,
"industry" CHAR,
"contact1" CHAR,
"contact2" CHAR,
"email" CHAR,
"website" CHAR,
"facts" CHAR,
"phone" VACHAR)
表工作
CREATE TABLE "Job" ("jobID" INTEGER PRIMARY KEY NOT NULL ,
"employerID" INTEGER,
"title" CHAR,
"description" CHAR,
"type" CHAR,"salary" CHAR,
"benefits" CHAR,
"vacancies" INTEGER,
"closing" CHAR,
"requirement" CHAR,
"placement" BOOL,
"applyTo" CHAR,
"status" CHAR,
"posted" CHAR,
"location" CHAR)
類Employer_GUI - 包含一個簡單的表單和保存按鈕,它可以將新的EMPLOYERS保存到Employer
表
private void SaveEmpButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
String sql = "INSERT INTO Employer (name,industry,contact1,contact2,email,website,facts,phone) VALUES (?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sql);
pst.setString(1, txtName.getText());
pst.setString(2, txtInd.getText());
pst.setString(3, txtC1.getText());
pst.setString(4, txtC2.getText());
pst.setString(5, txtEmail.getText());
pst.setString(6, txtWeb.getText());
pst.setString(7, txtFacts.getText());
pst.setString(8, txtPhone.getText());
pst.execute();
JOptionPane.showMessageDialog(null, ""+txtName.getText()+" added to database!");
this.setVisible(false);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, ""+txtName.getText()+" could not be added!");
}
finally {
try {
rs.close(); pst.close(); }
catch(Exception e) { } }
}
//類Job_GUI - 由一個形成於JOBS僅添加到Job
表
private void fillCombo() {
try {
String sql = "SELECT * FROM Employer";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()) {
String empName = rs.getString("name");
comboEmployer.addItem(empName);
}
}
怎麼能立即剛纔添加JComboBox
comboEmployer
所選擇的項目爲新僱主的名字嗎?
我覺得你的代碼假定'JCombobox'是在同一個班級,它不是其在另一GUI和有數據庫概念爲新僱主會立即添加到數據庫中。 – Hoody
@MuminAli @MuminAli不管重要是什麼邏輯,從那裏你可能必須傳遞變量,使用靜態變量或getter和setter來檢索你需要在組合框中設置選定員工的信息,甚至在組合框內創建一個方法類接受一個字符串,並允許你調用它,它將設置選定的項目作爲該字符串,無論最符合您的需求,但我只是讓我的課,添加新員工返回最新添加的條目的名稱? –
Il嘗試getter和setter,但它會更新而GUI仍然可見?意味着生活。 – Hoody