一个封装了基本JDBC操作的类

[ 复制到剪贴板 ]
packagebbs;

/*

databaseoperationclass,testbyodbc

Thisjavabeaniswrittenbyzergling

Itismyfirstjavabean:o

version1.01

*/

importjava.sql.*;

importjava.lang.*;

importjava.io.*;

importjava.util.*;

importsun.io.*;

publicclassodbc

{

ConnectionsqlCon;

ResultSetrstSql;

StatementstmS;

StringstrCon;

StringstrSql;

booleanstatus;

longrowcount;

intpage;

intpagesize;

longpagecount;

longfirstrecord;

//connecttothedefaultdatabase

publicbooleanconnect()

{

//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

this.strCon="jdbc:odbc:jspbbs";//replacewithyourdefaultdatabase

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

this.sqlCon=java.sql.DriverManager.getConnection(this.strCon,"sa","");//replacewithyourdefaultdatabaseconnectionconfigureoption

this.status=true;

returntrue;

}

catch(Exceptione)

{

this.status=false;

returnfalse;

}

}

//connecttothecustomdatabase

publicbooleanconnect(StringconName,Stringusername,Stringpassword)

{

//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

this.strCon=conName;

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

this.sqlCon=java.sql.DriverManager.getConnection(this.strCon,username,password);

this.status=true;

returntrue;

}

catch(Exceptione)

{

this.status=false;

returnfalse;

}

}

//executesql(insert,update,delete,...)

publicbooleanexecute(Strings)

{

try

{

this.stmS=this.sqlCon.createStatement();

this.stmS.executeUpdate(s);

this.status=true;

returntrue;

}

catch(Exceptione)

{

this.status=false;

returnfalse;

}

}

//querythedatafromdatabase

publicbooleanquery(Strings)

{

try

{

this.rowcount=0;

this.stmS=this.sqlCon.createStatement();

this.rstSql=this.stmS.executeQuery(s);

while(this.nextrecord())

{

this.rowcount++;

}

this.rstSql=this.stmS.executeQuery(s);

this.status=true;

returntrue;

}

catch(Exceptione)

{

this.status=false;

returnfalse;

}

}

//returntherowcount

publiclonggetrowcount()

{

returnthis.rowcount;

}

//returnthepagecount

publiclonggetpagecount()

{

returnthis.pagecount;

}

//returntheresultsetofdata

publicStringgetstring(Strings)

{

try

{

returnthis.rstSql.getString(s);

}

catch(Exceptione)

{

return"notexists";

}

}

publicintgetint(Strings)

{

try

{

returnInteger.parseInt(this.rstSql.getString(s));

}

catch(Exceptione)

{

return0;

}

}

//resultsetmoveforward

publicbooleannextrecord()

{

try

{

returnthis.rstSql.next();

}

catch(Exceptione)

{

returnfalse;

}

}

//setcurrentpage(recallfirst)

publicbooleansetpage(intsize,intno)

{

this.pagesize=size;

this.page=no;

this.pagecount=Math.round((this.rowcount-1)/this.pagesize)+1;

this.firstrecord=this.pagesize*(this.page-1);

try

{

for(inti=0;i
if(!this.nextrecord())break;

returntrue;

}

catch(Exceptione)

{

returnfalse;

}

}

//getstringfromdatabaseandchangeitintochinese

publicStringreadChinese(Strings)

{

try

{

Stringtemp=newString(s.getBytes("GB2312"),"8859_1");

returntemp;

}

catch(Exceptione)

{

returns;

}

}

//writestringtothedatabase"schinesetransform

publicstaticStringwriteChinese(Strings)

{

char[]orig=s.toCharArray();

byte[]dest=newbyte[orig.length];

for(inti=0;i
dest[i]=(byte)(orig[i]&0xFF);

try

{

ByteToCharConvertertoChar=ByteToCharConverter.getConverter("gb2312");

returnnewString(toChar.convertAll(dest));

}

catch(Exceptione)

{

returns;

}

}

//string"ssearchandreplace

publicStringreplace(Stringcon,Stringtag,Stringrep){

intj=0;

inti=0;

intk=0;

StringRETU="";

Stringtemp=con;

inttagc=tag.length();

while(i
if(con.substring(i).startsWith(tag)){

temp=con.substring(j,i)+rep;

RETU+=temp;

i+=tagc;

j=i;

}

else{

i+=1;

}

}

RETU+=con.substring(j);

returnRETU;

}

publicVectorlistValue(Stringcon,Stringtag){

intj=0;

inti=0;

intk=0;

Vectorvv=newVector();

Stringtemp=con;

inttagc=tag.length();

while(i
if(con.substring(i).startsWith(tag)){

temp=con.substring(j,i);

vv.addElement(temp);

i+=tagc;

j=i;

}

else{

i+=1;

}

}

vv.addElement(con.substring(j));

returnvv;

}

//filtthehtmlcode&sqlsymbol

publicStringhtmlencode(Strings)

{

try

{

Stringtemp=this.replace(s,"
temp=this.replace(temp,">",">");

temp=this.replace(temp,""",""");

temp=this.replace(temp,""",""");

temp=this.replace(temp,""," ");

temp=this.replace(temp,"

","
");

returntemp;

}

catch(Exceptione)

{

returns;

}

}

//returnthestatusoflastoperation

publicbooleangetstatus()

{

returnthis.status;

}

//closeallobject

publicbooleanclose()

{

try

{

if(this.sqlCon!=null)this.sqlCon.close();

if(this.rstSql!=null)this.rstSql.close();

if(this.stmS!=null)this.stmS.close();

this.status=true;

returnfalse;

}

catch(Exceptione)

{

this.status=false;

returntrue;

}

}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7178747/viewspace-160956/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/7178747/viewspace-160956/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值