JSP資料庫操作常式(Use Bean)_JSP編程
最後更新:2017-01-18
來源:互聯網
上載者:User
- 資料分頁顯示 - 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+") ";
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> ";
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;
}
}
}