JSP database operation routine (Use Bean)
Author:Eve Cole
Update Time:2009-07-02 17:13:24
- Data display in pages - JDBC 2.0: ODBC
Jsp can operate the database through jdbc:odbc. 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 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.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>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">Number</font></td>
<td width="91"><font size="2">Name</font></td>
<td width="174"><font size="2">E-mail</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;
publicJodb()
{
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="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;
}
}
}