81,094
社区成员
发帖
与我相关
我的任务
分享
public class AddStudent extends ActionSupport{
private static final long serialVersionUID = 1178440535366734465L;
private String name;
private String password;
private String sex;
private String email;
private String jiguan;
private String phone;
private String tel;
private String id;
private String department;
private StudentDAO stuDao;
private StudentDAO getStudentDAO(){
return DAOFactory.getInstance().createStudentDAO();
}
public String execute() throws Exception{
stuDao = this.getStudentDAO();
Student stu = new Student();
try{
stu.setId(new String(getId()));
stu.setName(new String(getName().getBytes("ISO-8859-1"),"GB2312"));
stu.setPassword(new String(getPassword().getBytes("ISO-8859-1"),"UTF-8"));
stu.setDepartment(new String(getDepartment().getBytes("ISO-8859-1"),"utf-8"));
stu.setSex(new String(getSex().getBytes("ISO-8859-1"),"utf-8"));
stu.setJiguan(new String(getJiguan().getBytes("ISO-8859-1"),"utf-8"));
stu.setEmail(getEmail());
stu.setPhone(getPhone());
stu.setTel(getTel());
System.out.println(getName() +getPhone()+getDepartment());
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
boolean result=stuDao.saveStudent(stu);
if(result){
return SUCCESS;
}else{
return INPUT;
}
}
//......setter getter
}
public class StudentDAOImp implements StudentDAO {
private static Log log = LogFactory.getLog(StudentDAOImp.class);
//保存学生信息
public boolean saveStudent(Student stu){
System.out.println(stu.getName() +stu.getPhone());
try{
Session s= HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.save(stu);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
}catch(Exception e){
log.fatal(e);
}
return false;
}
}
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/stuman</property>
<property name="connection.username">root</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
<property name="connection.password">123</property>
<property name="connection.pool_size">5</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- <property name="hbm2ddl.auto">create</property> -->
<property name="show_sql">true</property>
<mapping resource="Admin.hbm.xml" />
<mapping resource="Teacher.hbm.xml"/>
<mapping resource="Student.hbm.xml"/>
<mapping resource="Course.hbm.xml"/>
<mapping resource="Classes.hbm.xml"/>
<mapping resource="Enrol.hbm.xml"/>
</session-factory>
</hibernate-configuration>