程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2EE >> 一個實用的javabean

一個實用的javabean

編輯:J2EE

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+=" <a href="+filename+"?page=0&start="+old_start+search+"><<</a>";
}
pagestart=start/pagetotal*page_line;//取得當前頁數
for(int i=0;i<total&&i<page_line;i++)
{
pagenum=pagestart+i+1;
returnstr+=" <a href="+filename+"?page="+i+"&start="+start+search+">"+pagenum+"</a>";
}
if(total_start!=(start/pagetotal+1)&&num!=0)
{
int new_start=start+pagetotal;
returnstr+=" <a href="+filename+"?page=0&start="+new_start+search+">>></a>";
}
return returnstr;
}
//-------------------------------取得時間
public String GetPath(){
String dateString="123";
try{
java.text.SimpleDateFormat formatter=new Java.text.SimpleDateFormat("yyyy-MM");
java.util.Date currentTime_1=new Java.util.Date();
dateString=formatter.format(currentTime_1);
}catch(Exception e){
}
return dateString;
}
//-------------------------------取得時間
public String strNowtime(){
String dateString="123";
try{
java.text.SimpleDateFormat formatter=new Java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
java.util.Date currentTime_1=new Java.util.Date();
dateString=formatter.format(currentTime_1);
}catch(Exception e){
}
return dateString;
}
//--------------------------去掉Html---------------------------
public String HtmlEncode(String Str){
Str=ebbReplace(Str,"<","<");
Str=ebbReplace(Str,">",">");
Str=ebbReplace(Str,"\n","<BR>");
return Str;
}
//--------------------------替換變量---------------------------
public String ebbReplace(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 LCReplace(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=ebbReplace(ReStr,"$lichao$",Str1);
Str1=BStr+Str1+EStr;
Str=ebbReplace(Str,Str1,Str2);
n=i+Str2.length()-Str1.length();
}
}
ReturnStr=Str;
return ReturnStr;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved