hibernate+struts2.0向数据库中添加数据和修改数据。

[size=xx-large]1、在上面的利用hibernate和配置文件来建立数据库的基础上表如下:[/size]CREATE TABLE `_leavemessage` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(20) DEFAULT NULL,
`province` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
`company` varchar(255) DEFAULT NULL,
`position` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`content` text,
`addTime` varchar(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

[size=xx-large]2、创建LeaveMessageAction.java[/size]

package com.sipu.wcme.action.newscenter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sipu.wcme.action.newcenter.dao.LeaveMessageDao;

import com.sipu.wcme.bean.LeaveMessage;

public class LeaveMessageAction extends ActionSupport {

private static final long serialVersionUID = 1L;

/**
* 留言人类别。
*/
private String type;
/**
* 留言的省份。
*/
private String province;
/**
* 留言人姓名。
*/
private String name;
/**
* 留言人所在公司。
*/
private String company;
/**
* 留言人的职位。
*/
private String position;
/**
* 留言人邮箱。
*/
private String email;
/**
* 留言内容。
*/
private String content;

private String addTime;

private List<LeaveMessage> list;


public String getAddTime() {
return addTime;
}


public void setAddTime(String addTime) {
this.addTime = addTime;
}


public String getType() {
return type;
}


public void setType(String type) {
this.type = type;
}


public String getProvince() {
return province;
}


public void setProvince(String province) {
this.province = province;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getCompany() {
return company;
}


public void setCompany(String company) {
this.company = company;
}


public String getPosition() {
return position;
}


public void setPosition(String position) {
this.position = position;
}


public String getEmail() {
return email;
}


public void setEmail(String email) {
this.email = email;
}


public String getContent() {
return content;
}


public void setContent(String content) {
this.content = content;
}


/**
* 插入用户留言。
*/
public String execute() throws Exception {
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String currentTime = formatter.format(curDate);

LeaveMessageDao dao=new LeaveMessageDao();
LeaveMessage obj=new LeaveMessage();

obj.setType(type);
obj.setProvince(province);
obj.setPosition(position);
obj.setCompany(company);
obj.setName(name);
obj.setEmail(email);
obj.setContent(content);
obj.setAddTime(currentTime);

System.out.println(type+province+position);

if(dao.addMessage(obj))
return SUCCESS;
else
return "error";
}


/**
* 查询用户留言。
*/
public String queryMessage() throws Exception {

LeaveMessageDao dao=new LeaveMessageDao();

list = dao.queryMessage();

return SUCCESS;

}


public List<LeaveMessage> getList() {
return list;
}


public void setList(List<LeaveMessage> list) {
this.list = list;
}

}
[size=xx-large]3、建立一个处理逻辑的LeaveMessageDao.java文件。[/size]package com.sipu.wcme.action.newcenter.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.sipu.wcme.bean.LeaveMessage;
import com.sipu.wcme.util.HibernateSessionFactory;

public class LeaveMessageDao {

public List<LeaveMessage> queryMessage() {
List<LeaveMessage> list = new ArrayList<LeaveMessage>();
Session se = HibernateSessionFactory.getSession();
LeaveMessage lm=new LeaveMessage();

try {
se.beginTransaction();
String hql = "from LeaveMessage order by addTime desc";

Query query1 = se.createQuery(hql);
list = query1.list();

se.beginTransaction().commit();
} catch (Exception e) {
se.beginTransaction().rollback();
//将来记录到日志中
e.getMessage();
e.printStackTrace();
} finally{
if(se.isOpen()){
se.close();
}
}

return list;
}
/**
*
* @param obj
* @return
*/
public boolean addMessage(LeaveMessage obj) {
Session session = HibernateSessionFactory.getSession();
try {

session.beginTransaction();
session.save(obj);
session.beginTransaction().commit();
System.out.println("--------------success------------");
return true;
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("------------failed----------------");
return false;
}
finally{
if(session.isOpen()){
session.close();
}
}

}
}

[size=xx-large]4、表单提交页面部分代码[/size]<FORM id="form1" method="post" name="form1" action="newscenter/message/message.action">
<DIV> </DIV>
<DIV id=pagebox>
<LINK rel=stylesheet type=text/css href="css/newscenter_css/global.css">
<LINK title=style2 rel=stylesheet type=text/css href="css/newscenter_css/global_orange.css">
<LINK title=style3 rel=stylesheet type=text/css href="css/newscenter_css/global_green.css">
<SCRIPT type=text/javascript src="script/newscenter_script/replaceStyle.js"></SCRIPT>

<SCRIPT language=javascript type=text/javascript>
//跳出框架
if (top.location !== self.location) {
//top.location=self.location;
}
</SCRIPT>

<DIV>
<DIV id=galleryleft>
<DIV><IMG src="images/newscenter_images/message/ifairbanner1.jpg"><IMG
src="images/newscenter_images/message/ifairbanner2.jpg"><IMG
src="images/newscenter_images/message/ifairbanner3.jpg"><IMG
src="images/newscenter_images/message/ifairbanner4.jpg"></DIV>
<DIV id=navigate><A href="http://www.cantonfair.org.cn/cn/index.asp">首页</A>
>> <A href="http://www.cantonfair.org.cn/cn/info/default.aspx">多媒体新闻中心</A>
>> <A
href="http://www.cantonfair.org.cn/cn/info/MyCantonfair.aspx">我的广交会</A></DIV>
<DIV id=myvoice class=galleryleft>
<DIV id=myvoice_tit class=ifair_tit>
<DIV class=left><IMG id=img_title
src="images/newscenter_images/message/wyvoice_tit.gif"></DIV>
<DIV class=right><A
href="http://www.cantonfair.org.cn/cn/info/voicing.shtml"></A></DIV>
<DIV class=clear></DIV></DIV>
<DIV id=voices>
<DIV id=upload_form>
<DIV
style="BORDER-BOTTOM: #ddd 1px solid; PADDING-BOTTOM: 10px; MARGIN-BOTTOM: 10px"
class=red14>
<P>上传您在现场拍摄的照片,宏伟的广交会展馆、与采购商洽谈、特装风采、为展客商服务…… 在广交会官方网站留下您的身影,或分享您镜头中的广交会。</P>
<P></P></DIV>
<DIV class=name>我的留言</DIV>
<DIV class=select><TEXTAREA style="WIDTH: 320px; HEIGHT: 109px" id="content" rows=2 cols=20 name="content"></TEXTAREA></DIV>
<DIV class=name>我是</DIV>
<DIV class=select><SELECT style="WIDTH: 100px" id="type" name="type">
<OPTION selected value=参展商>参展商</OPTION> <OPTION value=采购商>采购商</OPTION> <OPTION
value=服务人员>服务人员</OPTION> <OPTION value=其他>其他</OPTION></SELECT> </DIV>
<DIV class=name>我来自(国家/地区)</DIV>
<DIV class=select><INPUT style="WIDTH: 314px" id="province" type="text"
name="province"> </DIV>
<DIV class=name>我的名字</DIV>
<DIV class=select><INPUT style="WIDTH: 314px" id="name" type="text"
name="name"></DIV>
<DIV class=name>我的公司</DIV>
<DIV class=select><INPUT style="WIDTH: 314px" id="company" type="text"
name="company"></DIV>
<DIV class=name>职位</DIV>
<DIV class=select><INPUT style="WIDTH: 314px" id="position" type="text"
name="position"></DIV>
<DIV class=name>电子邮件</DIV>
<DIV class=select><INPUT style="WIDTH: 314px" id="email" type="text"
name="email"></DIV>
<DIV class=name>验证码</DIV>
<DIV class=select><INPUT onBlur="this.className='colorblur ';"
style="WIDTH: 63px" id=tbCheckCode class=colorblur
οnfοcus="this.className='colorfocus';" maxLength=5 type=text
name=tbCheckCode>  <IMG style="CURSOR: hand" id=checkcode
title=看不清验证码?点击图片更换另外一个验证码 οnclick=reloadcode();
src="images/newscenter_images/message/checkcode.gif"></DIV>
<DIV class=name></DIV>
<DIV class=select><INPUT id="Button1" value=提交 type="submit" name="Button1"> </DIV>
<DIV class=clear></DIV></DIV>
</form>
[size=xx-large]5、查询了显示出来的数据。[/size]<UL>

<!-- 利用迭代器将数据库里面的数据取出来显示-->
<s:iterator value="list" >
<DIV class=list><SPAN><s:property value="content"/><BR></SPAN>
<BR><BR><SPAN>——[参展商]<s:property value="company"/></SPAN>
</DIV>
</s:iterator>

</UL>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值