利用Oracle自带的连接池类的一例

来源:互联网 发布:手机淘宝首页展示位置 编辑:程序博客网 时间:2024/06/05 18:38
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

  author:yancheng(sharetopstudio)

  from:javacool.com

  /**

  封装了对数据库的连接,用于处理SQL语句。

  @author:yancheng(sharetopstudio)

  @version:1.0.0

  */

  packageDBUtil;

  importjava.sql.*;

  importjava.io.*;

  importjavax.sql.*;

  importjavax.naming.*;

  importOracle.jdbc.pool.*;

  publicclassOraPooledSQL

  {

  privatePooledConnectiondbpool;

  /**

  @paramConnectionURL连接名如:jdbc:odbc:myODBC

  @paramUserID用户名

  @paramPassWord用户密码

  */

  publicOraPooledSQL(StringConnectionURL,StringUserID,StringPassWord)

  {

  try{

  OracleConnectionPoolDataSourceocpds=newOracleConnectionPoolDataSource();

  ocpds.setURL(ConnectionURL);

  ocpds.setUser(UserID);

  ocpds.setPassword(PassWord);

  dbpool=ocpds.getPooledConnection();

  }

  catch(Exceptionex)

  {

  System.err.println("ErrorinPooledSQL-construct:");

  ex.printStackTrace(System.err);

  }

  }//endOraPooledSQL

  //closedbpool

  protectedvoidfinalize()

  {

  if(dbpool!=null)

  {

  try

  {

  dbpool.close();

  }

  catch(Exceptionex)

  {

  }

  }

  }

  /**

  用于更新、添加或删除的SQL语句

  @paramSQLSQL语句字串,如:insertintotablenamevalues(id,......)

  */

  publicintUpdate(StringSQL)

  {

  Statementstmt=null;

  intrc=0;

  Connectionconnection=null;

  try

  {

  connection=dbpool.getConnection();

  stmt=connection.createStatement();

  rc=stmt.executeUpdate(SQL);

  }

  catch(Exceptionex)

  {

  System.err.println("ErrorinUpdate-OraPooledSQL:");

  ex.printStackTrace(System.err);

  }

  returnrc;

  }//endUpdate()

  /**

  用于查询的SQL语句

  @paramSQLSQL语句字串,如:select*fromtablename

  */

  publicResultSetQuery(StringSQL)

  {

  Statementstmt=null;

  ResultSetrs=null;

  Connectionconnection=null;

  try

  {

  connection=dbpool.getConnection();

  stmt=connection.createStatement();

  rs=stmt.executeQuery(SQL);

  }

  catch(Exceptionex)

  {

  System.err.println("ErrorinQuery-SQLBean:");

  ex.printStackTrace(System.err);

  }

  returnrs;

  }//endQuery

  }//endClass

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>