为了方便您阅读最新章节,请记住“闪文书库”网址 www.shanwen.com,注册会员收藏您喜爱的书籍
====>>>公告:晚上6点-11点高峰期,可能出现图片打开缓慢,如果太长时间没有打开,请刷新
第一卷第六章
?packagecom.mingo.sms2web.JNDICon;
/**
*
Title:SMS2WEB
*
Description:connectanywhere
*
Copyright:Copyright(c)2004
*
Company:speedmcu
*@authormingo
*@version1.0
*/
importjavax.naming.*;
importjavax.sql.*;
importjava.sql.*;
importjava.io.*;
publicclassDataConn{
privateContextctx;
privateDataSourceds;//数据源
privateConnectionconn=null;//数据连接
privateStatementstmt=null;//数据表达
publicResultSetrst=null;//数据结果集
publicStringsql;//sql语句
publicDataConn(StringDataBaseName){
init(DataBaseName);
}
/***********
初始化
************/
publicvoidinit(StringDataBaseName){
try{
ctx=newInitialContext();
if(ctx==null)
thrownewException("Boom-NoContext");
ds=
(DataSource)ctx.lookup(
"java:comp/env/jdbc/"+DataBaseName);
if(ds!=null){
//System.out.println("databaseOK"+ds);
conn=ds.getConnection();
if(conn!=null){
stmt=conn.createStatement();
//System.out.println("ConnOK:"+stmt);
}
}
}catch(Exceptione){
e.printStackTrace();
}
}
/*******************
*执行sql语句,用于insert
*****************/
publicbooleanExcuteSQL(Stringsql)
{
try
{
//System.out.println("sql:"+sql);
returnstmt.execute(sql);
}
catch(Exceptione)
{
e.printStackTrace();
}
returnfalse;
}
/*******************
*查询sql语句,用于select
*****************/
publicvoidSQLQuery(Stringsql)
{
try
{
rst=stmt.executeQuery(sql);
}
catch(Exceptione)
{
e.printStackTrace();
}
return;
}
/*******************
*更新的sql语句,用于Update
*****************/
publicintSQLUpdate(Stringsql)
{
try
{
returnstmt.executeUpdate(sql);
}
catch(Exceptione)
{
e.printStackTrace();
}
return0;
}
/**************
*关闭连接
***************/
publicintCloseConn()
{
try{
if(rst!=null){
try{rst.close();}
catch(SQLExceptione)
{System.out.print("rescolsetwice");}
rst=null;
}
stmt.close();
stmt=null;
conn.close();//Returntoconnectionpool
conn=null;//Makesurewedon-tcloseittwice
}catch(SQLExceptione){
System.out.println("CloseConnError");
}finally{
//Alwaysmakesureresultsetsandstatementsareclosed,
//andtheconnectionisreturnedtothepool
if(rst!=null){
try{rst.close();}
catch(SQLExceptione)
{System.out.print("rescolsetwice");}
rst=null;
}
if(stmt!=null){
try{stmt.close();}catch(SQLExceptione){;}
stmt=null;
}
if(conn!=null){
try{conn.close();}catch(SQLExceptione){;}
conn=null;
}
}
return1;
}
/****************
*获得Statement
****************/
publicStatementGetStatement()
{
returnstmt;
}
}
为了方便下次访问,请牢记闪文书库网址www.shanwen.com,您的支持是我们最大的动力。注册会员