JBOSS3.2.5中MYSQL数据库连接池的建立与测试

类别:Java 点击:0 评论:0 推荐:

数据库连接池的配置参照 samlei 的文档《切换JBOSS默认数据库到MySQL》 
http://dev.csdn.net/develop/article/33/33406.shtm

测试连接池的JSP页面:
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="java.sql.*"%>

<%
    Context ctx = new InitialContext();  //得到初始化上下文

    Object obj = ctx.lookup("java:/MySqlDS");//查找连接池
    DataSource ds = (DataSource) obj;//转换成DataSource

    try {
     String temp;
      Connection connect = ds.getConnection();//从连接池中得到一个连接
      System.out.println("Success connect Mysql Connection Pool!");

      Statement stmt = connect.createStatement();
      ResultSet rs = stmt.executeQuery("select * from pet");
      while (rs.next()) {
       temp=rs.getString("name");
%>

<html>
 <head>
   <title>Welcome</title>
 </head>
 <body>
  <center>name is : <%=temp%></center>
  
 </body>
</html>
<%
      }
    }
    catch (Exception e) {
      System.out.print("get data error!");
      e.printStackTrace();
    }
%>

页面返回结果:
name is : catmiw

本文地址:http://com.8s8s.com/it/it15099.htm