码迷,mamicode.com
首页 > 数据库 > 详细

向数据库插入数据第二种方法

时间:2014-10-22 21:33:03      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   os   ar   java   for   sp   数据   

<html>
<body>
<h1>学生添加</h1>
<s:form action="student/student_saveStudent" method="post" theme="simple"><br>
姓名:<s:textfield name="student.name" label="姓名" required="true"></s:textfield><br>
密码:<s:password label="密码" name="student.password" required="true"></s:password><br>
年龄:<s:textfield name="student.age" label="年龄" required="true"></s:textfield><br>
性别:<s:radio label="性别" name="student.sex" list="#{‘1‘:‘男‘,‘2‘:‘女‘}"></s:radio><br>
出生年月:<s:textfield name="student.dte" label="出生年月" onfocus="WdatePicker({dateFmt : ‘yyyy-MM-dd HH:mm:ss‘})" class="Wdate"></s:textfield><br>
爱好:<s:checkboxlist label="爱好" name="student.hobby" list="#{‘1‘:‘basketball‘,‘2‘:‘football‘,‘3‘:‘music‘,‘4‘:‘art‘}"></s:checkboxlist><br>
省份:<s:select id="addr1" label="省份 " name="province" list="{‘河北省‘,‘山东省‘}"></s:select>
城市:<select id="addr2"  name="city"></select><br>
地址:<s:textfield label="location" name="student.address" id="addr3"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>

将前台的name都设为student.**

package com.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.dao.StudentDao;
import com.dao.imp.StudentDaoImp;
import com.entity.Sheng;
import com.entity.Student;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StudentAction extends ActionSupport{
	private StudentDao dao=new StudentDaoImp();
	public StudentDao getDao() {
		return dao;
	}
	public void setDao(StudentDao dao) {
		this.dao = dao;
	}
	//new一个dao
	
	private Student student;
	//声明一个student对象
	public Student getStudent() {
		return student;
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	
	

	
	public String saveStudent(){
		
	
		dao.saveStudent(student);
		return "saveStudent";
	}
	
	
}

 在后台声明一个student对象   加上get set方法     就会自动得到前台对象的值 

这时候再调用dao.saveStudent(student);将其保存到数据库中即可

向数据库插入数据第二种方法

标签:blog   http   io   os   ar   java   for   sp   数据   

原文地址:http://www.cnblogs.com/tianhao/p/4044139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!