重新探索自我

    客观条件受制于人,并不足惧。重要的是,我们拥有选择的自由,可以对现实环境积极回应,
    为生命负责,为自己创造有利的机会,做一个“真正”操之在我的人!

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  3 随笔 :: 10 文章 :: 5 评论 :: 0 Trackbacks


配置文件:
<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();
 }



posted on 2006-10-17 12:32 蒋利文 阅读(991) 评论(0)  编辑  收藏 所属分类: J2EE开发

只有注册用户登录后才能发表评论。


网站导航: