1

表雇主

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 管理器。继承人的形式:在此处输入图像描述

4

3 回答 3

0

您可以从前端编码本身处理它。Combobox 有两个东西 - 值和文本。ComboBox 项的值应为 Job id,文本应为 Description。使用您的Select查询检索此部分。然后您可以插入 Selected 选项的值。因此将插入作业 ID。

于 2013-01-07T19:43:44.810 回答
0

我想我明白你想要做什么。您正在尝试使用表单上输入的值添加到“工作”表。由于雇主的下拉框是文本形式,并且“雇主”表只接受雇主 ID,因此您需要将雇主名称转换为雇主 ID 的内容。

试试这个代码。它是用普通 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 插入到“工作”表中。

于 2013-01-07T19:53:10.870 回答
0

JComboBox应该包含类型的对象Employer。这个 Employer 类至少应该包含雇主的 ID(以便能够唯一地识别他)和雇主的名称(在组合框中显示)。

要插入新工作,只需从组合框中获取选定的雇主,获取其 ID,然后将其传递给您的查询(不应有任何内部连接):

insert into job (employerID,title, ...) values (?, ?, ...)
于 2013-01-07T20:01:35.060 回答