服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

jboss配置 mysql数据库连接池


jboss配置 mysql数据库连接池实例
1 :配置:
   jdk 1.5
   jboss4.0.4
   mysql5.0
   myeclipse 4.1
2:  建立数据库:
 create database test;
use test;
drop table if exists `test`;
create table `test` (
  `test_id` int(11) not null auto_increment,
  `test_name` varchar(45) not null default '',
  `test_password` varchar(45) not null default '',
  primary key  (`test_id`)
) engine=innodb default charset=latin1;
insert into `test` values (1,'test','test'),(2,'test2','test');
3:copy mysql的jdbc驱动放到jboss-4.0.4/server/default/lib
  可到mysql网站下载www.mysql.com
4:在 jboss-4.0.4/server/default/deploy下新建文件mysql-ds.xml
   可从jboss-4.0.4/docs/examples/jca copy 修改
   想配置多个连接池只要多加一个<local-tx-datasource></local-tx-datasource>
   其中内容如下:
  <?xml version="1.0" encoding="utf-8"?>

<!-- $id: mysql-ds.xml,v 1.3.2.3 2006/02/07 14:23:00 acoliver exp $ -->
<!--  datasource config for mysql using 3.0.9 available from:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
  <local-tx-datasource>
    <jndi-name>test</jndi-name>
    <connection-url>jdbc:mysql://127.0.0.1:3306/test</connection-url><!?test为数据库名-->

    <driver-class>com.mysql.jdbc.driver</driver-class>
    <user-name>root</user-name><!?用户名以下相同-->
    <password>xxxxxxxx</password><!?密码以下相同-->

    <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.mysqlexceptionsorter</exception-sorter-class-name>
    <!-- should only be used on drivers after 3.22.1 with "ping" support
    <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.mysqlvalidconnectionchecker</valid-connection-checker-class-name>
    -->
    <!-- sql to call when connection is created
    <new-connection-sql>some arbitrary sql</new-connection-sql>
      -->
    <!-- sql to call on an existing pooled connection when it is obtained from pool - mysqlvalidconnectionchecker is preferred for newer drivers
    <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql>
      -->

    <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
    <metadata>
       <type-mapping>mysql</type-mapping>
    </metadata>
  </local-tx-datasource>
</datasources>

5:修改jboss-4.0.4/server/default/conf/ standardjaws.xml
<jaws>
<datasource>java:/test</datasource> 
<type-mapping>mysql</type-mapping> 
.....
</jaws>
修改jboss-4.0.4/server/default/conf/ standardjbosscmp-jdbc.xml
<jbosscmp-jdbc> 
<defaults> 
<datasource>java:/test</datasource>
<datasource-mapping>mysql</datasource-mapping>
</defaults>
</jbosscmp-jdbc>
修改jboss-4.0.4/server/default/conf/ login-config.xml
<application-policy name = "mysqldbrealm"> 

<authentication> 

<login-module code = 

"org.jboss.resource.security.configuredidentityloginmodule" 

flag = "required"> 

<module-option name ="principal">test</module-option> 

<module-option name ="username">root</module-option> 

<module-option name ="password">xxxxxxxx</module-option> 

<module-option name ="managedconnectionfactoryname"> 

jboss.jca:service=localtxcm,name=test 

</module-option> 

</login-module> 

</authentication> 

</application-policy>
6:myeclispe 新建web  project 命名为:usetest
新建java类databaseconn.java
package com.db;

import java.sql.*;

import javax.naming.*;
import javax.sql.datasource;

public class databaseconn {

        public static synchronized connection getconnection() {
            try {
                context envctx = new initialcontext(); 
                datasource ds = (datasource) envctx.lookup("java:/test");
                return ds.getconnection();
            } catch (sqlexception e) {
                system.out.println("数据源配置发生错误" + e.tostring());
                return null;
            } catch (namingexception e2) {
                system.out.print("数据源配置" + e2.tostring());
                return null;
            }

        }

    

    public static void close(resultset rs, statement st, connection conn) {
        try {
            if (rs != null)
                rs.close();
        } catch (sqlexception ex) {
        }
        ;

        try {
            if (st != null)
                st.close();
        } catch (sqlexception ex) {
        }
        ;

        try {
            if (conn != null)
                conn.close();
        } catch (sqlexception ex) {
        }
        ;
    }
}
7:新建jsp页面:myjsp.jsp
<%@ page language="java" import="java.util.*" pageencoding="gb2312"%>
<%@ page import="java.sql.*"%> 
<%@ page import="com.db.*"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
  <head>
    
    <title>my jsp 'myjsp.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    <% 
connection conn = databaseconn.getconnection();
statement stmt=conn.createstatement();
resultset rs=stmt.executequery("select * from test");
while(rs.next())
{
out.println(rs.getint("test_id"));
out.println(rs.getstring("test_name"));
out.println(rs.getstring("test_password"));

}
databaseconn.close(rs,stmt,conn);
%> 
  </body>
</html>
8 :部署web project
9:重新启动服务器
10:访问: http://127.0.0.1:8080/usetest/myjsp.jsp

扫描关注微信公众号