您的位置:首页 > 编程语言 > Java开发

如何在Struts 数据库应用程序中实现记录的添加(续)

2008-04-25 06:26 603 查看
《如何在Struts 数据库应程序中实现记录添加http://dev.csdn.net/develop/article/29/29224.shtm[/b][/b] [程序源代码][/b]对于程序源代码,[/b]本例唯一[/b]与《如何实现Struts 数据库应用程序》中的[/b]程序源代码[/b]不同之处,是多了[/b]dbListInsert.jsp文件,是[/b]添加记录页面文件。[/b][/b]dbListInsert.jsp文件:[/b]<%@ page contentType="text/html;charset=GB2312" language="java" %><%@ taglib uri="/WEB-INF/Struts-bean.tld" prefix="bean" %><%@ taglib uri="/WEB-INF/Struts-html.tld" prefix="html" %><%@ taglib uri="/WEB-INF/Struts-logic.tld" prefix="logic" %><%@ taglib uri="/WEB-INF/Struts-template.tld" prefix="template" %> <html:html ><head><title></title><META http-equiv=Content-Type content="text/html; charset=gb2312"> </head><body bgColor=white><html:base /> <html:form method="post" action="/dbListAction.do?action=insert&search=search&expression=NULL"><table cellPadding=2 width="80%" border=2><tr><td>序号</td><td><html:text property='序号' /></td></tr><tr><td>姓名</td><td><html:text property='姓名' /></td></tr><tr><td>备注</td><td><html:text property='备注' /></td></tr></table><html:submit value="添加新记录" /> <html:reset value="重写" /> </html:form ></body></html:html > [/b] dbListDAO.java文件:[/b]  这里定义一个DAO类,用于实现数据加的查找、删除、更新、添加及生成分页字符串等功能。getPagestr(int ipage)函数用于生成分页字符串,int ipage参数用于转递当前页码。findSQL(String sql,int ipage)函数用于查找数据库数据,String sql参数用于转递SQL查询语句字符串,int ipage参数用于转递当前页码。。。。。。。。。。。(省略)[/b]public class dbListDAO { 与[/b]记录添加无关的代码[/b]省略。[/b] public void create(dbList m_dbList) throws SQLException { PreparedStatement ps = null; //<createSQL> String sql = "INSERT INTO dbo.Test VALUES (?,?,?)"; //</createSQL> try { if (con.isClosed()) { throw new IllegalStateException("error.unexpected"); } //用SQL Server请加入下行: con.setAutoCommit(true); //SQL Server ps = con.prepareStatement(sql); //<create> ps.setInt(1,m_dbList.get序号()); ps.setString(2,m_dbList.get姓名()); ps.setString(3,m_dbList.get备注()); //</create> if (ps.executeUpdate() != 1) { throw new SQLException ("error.create.dbList"); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("error.unexpected"); } finally { try { if (ps != null) ps.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("error.unexpected"); } } } 省略[/b] } dbListAction.java文件:。。。。。。。。。。(省略)[/b]public final class dbListAction extends Action { // 变量定义: private Log log = LogFactory.getLog("org.apache.Struts.webapp.Example"); private ConnectionPool pool; public dbListAction() { pool = ConnectionPool.getInstance(); } //函数定义: public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes and parameters we will need Locale locale = getLocale(request); MessageResources messages = getResources(request); HttpSession session = request.getSession(); dbListForm m_dbListForm = (dbListForm) form; Connection con = null; try { con = pool.getConnection(); dbListDAO m_dbListDAO = new dbListDAO(con); //定义DAO对象,用于实现数据库的各种操作 String action=request.getParameter("action"); //动作类型 String search=request.getParameter("search"); //数据库查找方式 String expression=request.getParameter("expression"); //参数,这里用于保存记录(行)的ID数据 if(action==null)action="find"; if(search==null)search="UNsearch"; if(expression==null)expression=""; expression=toChinese(expression); //处理中文问题,实现编码转换 if("delete".equals(action)) //删除记录操作[/b] {[/b] m_dbListDAO.removeID(expression);[/b] }[/b] ////////////[/b] if("update".equals(action)||"insert".equals(action)) //更新、添加记录操作[/b] {[/b] dbList m_dbList= new dbList(); [/b] //<update>[/b] m_dbList.set序号(m_dbListForm.get序号());[/b] m_dbList.set姓名(m_dbListForm.get姓名());[/b] m_dbList.set备注(m_dbListForm.get备注());[/b] //</update>[/b] if("update".equals(action)) //调用DAO对象更新记录[/b] m_dbListDAO.update(m_dbList,expression);[/b] if("insert".equals(action)) //调用DAO对象添加记录[/b] m_dbListDAO.create(m_dbList);[/b] }[/b] ///////////////查找数据库 //<sqlstr> String sql="SELECT * FROM dbo.Test"; //</sqlstr> ////SQL字符处理 if("search".equals(search)) { //<search> sql+=" where "; sql+="序号"; sql+="="; if("NULL".equals(expression)||"".equals(expression)) { sql+=m_dbListForm .get序号(); } else { sql+=expression; } sql+=""; //</search> } //<分页> m_dbListDAO.setLength(10);//设置每页显示记录数 int ipage;//当前页 try { String page=request.getParameter("page");//分页参数,读取请求的当前页 ipage=java.lang.Integer.parseInt(page,10); } catch (Exception e) { ipage=m_dbListForm.getPage(); } if(ipage<1)ipage=1; Collection col = m_dbListDAO.findSQL(sql,ipage);//通过DAO对象查询数据 request.setAttribute("dbList",col); //保存数据 String pagestr=m_dbListDAO.getPagestr(ipage);//分页字符串,分页内容 String s_find,str; if(!"search".equals(search)) //查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。如果是全部显示则去掉"search=search&" { s_find="search=search&"; while(pagestr.indexOf(s_find)!=-1) { str=pagestr.substring(0,pagestr.indexOf(s_find)); str+=pagestr.substring(pagestr.indexOf(s_find)+s_find.length(),pagestr.length()); pagestr=str; } } m_dbListForm.setPagestr(pagestr); m_dbListForm.setAction(action); request.setAttribute("dbListForm",m_dbListForm); //</分页> return mapping.findForward("success"); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("Unable to get connection."); } finally { try { if (con != null) con.close(); } catch (SQLException e) { throw new RuntimeException(e.getMessage()); } } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: