Servlet calls JavaBean output result set example (transfer)

xiaoxiao2021-03-06  23

`/ Call the JavaBean output set set through servlet, and print

// javabean: Tbean Sourcecode

Package demo;

Import java.io. *;

Import java.sql. *;

/ **

* Insert the type description here.

* Create date: (00-7-10 8:30:32)

* @Author: administrator

* /

Public Class Tbean Implements Serializable {

Protected connection conn = NULL;

protected statement stmt = null;

protected java.sql.resultset result;

/ **

* TBean structure sub-annotation.

* /

Public tbean () {

Super ();

}

/ **

* Insert the method herein.

* Create date: (00-7-10 8:31:06)

* /

Public void execute () {

Try {

Class.Forname ("com.ibm.db2.jdbc.app.db2driver"). NewInstance ();

CONN = DriverManager.getConnection ("JDBC: DB2: Sample", "DB2Admin", "DB2Admin");

STMT = conn.createstatement ();

String SQL = "SELECT * from Staff Where DEPT = 20"

ResultSet RS = Stmt.executeQuery (SQL);

SETRESULT (RS);

} catch (sqlexception e) {

} catch (IllegaCCssexception E2) {

} catch (classnotfoundexception e3) {

} catch (InstantiationException E4) {

}

}

/ **

* Insert the method herein.

* Create date: (00-7-10 8:36:42)

* @Return java.sql.resultset

* /

Public java.sql.resultset getResult () {

Return Result;

}

/ **

* Insert the method herein.

* Create date: (00-7-10 8:36:42)

* @Param newResult java.sql.resultset

* /

Public void setResult (java.sql.resultset newresult) {

Result = newresult;

}

/ **

* Insert the method herein.

* Create date: (00-7-10 8:35:54)

* /

Public void sqlclose () {

Try {

CONN.CLOSE ();

Stmt.close ();

} catch (sqlexception e) {

}

}

}

// servlet: TSERVLET, call JavaBean, and print the result set:

Package demo;

Import java.io. *;

Import java.sql. *;

Import javax.servlet. *;

Import javax.servlet.http. *;

/ **

* Insert the type description here.

* Create date: (00-7-10 8:37:57)

* @Author: administrator * /

Public Class TSERVLET EXTENDS HTTPSERVLET IMPLEments SerializAble {

/ **

* TSERVLET constructor annotation.

* /

Public TSERVLET () {

Super ();

}

/ **

* Service method annotation.

* /

Public void service (httpservletRequest Req, httpservletResponse res) throws servletexception, oewception {

Try {

Demo.tbean javabean = new demo.tbean ();

Javabean.execute ();

ResultSet RS1 = javabean.getresult ();

PrintWriter out = res. maxwriter ();

Res.SetContentType ("text / html");

Out.println ("

");

Out.println ("

Hello World );

Out.println ("

");

For (INT i = 1; i <= 7; i )

Out.println ("

ID Name DEPT Job Years SALY < / TD> COMM ");

While (RS1.NEXT ()) {

Out.println ("

" RS1.GetString (i) "");

Out.println ("");

}

Out.println ("");

Javabean.sqlclose ();

} catch (sqlexception e) {

}

}

}

// Operation: In the environment of Visualage for Java's IBM WebSphere Test Environment:

//http://localhost:8080/servlet/demo.tservlet

Hello Print ID Name Dept Job Years Salary CommM

10 Sanders 20 MGR 7 18357.50 NULL

20 Pernal 20 Sales 8 18171.25 612.45

80 James 20 Clerk Null 13504.60 128.20

190 Sneider 20 Clerk 8 14252.75 126.50

(Author: Unknown Source: jsp lovers)

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

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.043, SQL: 9