表雇主
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)
try {
//String sqlStm = "INSERT INTO Job (employerID,title,description,type,salary,benefits,vacancies,closing,requirement,placement,applyTo,status,posted,location) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?) INNER JOIN Employer ON Job.employerID = Employer.employerID";
String sqlStm = "SET @SELECTED_ID = (SELECT employerID FROM Employer WHERE name = '"+comboEmployer.getSelectedItem().toString()+"') INSERT INTO Job (employerID,title,description,type,salary,benefits,vacancies,closing,requirement,placement,applyTo,status,posted,location)VALUES (@SELECTED_ID,?,?,?,?,?,?,?,?,?,?,?,?,?)";
pst = conn.prepareStatement(sqlStm);
pst.setString(2,txtTitle.getText());
pst.setString(3,areaDescription.getText());
pst.setString(4,comboType.getSelectedItem().toString());
pst.setString(5,txtSalary.getText());
pst.setString(6,areaBenefits.getText());
pst.setString(7,txtVac.getText());
pst.setString(8,txtDate.getDateFormatString().toString());
pst.setString(9,areaReq.getText());
pst.setString(10,comboPlace.getSelectedItem().toString());
pst.setString(11,txtWeb.getText());
pst.setString(12,comboStatus.getSelectedItem().toString());
pst.setString(13,"01-01-2013");
pst.setString(14,txtLoc.getText());
pst.executeUpdate();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,e);
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
}
}
}
我正在尝试允许用户向系统添加作业,而表单要求他们从Employer
表中选择雇主。我不确定如何将所选择的雇主与JCombobox
联系起来。即Job
表中的employerID应与employer
表的employerID匹配。我设法做了Select
声明,
sql = "SELECT Job.jobID as 'Job ID', Employer.name as'Company', Job.title as 'Role', Job.type as 'Type', Job.closing as 'Closing Date' FROM Job INNER JOIN Employer ON Job.employerID=Employer.employerID ORDER BY Employer.name";
但不确定如何INSERT
进入sqlite manager。下面是表格:
答案 0 :(得分:0)
您可以从前端编码本身处理它。 Combobox有两件事 - 值和文本。 ComboBox项的值应为Job id,文本应为Description。使用Select
查询检索此部分。然后,您可以插入Selected选项的值。因此将插入作业ID。
答案 1 :(得分:0)
我想我明白你要做什么。您正尝试使用表单上输入的值添加到“作业”表。由于雇主的下拉框采用文本形式,而“雇主”表仅接受雇主ID,因此您希望将雇主姓名翻译为雇主ID。
尝试使用此代码。它是用vanilla SQL编写的,因此您可能需要对SQLite进行一些小的调整
SET @SELECTED_ID = (SELECT employerID
FROM Employer
WHERE name = @your_text_box_variable)
INSERT INTO Job (employerID,title,description,type,salary,benefits,vacancies,closing,requirement,placement,applyTo,status,posted,location)
VALUES (@SELECTED_ID,?,?,?,?,?,?,?,?,?,?,?,?,?)
基本上,我们从表单中获取雇主姓名的值(使用变量@your_text_box_variable传输)并撤回与该名称相关联的雇主ID。然后,我们可以使用此雇主ID插入“工作”表。
答案 2 :(得分:0)
您的JComboBox
应包含Employer
类型的对象。该雇主类应该至少包含雇主的ID(能够唯一地识别他)和雇主的姓名(在组合框中显示)。
要插入新作业,只需从组合框中获取选定的雇主,获取其ID,然后将其传递给您的查询(不应该有任何内部联接):
insert into job (employerID,title, ...) values (?, ?, ...)