Use Bean)

Source: Internet
Author: User

-Data pagination-JDBC 2.0: ODBC
Jdbc: odbc can be used to implement Jsp operations on the database. In this example, I wrote the database connection in a JavaBean, which can be reused.
Pagetest. jsp file:

<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<% @ Page contentType = "text/html; charset = gb2312" %>
<Jsp: useBean id = "Htool" scope = "session" class = "zbean. HtmlTool"/>
<Jsp: useBean id = "Jodb" scope = "session" class = "zbean. Jodb"/>
<Html>
<Head>
<Title> database paging test </title>
</Head>
<%
// Define the ResultSet class
Java. SQL. ResultSet rst;

// Set the Odbc Data Source
Jodb. setConnStr ("jdbc: odbc: jtest ","","");

// Set the Jdbc driver
Jodb. setDbDriver ("sun. jdbc. odbc. JdbcOdbcDriver ");

// Execute the SQL statement and call the execute METHOD OF THE Jodb class
Rst=Jodb.exe cute ("select * from gbook ");
%>


<%

Int startRowNum;
Int pageSize = 10;
Rst. last ();
Int rowCount = rst. getRow ();
Int pageCount = (rowCount + pageSize-1)/pageSize;
Int intPage;
String strPage = request. getParameter ("page ");
If (strPage = null)
{
IntPage = 1;
}
Else
{
IntPage = java. lang. Integer. parseInt (strPage );
If (intPage <1) intPage = 1;
If (intPage> pageCount) intPage = pageCount;
}
StartRowNum = (intPage-1) * pageSize + 1;
%>
<Body>


<Div align = "center">
<Center>
<P> database paging test </p>
<P> <% = Htool. getStr (Jodb. pageStr (intPage, pageCount, "pagetest. jsp? "," En ") %> </p>
<Table border = "1" width = "600" bordercolorlight = "#000000" cellspacing = "0" cellpadding = "2" bordercolordark = "# FFFFFF">
<Tr>
<Td width = "49"> <font size = "2"> NO. </font> </td>
<Td width = "91"> <font size = "2"> last name </font> </td>
<Td width = "174"> <font size = "2"> email </font> </td>
<Td width = "250"> <font size = "2"> Leave a message </font> </td>
</Tr>

<%
For (int I = 0; I <pageSize; I ++ ){

Rst. absolute (startRowNum + I );

If (rst. isAfterLast ())
{
Break;
}

%>
<Tr>
<Td width = "49"> <% = rst. getLong ("id") %> </td>
<Td width = "91"> <% = rst. getString ("name") %> </td>
<Td width = "174"> <% = rst. getString ("email") %> </td>
<Td width = "250"> <% = rst. getString ("pnote") %> <% = rst. getRow () %> </td>
</Tr>
<%
}
%>
</Table>
</Center>
</Div>

</Body>
</Html>


The Jodb. java file is as follows:

Package zbean;
Import java. SQL .*;
// Import zbean .*;

Public class Jodb
{
Public String sdbdriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
Public String sConnStr;
Public long count;
String uid;
String pwd;
Connection conn = null;
ResultSet rs = null;

Public Jodb ()
{
Try
{
Class. forName (sdbdriver );
}
Catch (java. lang. ClassNotFoundException e)
{
System. err. println ("Jodb ():" + e. getMessage ());
}
}


Public void setDbDriver (String y)
{
Sdbdriver = y;
}

Public void setConnStr (String x, String z, String)
{
SConnStr = x;
Uid = z;
Pwd =;
}

Public ResultSet execute (String SQL)
{
Rs = null;


Try
{
Conn = DriverManager. getConnection (sConnStr, uid, pwd );
Statement stmt = conn. createStatement (java. SQL. ResultSet. TYPE_SCROLL_SENSITIVE, java. SQL. ResultSet. CONCUR_READ_ONLY );
Rs1_stmt.exe cuteQuery (SQL );
}
Catch (SQLException ex)
{
System. err. println ("Jodb.exe cute ():" + ex. getMessage ());
}
Return rs;
}

Public long update (String SQL)
{
Long x = 0;
Try
{
Conn = DriverManager. getConnection (sConnStr );
Statement stmt = conn. createStatement ();
X=stmt.exe cuteUpdate (SQL );
}
Catch (SQLException ey)
{
System. err. println ("Jodb. update ():" + ey. getMessage ());
}
Return x;
}

Public String pageStr (int page, int pageCount, String url, String showStr)
{
// String str = "Page :(" + page + "/" + pageCount + ")";
String str = "";
String fstr;
String pstr;
String nstr;
String lstr;
// Int page = currPage;
// Int pageCount = pageCt;

If (showStr = "cn ")
{
Fstr = "first page ";
Pstr = "Previous Page ";
Nstr = "next page ";
Lstr = "last page ";
}
Else if (showStr = "en ")
{
Fstr = "First ";
Pstr = "Previous ";
Nstr = "Next ";
Lstr = "Last ";
}
Else
{
String [] temp_array = split (showStr ,",");
If (temp_array = null)
{
Str = "Please input String like:" First, Previous, Next, Last "";
Return str;
}
Fstr = temp_array [0];
Pstr = temp_array [1];
Nstr = temp_array [2];
Lstr = temp_array [3];
}
/*
Int npage;
Npgae = page + 1;
Int ppage;
Ppage = page-1;
If (npage> pageCount)
{Npae = pageCount ;}
If (ppgae <1)
{Ppage = 1 ;}
*/
If (page = 1 ){
Str = str + "<a href =" + url + "page =" + (page + 1) + ">" + nstr + "</a> ";
Str = str + "<a href =" + url + "page =" + pageCount + ">" + lstr + "</a> ";
}
If (page = pageCount ){
Str = str + "<a href =" + url + "page = 1>" + fstr + "</a> ";
Str = str + "<a href =" + url + "page =" + (page-1) + ">" + pstr + "</a> ";
}
If (page> 1 & page <pageCount ){
Str = str + "<a href =" + url + "page = 1>" + fstr + "</a> ";
Str = str + "<a href =" + url + "page =" + (page-1) + ">" + pstr + "</a> ";
Str = str + "<a href =" + url + "page =" + (page + 1) + ">" + nstr + "</a> ";
Str = str + "<a href =" + url + "page =" + pageCount + ">" + lstr + "</a> ";
}

Return str;
}

Public String [] split (String str, String strIn)
{
Char [] temp_array;
Temp_array = str. toCharArray ();
Int strLength = str. length ();
Int strInLength = strIn. length ();
Int strInTimes = 0;
Int strIndex [] = new int [strLength];

Int I = 0;
Int ii = 0;
While (I <= strLength-strInLength)
{
String temp_str = "";
For (int j = I; j <I + strInLength; j ++)
{
Temp_str = temp_str + temp_array [j];
}
If (temp_str.equals (strIn ))
{
StrInTimes ++;
StrIndex [ii] = I;
I = I + strInLength;
Ii ++;
}
Else
{
I ++;
}

}

If (strInTimes <1)
{
String [] back_str = null;
Return back_str;
}
Else
{
String back_str [] = new String [strInTimes + 1];
Back_str [0] = str. substring (0, strIndex [0]);
For (int k = 1; k <strInTimes; k ++)
{
Back_str [k] = str. substring (strIndex [k-1] + strInLength, strIndex [k]);
}
Back_str [strInTimes] = str. substring (strIndex [strInTimes-1] + strInLength, str. length ());
Return back_str;
}

}

}

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.