How to implement records in the Struts database application (continued)

zhaozj2021-02-16  44

"How to achieve records in the Struts database application" http://dev.9cbs.net/develop/Article/29/29224.shtm

[Program source code]

For program source code, this example is uniquely different from the program source code in "How to implement Struts Database Application", which is a dblistinsert.jsp file, which is to add a record page file.

DBListInsert.jsp file:

<% @ 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 "%>

</p> <p></ title></p> <p><</p> <p>Meta</p> <p>http-equiv = content-type content = "text / html; charSet = GB2312"></p> <p></ hEAD></p> <p><body bgcolor = white></p> <p><html: base /> <html: form method = "post" action = "/ dblistion.do?action=insert&search=search&pression=null"></p> <p><Table cellpadding = 2 width = "80%" Border = 2> <Tr> <TD> Sequence Number </ TD> <TD> <HTML: Text Property = 'Serial No.' /> </ TD> </ TR></p> <p><TR> <TD> Name </ TD> <TD> <HTML: Text Property = 'Name' /> </ TD> </ TR></p> <p><TR> <TD> Remarks </ TD> <TD> <HTML: Text Property = 'Note' /> </ TD> </ TR></p> <p></ TABLE></p> <p><html: submit value = "Add new record" /> <html: reset value = "rewrite" /> </ html: form></p> <p></ body></p> <p></ html: html></p> <p>DBListDao.java file:</p> <p>Here, a DAO class is defined to implement functions such as data plus, delete, update, add, and generate zone strings. The getPagestr (int ipage) function is used to generate a pages string, and the int ipage parameter is used to transfer the current page number. The FindSQL (STRING SQL, INT iPAGE) function is used to find database data, and the String SQL parameter is used to deliver the SQL query statement string, and the int ipage parameter is used to transfer the current page number. . . . . . . . . . . (Omit)</p> <p>Public class dblistdao {</p> <p>Unrelated code that is not related to the record is omitted.</p> <p>Public void create (dblist m_dblist) throws sqlexception {</p> <p>PreparedStatement PS = NULL;</p> <p>// <createSQL></p> <p>String SQL = "INSERT INTO DBO.TEST VALUES (?,?,?)</p> <p>// </ createsql></p> <p>Try {</p> <p>IF (con?isclosed ()) {</p> <p>Throw New IllegalStateException ("Error.unexpected");</p> <p>}</p> <p>// Please join the down with SQL Server:</p> <p>Con.SetAutocommit (TRUE);</p> <p>// SQL Server</p> <p>PS = con.preparestatement (SQL);</p> <p>// <crete></p> <p>Ps.setint (1, m_dblist.get serial number ());</p> <p>ps.setstring (2, m_dblist.get name ());</p> <p>Ps.setstring (3, m_dblist.get Remarks ());</p> <p>// </ create></p> <p>IF (ps.executeUpdate ()! = 1) {</p> <p>Throw New Sqlexception ("Error.create.dblist");</p> <p>}</p> <p>} catch (sqlexception e) {</p> <p>E.PrintStackTrace ();</p> <p>Throw new runtimeException ("error.unexpected");</p> <p>} finally {</p> <p>Try {</p> <p>IF (PS! = NULL)</p> <p>ps.close ();</p> <p>} catch (sqlexception e) {</p> <p>E.PrintStackTrace ();</p> <p>Throw new runtimeException ("error.unexpected");</p> <p>}</p> <p>}</p> <p>}</p> <p>Omit</p> <p>}</p> <p>DBListAction.java file:</p> <p>. . . . . . . . . . (Omit)</p> <p>Public Final Class DBListAction Extends Action</p> <p>{</p> <p>// Variable definitions:</p> <p>Private log log = logfactory.getlog ("org.apache.struts.Webapp.example");</p> <p>PRIVATE CONNECTIONPOOL POOL;</p> <p>Public dblistression () {</p> <p>Pool = connectionPool.getInstance ();</p> <p>}</p> <p>// Function definition:</p> <p>Public ActionForward Execute (ActionMapping Mapping,</p> <p>Actionform Form,</p> <p>HTTPSERVLETREQUEST REQUEST,</p> <p>Httpservletresponse response</p> <p>Throws Exception</p> <p>{</p> <p>// Extract Attributes and Parameters WE WILL NEED</p> <p>Locale locale = getLocale (request);</p> <p>MessageResources Messages = getResources (request); httpsession session = request.getations ();</p> <p>DBListForm m_dblistform = (dblistform) form;</p> <p>Connection con = NULL;</p> <p>Try</p> <p>{</p> <p>Con = pool.getConnection ();</p> <p>DBListDao m_dblistdao = new dblistdao (con); // Defines the DAO object to implement various operations of the database.</p> <p>String action = Request.getParameter ("action"); // action type</p> <p>String Search = Request.getParameter ("Search"); // Database Find Mode</p> <p>String Expression = Request.getParameter ("Expression"); // Parameter, here to save record (row) ID data</p> <p>IF (Action == Null) action = "find";</p> <p>IF (search == null) Search = "unsearch";</p> <p>IF (Expression == NULL) Expression = "";</p> <p>Expression = toCHINESE (Expression); // Processing Chinese issues, implementing coding conversion</p> <p>IF ("delete" .equals (action) // Delete record operation</p> <p>{</p> <p>m_dblistdao.removeid (expression);</p> <p>}</p> <p>IF ("Update" .Equals (action) || "insert" .Equals (action)) // update, add a record operation</p> <p>{</p> <p>DBLIST M_DBLIST = New DBLIST ();</p> <p>// <update></p> <p>m_dblist.set serial number (m_dblistform.get serial number ());</p> <p>m_dblist.set name (m_dblistform.get name ());</p> <p>M_dblist.set Remarks (M_DBListform.get Remarks ());</p> <p>// </ update></p> <p>IF ("Update" .Equals (action) // Call DAO object update record</p> <p>m_dblistdao.update (m_dblist, expression);</p> <p>IF ("INSERT" .Equals (action) // Call DAO object Add Record</p> <p>m_dblistdao.create (m_dblist);</p> <p>}</p> <p>// / Find a database</p> <p>// <sqlstr></p> <p>String SQL = "SELECT * from dbo.test";</p> <p>// </ sqlstr></p> <p>SQL character processing</p> <p>IF ("Search" .equals (search))</p> <p>{</p> <p>// <search></p> <p>SQL = "Where";</p> <p>SQL = "Sequence Number";</p> <p>SQL = "=";</p> <p>IF ("NULL" .Equals (expression) || "" "" "Equals (expression))</p> <p>{</p> <p>SQL = m_dblistform .GET serial number ();</p> <p>}</p> <p>Else</p> <p>{</p> <p>SQL = Expression;</p> <p>}</p> <p>SQL = ""; // </ Search></p> <p>}</p> <p>// <Page></p> <p>m_dblistdao.setlength (10); // Set the number of records per page</p> <p>INT ipage; // current page</p> <p>Try</p> <p>{</p> <p>String Page = Request.getParameter ("Page"); // Pedestrian Parameters, read the current page of the request</p> <p>ipage = java.lang.integer.parseint (page, 10);</p> <p>}</p> <p>Catch (Exception E)</p> <p>{</p> <p>ipage = m_dblistform.getpage ();</p> <p>}</p> <p>IPAGE = 1;</p> <p>Collection col = m_dblistdao.findsql (SQL, IPAGE); // Query data via DAO object</p> <p>Request.setattribute ("dblist", col); // Save data</p> <p>String PageStr = m_dblistdao.getpagestr (ipage); // Paging string, paging content</p> <p>String s_find, str;</p> <p>IF (! "Search" .Equals (search) // lookup and all display two different operations, which are different, which is handled here. If all it is displayed, remove "Search = Search &"</p> <p>{</p> <p>s_find = "Search = SEARCH &";</p> <p>While (PageStr.Indexof (s_find)! = - 1)</p> <p>{</p> <p>Str = pageStr.Substring (0, PageStr.Indexof (s_find));</p> <p>Str = pageStr.substring (PageStr.Indexof (s_find) s_find.length (), pageStr.Length ());</p> <p>PageStr = STR;</p> <p>}</p> <p>}</p> <p>m_dblistform.setpageStr (PageStr);</p> <p>m_dblistform.setAction;</p> <p>Request.setattribute ("dblistform", m_dblistform);</p> <p>// </ page></p> <p>Return mapping.findforward ("Success");</p> <p>}</p> <p>Catch (SQLException E)</p> <p>{</p> <p>E.PrintStackTrace ();</p> <p>Throw new runtimeException ("Unable to get connection.");</p> <p>}</p> <p>Finally</p> <p>{</p> <p>Try</p> <p>{</p> <p>IF (con! = null)</p> <p>C. close ();</p> <p>}</p> <p>Catch (SQLException E)</p> <p>{</p> <p>Throw new runtimeException (E.GetMessage ());</p> <p>}</p> <p>}</p> <p>}</p> <p>}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-25849.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="25849" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.058</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'miQe_2Fu3RMoUIqUxsgE4n_2B5jcgJ3LkrrEB3wihtgelKZ0xDImPlX_2F85aM7jpfQGiZd01A1Jcv4mNDlM81'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>