分析以下代码并给出修改后的代码数据库结构:gender enum男女 not null default 男public class Students private int age;public int getAge return age; public static void mainString args throws Exception Stud
问题出在以下代码段: s.setAge(30); s.setGender("男");
原因是数据库中的age字段是整数类型,而代码中将"男"赋给了age字段,导致类型不匹配,因此报错。
修改后的代码: public class Students { private int age; private String gender;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public static void main(String[] args) throws Exception {
StudentDAOImpl sdao = new StudentDAOImpl();
//添加学生
Students s = new Students();
s.setStuNo(UUID.randomUUID().toString());
s.setName("韦汉臣");
s.setAge(30);
s.setGender("男");
s.setBirth("1992-12-15");
Clazz c = new Clazz();
c.setId(1);
s.setCid(c);
boolean flag = sdao.addStudent(s);
System.out.println(flag);
}
}
修改后的代码在Students类中添加了一个gender字段,并提供了对应的getGender和setGender方法。这样可以将"男"作为字符串类型赋值给gender字段,解决了类型不匹配的问题
原文地址: https://www.cveoy.top/t/topic/iEV6 著作权归作者所有。请勿转载和采集!