[
复制到剪贴板 ]
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;
}
}
/*
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/