Is to put the first JSPProgramThe background section in is encapsulated in the JavaBean. In my opinion, the JavaBean is the same as the OCX control in ASP. It is concise and easy to use. It is a Java class used by JSP, which is essentially no different from Java.
The following is Bean'sCode:
/*
* Creation date:
*
* Change the generated file template
* WINDOW> preferences> JAVA> code generation> code and comments
*/
Package test;
Import java. SQL .*;
/**
* @ Author Administrator
*
* Change the template of the generated type comment
* WINDOW> preferences> JAVA> code generation> code and comments
*/
Public class helloworld {
Public resultset rs = NULL;
Public connection con = NULL;
Public helloworld ()
{
}
Public String getname ()
{
Return "generate a list of results called by RS on the page using JavaBean :";
}
Public resultset getlist () throws sqlexception
{
Try {
String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = liveflow ";
String user = "sa"; // database username
String Password = "sa"; // Database User Password
String sqlstr = "select Top 100 * From employee_base ";
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
Con = drivermanager. getconnection (URL, user, password );
Statement ST = con. createstatement ();
Rs = st.exe cutequery (sqlstr );
}
Catch (exception ee)
{
System. Out. println ("Connect dB error:" + ee. getmessage ());
}
// Finally
//{
// Con. Close ();
//}
Return Rs;
}
}
The following is the JSP code:
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<% @ Page
Language = "Java"
Import = "Java. SQL .*"
Contenttype = "text/html; charset = GBK"
Pageencoding = "GBK"
%>
<Meta http-equiv = "Content-Type" content = "text/html; charset = GBK">
<Meta name = "generator" content = "IBM WebSphere Studio">
<Meta http-equiv = "content-style-type" content = "text/CSS">
<Link href = "Theme/master.css" rel = "stylesheet" type = "text/CSS">
<Title> beantest. jsp </title>
</Head>
<Body>
<JSP: usebean class = "test. helloworld" id = "hello"> </jsp: usebean>
<% = Hello. getname () %>
<%
Int currpage = 0;
Try
{
Currpage = java. Lang. Integer. parseint (request. getparameter ("page"); // current page number}
}
Catch (exception ex)
{
Ex. getmessage ();
}
// Int rownum = 0; // number of record rows
// Int pagesize = 5;
// Int totalpages = 0;
Resultset rs = Hello. getlist (); // obtain the record set.
// Rownum = Rs. getint ("total_rows"); // obtain the total number of rows.
// Totalpages = rownum/pagesize + 1; // total number of pages
// If (rownum % pagesize = 0) & (rownum! = 0 )){
// Totalpages = totalpages-1 ;}
%>
<Table border = 1>
<%
// If (Java. Lang. Integer. parseint (request. getparameter ("page")> = 1)
//{
// Rs. Absolute (currpage * pagesize + 1 );
//} %>
<% While (Rs. Next ())
{%>
<Tr> <TD> <% = Rs. getstring ("employee_id") %> </TD> <% = Rs. getstring ("employee_name") %> </TD> <% = Rs. getstring ("post_name") %> </TD> <% = Rs. getstring ("rela_phone") %> </TD> <% = Rs. getstring (6) %> </TD> </tr>
<% }%>
<Tr> <TD> <a href = beantest. jsp? Page = <% = currpage-1 %> previous page </a>
<A href = beantest. jsp? Page = <% = currpage + 1%> next page </a> </TD> <tr>
</Table>
</Body>
</Html>