一个人希望向数据库添加一个新工作。列出数据库中已有的Combobox
现有雇主,以便添加新工作。但是,如果雇主不在场,客户可以选择单击按钮添加雇主。添加后,雇主应立即显示在文本字段中。
我正在尝试使用我的编码和 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) { } }
}
//Class Job_GUI - 由一个 FORM 组成,仅将 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
立即将所选项目作为刚刚添加的新雇主名称?