JSP数据库操作例程(Use Bean)
程序员文章站
2023-12-17 16:58:46
- 数据分页显示 - jdbc 2.0:odbc 通过jdbc:odbc可以实现jsp对数据库的操作,在这个例子中我将数据库的连接写在了一个javabean中,可以实现重复...
- 数据分页显示 - 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;
}
}
}
通过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;
}
}
}