2个回答
展开全部
//dao中
public class StudentDAO {
public boolean addStudent(Student student){
Connection conn=null;
PreparedStatement pstat=null;
String sql="insert into t_student(student_password,student_name,student_sex,student_nation," +
"student_age)values(?,?,?,?,?)";
try {
conn=DBConnection.getConnection();
pstat=conn.prepareStatement(sql);
pstat.setString(1,student.getStudentPassword());
pstat.setString(2,student.getStudentName());
pstat.setString(3,student.getStudentSex());
pstat.setString(4,student.getStudentNation());
pstat.setInt(5,student.getStudentAge());
int flg=pstat.executeUpdate();
if(flg>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DBConnection.close(null, pstat, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
//action中
package com.softeem.action;
import com.opensymphony.xwork2.ActionSupport;
import com.softeem.dao.StudentDAO;
import com.softeem.dto.Student;
public class AddAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -5853742467378306000L;
private StudentDAO studentDAO=new StudentDAO();
private Student student = new Student();
public String add(){
boolean flg=studentDAO.addStudent(student);
if(flg){
return "success";
}else{
return "error";
}
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
//struts配置文件!
<struts>
<package name="struts" extends="struts-default">
<action name="adds" class="com.softeem.action.AddAction">
<result name="success">/login.jsp</result>
<result name="error">/registry.jsp</result>
</action>
<struts>
//jsp中
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="<%=request.getContextPath()%>/adds!add" method="post">
用户名:<input type="text" name="student.studentName"><s:fielderror fieldName="studentName"/><br>
密码:<input type="text" name="student.studentPassword"><br>
民族:<input type="text" name="student.studentNation"><br>
性别:<input type="text" name="student.studentSex"><br>
年龄:<input type="text" name="student.studentAge"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
不知道能不能满足你的问题,希望够详细。!!希望能采纳
public class StudentDAO {
public boolean addStudent(Student student){
Connection conn=null;
PreparedStatement pstat=null;
String sql="insert into t_student(student_password,student_name,student_sex,student_nation," +
"student_age)values(?,?,?,?,?)";
try {
conn=DBConnection.getConnection();
pstat=conn.prepareStatement(sql);
pstat.setString(1,student.getStudentPassword());
pstat.setString(2,student.getStudentName());
pstat.setString(3,student.getStudentSex());
pstat.setString(4,student.getStudentNation());
pstat.setInt(5,student.getStudentAge());
int flg=pstat.executeUpdate();
if(flg>0){
return true;
}else{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
DBConnection.close(null, pstat, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
//action中
package com.softeem.action;
import com.opensymphony.xwork2.ActionSupport;
import com.softeem.dao.StudentDAO;
import com.softeem.dto.Student;
public class AddAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = -5853742467378306000L;
private StudentDAO studentDAO=new StudentDAO();
private Student student = new Student();
public String add(){
boolean flg=studentDAO.addStudent(student);
if(flg){
return "success";
}else{
return "error";
}
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
//struts配置文件!
<struts>
<package name="struts" extends="struts-default">
<action name="adds" class="com.softeem.action.AddAction">
<result name="success">/login.jsp</result>
<result name="error">/registry.jsp</result>
</action>
<struts>
//jsp中
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="<%=request.getContextPath()%>/adds!add" method="post">
用户名:<input type="text" name="student.studentName"><s:fielderror fieldName="studentName"/><br>
密码:<input type="text" name="student.studentPassword"><br>
民族:<input type="text" name="student.studentNation"><br>
性别:<input type="text" name="student.studentSex"><br>
年龄:<input type="text" name="student.studentAge"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
不知道能不能满足你的问题,希望够详细。!!希望能采纳
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询