gpt4 book ai didi

java - java jSTL 中外键的 DAO Controller

转载 作者:行者123 更新时间:2023-12-02 08:40:33 25 4
gpt4 key购买 nike

我有 2 张 table 要加入。第一个表名为 students,有几个字段 => (studentID、姓名、名字、性别、出生日期、地址、电话、电子邮件、状态、日期注册、许可证 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"));

未为 Student 类型定义 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;
}

编辑:这是屏幕截图

enter image description here

AddStudent方法()

public static int addStudent(Student studentBean){  
int status=0;
try{
Connection con=getConnection();
PreparedStatement ps=con.prepareStatement("insert into students(name, firstname, sex, dateofbirth, address, phone, email, status, dateregister, licenseID) values(?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,studentBean.getName());
ps.setString(2,studentBean.getFirstname());
ps.setString(3,studentBean.getDateofbirth());
ps.setString(4,studentBean.getSex());
ps.setString(5,studentBean.getAddress());
ps.setString(6, studentBean.getPhone());
ps.setString(7, studentBean.getEmail());
ps.setString(8, studentBean.getStatus());
ps.setString(9, studentBean.getDateregister());
ps.setInt(10, studentBean.getLicenseID());
status=ps.executeUpdate();
}catch(Exception e){System.out.println(e);}
return status;
}

真诚地,我不明白我应该做什么?

最佳答案

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

rs.getString("licenses.type_license");

将其更改为

rs.getString("type_license");

它应该可以工作。

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

**编辑他们是错误的类型。 type_license 在 licence 中是 String,在 Student 中是 int。

要获取解析它或更改其中一种类型所需的值。我喜欢;

studentBean.setTypeLicense(Integer.valueOf(rs.getString("type_license")));

但是您可以通过几种不同的方式来做到这一点。

关于java - java jSTL 中外键的 DAO Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61414375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com