odbc.java
———————————————
package bbs;
/*
database operation class, test by odbc
this javabean is written by zergling
it is my first javabean 😮
version 1.01
*/
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import sun.io.*;
public class odbc
{
connection sqlcon;
resultset rstsql;
statement stms;
string strcon;
string strsql;
boolean status;
long rowcount;
int page;
int pagesize;
long pagecount;
long firstrecord;
//connect to the default database
public boolean connect()
{
//class.forname("sun.jdbc.odbc.jdbcodbcdriver");
this.strcon = "jdbc:odbc:jspbbs"; //replace with your default database
try
{
class.forname("sun.jdbc.odbc.jdbcodbcdriver");
this.sqlcon = java.sql.drivermanager.getconnection(this.strcon,"sa",""); //replace with your default database connection configure option
this.status = true;
return true;
}
catch(exception e)
{
this.status = false;
return false;
}
}
//connect to the custom database
public boolean connect(string conname,string username,string password)
{
//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;
return true;
}
catch(exception e)
{
this.status = false;
return false;
}
}
//execute sql(insert,update,delete,…)
public boolean execute(string s)
{
try
{
this.stms = this.sqlcon.createstatement();
this.stms.executeupdate(s);
this.status = true;
return true;
}
catch(exception e)
{
this.status = false;
return false;
}
}
//query the data from database
public boolean query(string s)
{
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;
return true;
}
catch(exception e)
{
this.status = false;
return false;
}
}
//return the row count
public long getrowcount()
{
return this.rowcount;
}
//return the pagecount
public long getpagecount()
{
return this.pagecount;
}
//return the resultset of data
public string getstring(string s)
{
try
{
return this.rstsql.getstring(s);
}
catch(exception e)
{
return "not exists";
}
}
public int getint(string s)
{
try
{
return integer.parseint(this.rstsql.getstring(s));
}
catch(exception e)
{
return 0;
}
}
//resultset move forward
public boolean nextrecord()
{
try
{
return this.rstsql.next();
}
catch(exception e)
{
return false;
}
}
//set current page (recall first)
public boolean setpage(int size,int no)
{
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(int i=0;i<this.firstrecord;i++)
if (!this.nextrecord()) break;
return true;
}
catch(exception e)
{
return false;
}
}
//get string from database and change it into chinese
public string readchinese(string s)
{
try
{
string temp = new string(s.getbytes("gb2312"),"8859_1");
return temp;
}
catch(exception e)
{
return s;
}
}
//write string to the databases chinese transform
public static string writechinese(string s)
{
char[] orig =s.tochararray();
byte[] dest =new byte[orig.length];
for(int i=0;i<orig.length;i++)
dest[i] =(byte)(orig[i]&0xff);
try
{
bytetocharconverter tochar =bytetocharconverter.getconverter("gb2312");
return new string(tochar.convertall(dest));
}
catch(exception e)
{
return s;
}
}
//strings search and replace
public string replace(string con ,string tag,string rep){
int j=0;
int i=0;
int k=0;
string retu="";
string temp =con;
int tagc =tag.length();
while(i<con.length()){
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);
return retu;
}
public vector listvalue(string con ,string tag){
int j=0;
int i=0;
int k=0;
vector vv=new vector();
string temp =con;
int tagc =tag.length();
while(i<con.length()){
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));
return vv;
}
//filt the html code & sql symbol
public string htmlencode(string s)
{
try
{
string temp = this.replace(s,"<","<");
temp = this.replace(temp,">",">");
temp = this.replace(temp,"",""");
temp = this.replace(temp,"\"",""");
temp = this.replace(temp," "," ");
temp = this.replace(temp,"\n","<br>");
return temp;
}
catch(exception e)
{
return s;
}
}
//return the status of last operation
public boolean getstatus()
{
return this.status;
}
//close all object
public boolean close()
{
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;
return false;
}
catch(exception e)
{
this.status = false;
return true;
}
}
}
