• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
thankgoodness
博客园    首页    新随笔    联系   管理    订阅  订阅

Tomcat 5.5 配置数据源

1。配置数据源
配置文件:
<Context path="/apple" docBase="D:\workspace\bolg\webdoc"
         privileged="true" antiResourceLocking="false" antiJARLocking="false">
 <Resource name="jdbc/blogdb" auth="Container"
    type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000"
    username="root" password=""
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/blogdb?autoReconnect=true"/>
</Context>

调用方式:
 Context ctx = null ;
 Connection conn = null;
 Statement stmt = null ;
 ResultSet rs = null;
 try{
  ctx = new InitialContext();
  if( ctx == null) throw new Exception("没有匹配的环境!");
  DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/blogdb");
  if( ds == null ) throw new Exception("没有匹配的数据库!");
  conn = ds.getConnection();
  stmt = conn.createStatement();
  rs = stmt.executeQuery(" select * from tbl_blog");
  while(rs.next()){
   out.print(rs.getString(2));
  }
  
 }catch(Exception e){
  e.printStackTrace();
 }finally{
  if(rs!=null) rs.close();
  if(stmt!=null) stmt.close();
  if(conn!=null) conn.close();
  if(ctx!=null) ctx.close();
 }
2。
posted @ 2008-05-28 08:59  宇晨  阅读(388)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3