网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Java编程 >> 文章正文
  初始化数据库连接类(DB.java)            【字体:
初始化数据库连接类(DB.java)
作者:佚名    文章来源:不详    点击数:    更新时间:2007-7-8    
正在装载数据……

package org.lzh.struts;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DB {
 /**connect*/
 Connection connect = null;
 /**结果集*/
 ResultSet rs = null;
 
 public DB(DataSource dataSource) { 
  try {
   /*通过数据源,初始化连接池*/
   connect = dataSource.getConnection();
  }
  catch(SQLException e) {
   e.printStackTrace();
  }
 }
 
 public ResultSet OpenSql(String sql) {
  try {
   /**
    * ResultSet.TYPE_SCROLL_INSENSITIVE
    * The constant indicating the type for a ResultSet object
    * that is scrollable but generally not sensitive to changes made by others.
    */
   /**
    * ResultSet.CONCUR_READ_ONLY
    * The constant indicating the concurrency mode for a ResultSet object that may NOT be updated.
    */
   Statement stmt = connect.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
   rs = stmt.executeQuery(sql);
  }
  catch(SQLException ex) {
   ex.printStackTrace();
  }
  return rs;
 }
 
 public int ExecSql(String sql) {
  int result = 0;
  try {
   Statement stmt = connect.createStatement();
   result = stmt.executeUpdate(sql);
  }
  catch(SQLException ex) {
   System.err.println(ex.getMessage());
   
  }
  return result;
 }
 
 /**
  * 关闭数据库连接池
  *
  */
 public void close(){
  if(connect!=null){
   try{
    connect.close();
    connect = null; 
   }catch(SQLException ex) {
    System.err.println(ex.getMessage());
   }
  } 
 }
}

//数据库连接池的配置
<DATA-SOURCES>
    <DATA-SOURCE type="org.apache.commons.dbcp.BasicDataSource" key="dataSource">
      <SET-PROPERTY value="root" property="password" />
      <SET-PROPERTY value="3" property="minCount" />
      <SET-PROPERTY value="10" property="maxCount" />
      <SET-PROPERTY value="root" property="username" />
      <SET-PROPERTY value="com.mysql.jdbc.Driver" property="driverClassName" />
      <SET-PROPERTY value="strutsbbs" property="description" />
      <SET-PROPERTY value="jdbc:mysql://localhost:3306/strutsbbs" property="url" />
      <SET-PROPERTY value="false" property="readOnly" />
      <SET-PROPERTY value="true" property="autoCommit" />
    </DATA-SOURCE>
  </DATA-SOURCES> 




本文来源:http://blog.csdn.net/nickshen3/archive/2007/06/17/1655826.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     用java实现web服务器
     用java快速开发linux gu…
     正则表达式分解siemens交…
     [portal参考手册]目录
     jsp中调用oracle存储过程…
  • 开发手记---JAVA数据库连接池

  • 解决JSP数据输入中文乱码问题

  • ava与数据库连接的四种方法

  • javabean+mysql数据库连接池

  • 关于C++类成员的初始化

  • 静态成员,友元,共享数据保…

  • VB.NET的数据库基础编程

  • 手把手教你做一个简单的VB数…

  • Delphi和C++数据类型对照表

  • 在delphi7下实现省市县的三级…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David