快捷搜索:

一个实用的javabean

import java.sql.*;

import java.util.*;

import java.text.*;

import java.io.*;

public class jdbc_sql

{

String conn_str="jdbc:mysql://localhost/HeaVeN_news";//---------数据库

String username="root";//---------------数据库用户名

String password="";//-------------数据库密码

private Connection conn=null;

private Statement stmt=null;

ResultSet rs=null;

//----------------------数据库链接----------------

public jdbc_sql()

{

try{

Class.forName("org.gjt.mm.mysql.Driver");

}

catch(java.lang.ClassNotFoundException e)

{

System.err.println("dbclass():"+e.getMessage());

}

try{

conn=DriverManager.getConnection(conn_str,username,password);

}

catch(SQLException ex) {

System.err.println("dbclass():"+ex.getMessage());

}

}

//---------------------数据库查询-----------------

public ResultSet query(String sql)

{

try{

stmt=conn.createStatement();

rs=stmt.executeQuery(sql);}

catch(SQLException ex) {

System.err.println("query():"+ex.getMessage());

}

return rs;

}

//-----------数据库改动---------

public void update(String sql)

{

try{

stmt=conn.createStatement();

stmt.executeUpdate(sql);}

catch(SQLException ex) {

System.err.println("update():"+ex.getMessage());

}

}

//----------关闭数据源-------

public void closestmt()

{

try{stmt.close();

}

catch(SQLException ex) {

System.err.println("closestmt():"+ex.getMessage());

}

}

//----------关闭链接---------

public void closeconn()

{try{conn.close();}

catch(SQLException ex) {System.err.println("closeconn()"+ex.getMessage());}

}

//---------------------转换中文

public String toChinese(String s){

if(s==null)

return null;

try{

String convert=new String(s.getBytes("gb2312"),"ISO8859-1");

return convert;

}catch(Exception e){}

return null;

}

//--------------------分页

public String page1(int num,int line,int page_line,int start,int page,String search,String filename){

int pagetotal=line*page_line;//所要显示的总条数

int pagestart;//

int pagenum;//

int total=(num-start+line-1)/line;//取得总页数

int total_start=(num+pagetotal-1)/pagetotal;//取得总偏移数

String returnstr="";

if(start!=0)

{

int old_start=start-pagetotal;

returnstr+="<<";

}

pagestart=start/pagetotal*page_line;//取适合前页数

for(int i=0;i",">");

Str=ebbWordStr(Str,"

","

");

return Str;

}

//--------------------------调换变量---------------------------

public String ebbWordStr(String Str,String oldStr,String newStr){

String ReturnStr="";

int i,j,t,m,n;

n=0;

j=oldStr.length();

if (Str.indexOf(oldStr)>-1)

{

while(Str.indexOf(oldStr,n)>-1)

{

i=Str.length();

if (Str.indexOf(oldStr)==0)

Str=newStr+Str.substring(j,i);

else

{

t=Str.indexOf(oldStr);

m=(t+j);

Str=Str.substring(0,t)+newStr+Str.substring(m,i);

n=t+newStr.length()-j+1;

}

}

}

ReturnStr=Str;

return ReturnStr;

}

//----------------------------ubb调换--------------------------

public String LCWordStr(String Str,String BStr,String EStr,String ReStr){

String ReturnStr="",Str1="",Str2="";

int i,j,n;

n=0;

if ((Str.indexOf(BStr)>-1)&&((Str.indexOf(EStr)>-1)))

{

while(Str.indexOf(BStr,n)>-1)

{

i=Str.indexOf(BStr);

j=Str.indexOf(EStr);

Str1=Str.substring((i+BStr.length()),j);

Str2=ebbWordStr(ReStr,"$lichao$",Str1);

Str1=BStr+Str1+EStr;

Str=ebbWordStr(Str,Str1,Str2);

n=i+Str2.length()-Str1.length();

}

}

ReturnStr=Str;

return ReturnStr;

}

}

您可能还会对下面的文章感兴趣: