1
CREATE TABLE "Employer" (
        "employerID" INT PRIMARY KEY NOT NULL,
        "name" CHAR,
        "industry" CHAR,
        "contact1" CHAR,
        "contact2" CHAR,
        "email" CHAR,
        "website" CHAR,
        "facts" CHAR,
        "phone" VACHAR
        )

CREATE TABLE "Job" (
        "jobID" INT PRIMARY KEY NOT NULL,
        "employerID" INT,
        "title" CHAR,
        "description" CHAR,
        "type" CHAR,
        "salary" CHAR,
        "benefits" CHAR,
        "vacancies" INT,
        "closing" CHAR,
        "requirement" CHAR,
        "placement" BOOL,
        "applyTo" CHAR,
        "notes" CHAR,
        FOREIGN KEY (employerID) REFERENCES Employer(employerID)
        )

我正在寻找从数据库中获取值并将它们设置为文本,但是当涉及到键JLabel时我不确定 sql 语句。foreignSQL 语句应该是表中的SELECT所有值,Job除了employerID并使用外键employerID来获取name公司的值...可以告诉我正确的 sql 语句。我想显示如下示例的信息:

分析师 - 微软

JobID: 4545454 描述: Good job email: 123@hotmail.com 电话: 45545

但不确定如何显示 MICROSOFT

public void setText() {
    try {
    String sql = "SELECT a.*, b.name FROM Employer a INNER JOIN `Job` b ON a.employerID = b.employerID";
    pst = conn.prepareStatement(sql); 
    rs = pst.executeQuery();
    while (rs.next()) {

        System.out.println(rs.getString(1)); //how Do i Print the name value from Employer table?? 
        System.out.println(rs.getString(2));
        System.out.println(rs.getString(3));
        System.out.println(rs.getString(4));
        System.out.println(rs.getString(5));
        System.out.println(rs.getString(6)); 
    }


}

如何打印nameEMployer 表中的值以及 Job 表中的所有数据?

4

1 回答 1

1

一个简单的连接就可以完成这项工作。

SELECT  a.*, b.*       // -- SELECT the columns you want to display
                       // -- eg b.Title
FROM    Employer a
        INNER JOIN `Job` b
            ON a.EmployerID = b.EmployerID
于 2013-01-03T15:39:26.690 回答