How to implement Struts database applications (2)

zhaozj2021-02-16  51

[Related Links] "JavaWebStudio Series Development Tools Visaul Struts Report" http://dev.9cbs.net/develop/Article/28/28871.shtm "How to achieve the simplest struts program" http: //dev.9cbs. Net / development / article / 28 / 28946.SHTM "Struts drop-down box" http://dev.9cbs.net/develop/Article/28/28956.SHTM "http: // dev" http: // dev .9cbs.net / development / article / 29 / 29042.shtm "Struts check box" http://dev.9cbs.net/develop/Article/29/29043 The relationship between the SHTM "Struts program (1) "http://dev.9cbs.net/develop/article/29/29131.shtm" The relationship between the Struts program each file (2) "http://dev.9cbs.net/develop/Article/ 29 / 29133.SHTM "How to implement the struts database application (1)" http://dev.9cbs.net/develop/Article/29/29135.shtm "How to implement Struts Database Application (2)" How to in Struts Database Application Implementation Paging Display "How to Remove Remove, Update, and Link in Struts Database Application" "How to Add" How to Implement Record in Struts Database Application "" How to create a Struts database application with login page "How to create a Struts database application with landing page and role" The above example development environment: Java Web Studio Series Development Environment Visual Strutst version. The latest JavaWebStudio download address: ftp: //210.36.64.79/kui

[Program source code]

DBList.jsp file:

(Omit)

DBListOut.jsp file:

(Omit)

DBList.java file:

(Omit)

DBListDao.java file:

Here, a DAO class is defined to implement functions such as data plus, delete, update, add, and generate zone strings.

Package EmptyPRJ;

Import emptyprj.dblist;

Import java.sql.connection;

Import java.sql.preparedStatement;

Import java.sql.resultset;

Import java.sql.sqlexception;

Import java.util.collection;

Import java.util.arraylist;

Public class dblistdao {

PRIVATE CONNECTION CON;

PRIVATE INT ROWCOUNT;

PRIVATE INT pageCOUNT;

PRIVATE INT Length;

PRIVATE STRING PAGESTR;

Public int getLength () {return (this.Length);

Public void setlength (int length) {this.Length = Length;}

Public String getpageStr (int ipage)

{

String strpage = ""

IF (getLength ()> 0)

{

STRPAGE = "Total";

StrPage = String.Valueof (Rowcount);

STRPAGE = "Record, Total"; strpage = String.Valueof (PageCount);

STRPAGE = "page, currently the first";

StrPage = String.Valueof (ipage);

STRPAGE = "page,";

INT iStart, IEND;

iStart = ipage-5;

IF (iStart <0) {iStart = 0;}

IEND = ISTART 10;

Ie (IEND> PageCount) {IEND = PageCount;}

iStart = IEND-10;

IF (iStart <0) {iStart = 0;}

For (INT i = iStart; i

{

STRPAGE = "";

STRPAGE = String.Valueof (i 1);

STRPAGE = "";

STRPAGE = ""

}

}

this.pageStr = STRPAGE;

Return strpage;

}

Public dblistdao (connection con) {

THIS.CON = con;

}

(Omit)

Public Collection Findsql (String SQL, INT iPAG) {

PreparedStatement PS = NULL;

ResultSet RS = NULL;

ArrayList List = New ArrayList ();

Try {

IF (con?isclosed ()) {

Throw New IllegalStateException ("Error.unexpected");

}

PS = con.preparestatement (SQL, ResultSet.Type_Scroll_Sensitive, ResultSet.concur_Updata);

RS = ps.executeQuery ();

Rs.absolute (-1);

Rowcount = rs.getrow ();

INT offset = 1;

INT PAGESIZE = GetLength ();

IF (getLength () <1)

{

PageSize = rowcount;

PageCount = 1;

}

Else

{

PageCount = rowcount / getLength () ((RowCount% getLength ())> 0? 1: 0);

OFFSET = (ipage-1) * getLength () 1;

IF (Offset <1) OFFSET = 1;

IF (offset> rowcount) OFFSET = RowCount;

}

rs.absolute (offset);

For (int i = 0; i

DBLIST M_DBLIST = New dblist (); //

m_dblist.set serial number (Rs.Getint (1));

m_dblist.set name (rs.getstring (2));

M_dblist.set Remarks (rs.getstring (3));

//

rs.next ();

List.add (m_dblist);

}

Return List;

} catch (sqlexception e) {

//e.printstacktrace ();

// throw new runtimeException ("Error.unexpected");

Return List;

} finally {

Try {

IF (PS! = NULL)

ps.close ();

IF (rs! = null)

Rs.close ();

} catch (sqlexception e) {

E.PrintStackTrace ();

Throw new runtimeException ("error.unexpected");

}

}

}

}

DBListform.java file:

(Omit)

DBListAction.java file:

Here, an Action class is defined to implement the forwarding function of the page, and additional operations of different request types are also included. The Action variable represents an operation type, which is equivalent to the event type. Through this variable, it has achieved similar.

Net

The function of "event drive".

Package EmptyPRJ;

(Omit)

Public Final Class DBListAction Extends Action

{

// Variable definitions:

Private log log = logfactory.getlog ("org.apache.struts.Webapp.example");

PRIVATE CONNECTIONPOOL POOL;

Public dblistression () {

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.getations ();

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 Find Mode

String expression = request.getParameter ("expression"); // parameter, here to save record (row) ID data // // lookup database

//

String SQL = "SELECT * from dbo.test";

//

(Omit)

//

m_dblistdao.setlength (10); // Set the number of records per page

INT ipage; // current page

Try

{

String Page = Request.getParameter ("Page"); // Pedestrian Parameters, read the current page of the request

ipage = java.lang.integer.parseint (page, 10);

}

Catch (Exception E)

{

ipage = m_dblistform.getpage ();

}

IPAGE = 1;

Collection col = m_dblistdao.findsql (SQL, IPAGE); // Query data via DAO object

Request.setattribute ("dblist", col); // Save data

String PageStr = m_dblistdao.getpagestr (ipage); // Paging string, paging content

String s_find, str;

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 &"

{

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;

Request.setattribute ("dblistform", m_dblistform);

//

Return mapping.findforward ("Success");

}

Catch (SQLException E)

(Omit)

}

}

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

New Post(0)