站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题JSP数据库操作例程

JSP数据库操作例程

摘要: JSP数据库操作例程


通过jdbc:odbc可以实现Jsp对数据库的操作,在这个例子中我将数据库的连接写在了一个JavaBean中,可以实现重复使用
pagetest.jsp文件:

HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@page contentType="text/html;charset=gb2312" %>



<head>
数据库分页测试
</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>




数据库分页测试


<%= Htool.getStr(Jodb.pageStr(intPage,pageCount,"pagetest.jsp?","en")) %>


<table border="1" width="600" bordercolorlight="#000000" cellspacing="0" cellpadding="2" bordercolordark="#FFFFFF">
<tr>
<td width="49">编号</td>
<td width="91">姓 名</td>
<td width="174">电子邮箱</td>
<td width="250">留言</td>
</tr>

<%
for(int i=0;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>



</body>



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+""+nstr+" ";
str=str+""+lstr+" ";
}
if(page==pageCount){
str=str+""+fstr+" ";
str=str+""+pstr+" ";
}
if(page>1&&pagestr=str+""+fstr+" ";
str=str+""+pstr+" ";
str=str+""+nstr+" ";
str=str+""+lstr+" ";
}

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

}

}

</td> </tr> <tr> <td vAlign=top align=left height="100%">
↑返回目录
前一篇: 一个Struts实现分页,增删改查,Tiles,国际化的DEMO
后一篇: Jsp + JavaBean循序渐进教程(一)