白云岛资源网 Design By www.pvray.com
- 数据分页显示 - JDBC 2.0:ODBC
通过jdbc:odbc可以实现Jsp对数据库的操作,在这个例子中我将数据库的连接写在了一个JavaBean中,可以实现重复使用
pagetest.jsp文件:

<!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>数据库分页测试</title>
</head>
<%
//定义ResultSet类
java.sql.ResultSet rst;

//设定Odbc数据源
Jodb.setConnStr("jdbc:odbc:jtest","","");

//设定Jdbc驱动程序
Jodb.setDbDriver("sun.jdbc.odbc.JdbcOdbcDriver");

//执行Sql语句,调用Jodb类的execute方法
rst=Jodb.execute("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>数据库分页测试</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">编号</font></td>
   <td width="91"><font size="2">姓 名</font></td>
   <td width="174"><font size="2">电子邮箱</font></td>
   <td width="250"><font size="2">留言</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>


Jodb.java文件如下:

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 a)
{
sConnStr=x;
uid=z;
pwd=a;
}

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);
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("Jodb.execute():"+ex.getMessage());
}
return rs;
}

public long update(String sql)
{
long x=0;
try
{
conn=DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
x=stmt.executeUpdate(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+")&nbsp;&nbsp;";
String str="";
String fstr;
String pstr;
String nstr;
String lstr;
//int page=currPage;
//int pageCount=pageCt;

if(showStr=="cn")
{
fstr="第一页";
pstr="上一页";
nstr="下一页";
lstr="最末页";
}
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>&nbsp;";
str=str+"<a href="+url+"page="+pageCount+">"+lstr+"</a>&nbsp;";
}
if(page==pageCount){
str=str+"<a href="+url+"page=1>"+fstr+"</a>&nbsp;";
str=str+"<a href="+url+"page="+(page-1)+">"+pstr+"</a>&nbsp;";
}
if(page>1&&page<pageCount){
str=str+"<a href="+url+"page=1>"+fstr+"</a>&nbsp;";
str=str+"<a href="+url+"page="+(page-1)+">"+pstr+"</a>&nbsp;";
str=str+"<a href="+url+"page="+(page+1)+">"+nstr+"</a>&nbsp;";
str=str+"<a href="+url+"page="+pageCount+">"+lstr+"</a>&nbsp;";
}

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;
}

}

}
白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com