Java jstl中外键的DAO控制器

问题描述 投票:0回答:1

我有2个表要加入。第一个表名为students,具有多个字段=>(学生ID,姓名,名字,性别,生日,地址,电话,电子邮件,状态,日期寄存器,许可证ID)

我的第二个表名为licenses,具有2个字段=>(licenseID,type_license)。

我的SQL请求似乎正确,在下面:

SELECT students.studentID, students.name, students.firstname, students.dateofbirth, students.sex, students.address, 
students.phone, students.email, students.status, students.dateofbirth, licenses.type_license 

FROM students 

INNER JOIN licenses 

ON students.licenseID = licenses.licenseID

我想我的模特也可以

模型学生

package com.autoecole.model;


public class Student {

    private int studentID;
    private String name;
    private String firstname;
    private String dateofbirth;
    private String sex;
    private String address;
    private String phone;
    private String email;
    private String status;
    private String dateregister;
    private int licenseID;

    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setDateofbirth(String dateofbirth) {
        this.dateofbirth = dateofbirth;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setDateregister(String dateregister) {
        this.dateregister = dateregister;
    }

    public void setLicenseID(int licenseID) {
        this.licenseID = licenseID;
    }
    public int getStudentID() {
        return studentID;
    }

    public String getName() {
        return name;
    }

    public String getFirstname() {
        return firstname;
    }

    public String getDateofbirth() {
        return dateofbirth;
    }

    public String getSex() {
        return sex;
    }

    public String getAddress() {
        return address;
    }

    public String getPhone() {
        return phone;
    }

    public String getEmail() {
        return email;
    }

    public String getStatus() {
        return status;
    }

    public String getDateregister() {
        return dateregister;
    }

    public int getLicenseID() {
        return licenseID;
    }   

}

模型许可

public class License {

    private int licenseID;
    private String type_license;

    public void setLicenseID(int licenseID) {
        this.licenseID = licenseID;
    }

    public void setTypeLicense(String type_license) {
        this.type_license = type_license;
    }

    public int getLicenseID() {
        return licenseID;
    }

    public String getTypeLicense() {
        return type_license;
    }

}

在我的getAllRecordsStudents()方法中,我在此行上显示一条错误消息:

 studentBean.setTypeLicense(rs.getString("licenses.type_license"));

未为学生类型定义setTypeLicense(String)方法

public static List getAllRecordsStudents(){  
        List <Student> list=new ArrayList<Student>();  

        try{  
            Connection con=getConnection();  
            PreparedStatement ps=con.prepareStatement("SELECT students.studentID, students.name, students.firstname, students.dateofbirth, students.sex, students.address, students.phone, students.email, students.status, students.dateofbirth, licenses.type_license FROM students INNER JOIN licenses ON students.licenseID = licenses.licenseID");  
            ResultSet rs=ps.executeQuery();  
            while(rs.next()){  
                Student studentBean =new Student();  
                studentBean.setStudentID(rs.getInt("studentID"));  
                studentBean.setName(rs.getString("name"));  
                studentBean.setFirstname(rs.getString("firstname")); 
                studentBean.setDateofbirth(rs.getString("dateofbirth"));  
                studentBean.setSex(rs.getString("sex"));  
                studentBean.setAddress(rs.getString("address")); 
                studentBean.setPhone(rs.getString("phone")); 
                studentBean.setEmail(rs.getString("email")); 
                studentBean.setStatus(rs.getString("status")); 
                studentBean.setDateregister(rs.getString("dateregister")); 
                studentBean.setTypeLicense(rs.getString("licenses.type_license"));
                list.add(studentBean);  
            }  
        }catch(Exception e){System.out.println(e);}  
        return list;  
    }  

真诚地,我不知道该怎么办?

java jstl java-ee-8
1个回答
0
投票

使用联接,您无需在结果集查找中定义表名称,因为它会返回两个表的新视图。

rs.getString("licenses.type_license");

将其更改为

rs.getString("type_license");

它应该起作用。

如果可以,请查看运行该查询时返回的数据库中的完整表

© www.soinside.com 2019 - 2024. All rights reserved.