How to implement records, update, and links in the Struts database application (continued)

zhaozj2021-02-16  47

How to implement records, update, and links in the Struts database application (continued)

"How to implement records in the Struts database application" http://dev.9cbs.net/develop/Article/29/29219.SHTM

DBListDao.java file:

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)

Public class dblistdao {

Unrelated to record updates, delete and add-independent code omitted. Public void create (dblist m_dblist) throws sqlexception {preparedStatement ps = null; // String SQL = "Insert Into dbo.test value (?,?,?)"; // try {ix .isclosed ()) {throw new illegalstateException ("Error.unexpected");} // Please join the following line with SQL Server; // SQL Server PS = Con.PrepareStatement (SQL); // < Create> ps.setint (1, m_dblist.get serial number ()); ps.setstring (2, m_dblist.get name ()); ps.setstring (3, m_dblist.get Remark ()); //

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");}}}

Public void update (dblist m_dblist, string keyid) {preparedState ps = null; // string sql = "Update dbo.test set number =?, name =?, note =? where serial number =?"; // < / updatesql> try {if (con?isclosed ()) {throw new IllegalStateException ("Error.unexpected");} // Use SQL Server to join the downward: Con.SetAutocommit (TRUE); // SQL Server PS = Con. PrepareStatement (SQL); // ps.setint (1, m_dblist.get serial number ()); ps.setstring (2, m_dblist.get Name ()); ps.setstring (3, m_dblist.get Remarks () ); // /// // ps.setint (4, java.lang.integer.parseint (keyid, 10)); //

IF (ps.executeUpdate ()! = 1) {throw new sqlexception ("error.removed.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");}}}

Public void removeid (String Keyid) {// string SQL = "delete from dbo.test where"; sql = "serial number"; SQL = "=?"; //

PreparedStatement ps = null; try {

IF (con?isclosed ()) {throw new illegalStateException ("error.unexpected");} // Please join the downlink with SQL Server; // SQL Server PS = Con.PrepareStatement (SQL); // ps.setint (1, java.lang.integer.parseint (keyid, 10)); // if (ps.executeUpdate ()! = 1) {throw new SQLException ("Error. REMOVED.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");}}}

Unrelated to record updates, delete and add-independent code omitted.

}

DBListAction.java file:

. . . . . . . . . . (Omit)

Public final class dblistAction extends action {// Variable definition: private log log = logfactory.getlog ("org.apache.struts.Webapp.example");

PRIVATE CONNECTIONPOOL POOL;

Public dblistivity () {pool = connectionpool.getinstance ();}

// function definition: 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); // Defines the DAO object to implement various operations of the database String action = request.getParameter ("action"); // Action Type String Search = Request.getParameter ("Search"); // Database Finding Mode String Expression = Request.getParameter ("Expression"); // Parameter, here to save record (line) ID data IF (action == null) Action = "find"; if (search == null) Search = "unsearch"; if (expression == null) expression = "" "" "" "; expression = tochinese (expression); // Processing Chinese issues, implementing encoding conversion IF (" Delete ".equals (action) // Delete record operation

{

m_dblistdao.removeid (expression);

}

IF ("Update" .Equals (action) || "insert" .Equals (action)) // update, add a record operation

{

DBLIST M_DBLIST = New DBLIST ();

//

m_dblist.set serial number (m_dblistform.get serial number ());

m_dblist.set name (m_dblistform.get name ());

M_dblist.set Remarks (M_DBListform.get Remarks ());

//

IF ("Update" .Equals (action) // Call DAO object update record

m_dblistdao.update (m_dblist, expression);

IF ("INSERT" .Equals (action) // Call DAO object Add Record

m_dblistdao.create (m_dblist);

}

/// Find database // string SQL = "SELECT * from dbo.test"; //

SQL Character Processing IF ("Search" .Equals (Search)) {// SQL = "WHER"; SQL = "Serial"; SQL = "="; if ("Null" .Equals (expression) || "" .equals (expression)) {SQL = m_dblistform .GET serial number ();} else {sql = expression;} SQL = ""; // } //

(Omitted) //

Return mapping.findforward ("Success");} catch (sqlexception e) {()

}

}

转载请注明原文地址:https://www.9cbs.com/read-25851.html

New Post(0)