当前位置:首页 > 网络编程 > WEB编程 > JSP > JSP数据库操作例程(Use Bean)

JSP数据库操作例程(Use Bean)

点击次数:21 次 发布日期:2008-11-26 15:49:03 作者:源代码网
源代码网推荐 - 数据分页显示 - 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;
源代码网推荐 }
源代码网推荐
源代码网推荐 }
源代码网推荐
源代码网推荐 }
源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华