日期:2014-05-18 浏览次数:20687 次
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name=request.getParameter("name");
String sage=request.getParameter("age");
int age=Integer.parseInt(sage);
String sex=request.getParameter("sex");
String aclass=request.getParameter("class");
JavaBean jBean=new JavaBean();
jBean.setName(name);
jBean.setAge(age);
jBean.setSex(sex);
jBean.setAclass(aclass);
String sql="Insert into Stu(name,age,sex,class) values (?,?,?,?)";
SqlBean sBean=new SqlBean();
int count=sBean.getInsert(sql, jBean);
String urlString="";
if(count>0){
urlString="SearchList.jsp";
}else{
urlString="error.jsp";
request.setAttribute("error", "插入数据");
}
request.getRequestDispatcher(urlString).forward(request, response);
}
public int getInsert(String sql,JavaBean jBean) {
int count=0;
try {
pre=con.prepareStatement(sql);
pre.setString(1, jBean.getName());
pre.setInt(2, jBean.getAge());
pre.setString(3, jBean.getSex());
pre.setString(4, jBean.getAclass());
count=pre.executeUpdate(sql);
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
finally{
try {
pre.close();
con.close();
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
return count;
}
public class JavaBean {
private int id;
private String name;
private int age;
private String sex;
private String aclass;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAclass()&nb