Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2016-06-01 14:11:09
Original
1026 people have browsed it



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








数据库分页测试

//定义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


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

}

}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template