编程语言

img pbMaster

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

发表于2004/9/28 19:48:00  1800人阅读

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

稍做修改:
1、拷贝C:/jboss-3.2.5/docs/examples/jca/mysql-ds.xml到C:/jboss-3.2.5/server/default/deploy下
2、拷贝C:/jboss-3.2.5/docs/examples/jms/mysql-jdbc2-service.xml到C:/jboss-3.2.5/server/default/deploy/jms下,并把hsqldb-jdbc2-service.xml改名或者删除
3、C:/jboss-3.2.5/server/default/deploy/jms/hsqldb-jdbc-state-service.xml中的DefaultDS改为MySqlDS
4、重启JBoss

测试连接池的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

阅读全文
0 0

相关文章推荐

img
取 消
img