My second JSP program uses JavaBean

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.