java - java jstl中外键的DAO控制器
问题描述
我有 2 张桌子要加入。第一个表students
用几个字段命名 => (studentID,name, firstname, sex, dateofbirth, address, phone, email, status, dateregister, licenseID)
我的第二个表以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;
}
编辑:这是截图
添加学生方法()
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;
}
真诚的,我不明白我应该怎么做?
解决方案
使用连接,您无需在结果集查找中定义表名,因为它会返回两个表的新视图。
rs.getString("licenses.type_license");
将其更改为
rs.getString("type_license");
它应该可以工作。
如果可以,请查看运行该查询时返回的数据库中的完整表
**编辑他们是错误的类型。type_license
在 license 中是 String,在 student 中是 int。
要获取您需要解析它或更改其中一种类型的值。我喜欢;
studentBean.setTypeLicense(Integer.valueOf(rs.getString("type_license")));
但是有几种不同的方法可以做到这一点。
推荐阅读
- ruby - 使用 Rspec,我如何测试从 ruby 中的另一个类调用方法的方法
- mysql - 为什么这个 MySQL 递归公用表表达式 (CTE) 作为无限循环运行?
- python - 通过将列表元素转换为行和列,将列表转换为数据框 python
- google-cloud-platform - 知识图搜索 API 中缺少图像数据
- javascript - app.post 在 REACT JS 的 Firebase 数据库中不起作用(错误“=>”)
- spring-boot - Spring Boot 2.3.5 | 使用 gradle 将 jar 发布到本地 maven 存储库不起作用
- python - TypeError: 不支持的操作数类型 -: 'str' 和 'datetime.timedelta' 在 Dash 导入时
- android - 在 jsoup 上获取文本?
- python - 获取 .exe 与 .txt 交互
- sql-server - 删除日志文件夹后,mssql2019 服务也将无法启动