zoukankan      html  css  js  c++  java
  • JBOSS管理数据库连接

    一、普通JDBC方式:

        static final String DB_DRIVER_CLASS = "com.mysql.jdbc.Driver";
    static final String DB_URL =
    "mysql://localhost:3306/JBossatWorkDB?autoReconnect=true";

    Connection connection = null;

    try {
    // Load the Driver.
    Class.forName(DB_DRIVER_CLASS).newInstance( );

    // Connect to the database.
    connection = DriverManager.getConnection(DB_URL);

    } catch (SQLException se) {
    ...
    } catch (...) {
    ...
    }

    二、通过JBOSS管理
    1、配置文件jaw-ds.xml
    <datasources>
    <local-tx-datasource>
    <jndi-name>JBossAtWorkDS</jndi-name>
    <connection-url>jdbc:hsqldb:hsql://localhost:1701</connection-url>
    <driver-class>org.hsqldb.jdbcDriver</driver-class>
    <user-name>sa</user-name>
    <password></password>
    <min-pool-size>5</min-pool-size>
    <max-pool-size>20</max-pool-size>
    <idle-timeout-minutes>0</idle-timeout-minutes>
    <track-statements/>
    <metadata>
    <type-mapping>Hypersonic SQL</type-mapping>
    </metadata>
    <depends>jboss:service=Hypersonic-JAW,database=jawdb</depends>
    </local-tx-datasource>
    .......

    2、JNDI查寻器ServiceLocator.java:
    package com.jbossatwork.util;

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

    public class ServiceLocator {
    private ServiceLocator( ) { }

    public static DataSource getDataSource(String dataSourceJndiName)
    throws ServiceLocatorException {

    DataSource dataSource = null;
    try {
    Context ctx = new InitialContext( );
    dataSource = (DataSource) ctx.lookup(dataSourceJndiName);

    } catch (ClassCastException cce) {
    throw new ServiceLocatorException(cce);
    } catch (NamingException ne) {
    throw new ServiceLocatorException(ne);
    }
    return dataSource;
    }
    }

    3、通过JBOSS连接:

    static final String DATA_SOURCE= "java:comp/env/jdbc/JBossAtWorkDS"; DataSource dataSource = null; Connection conn = null; try { // Load the Driver. dataSource = ServiceLocator.getDataSource(DATA_SOURCE); // Connect to the database. conn = dataSource.getConnection( ); } catch (SQLException se) { ... } catch (ServiceLocatorException sle) { ... }

    
    
  • 相关阅读:
    692. 前K个高频单词
    准备工作:更新代码和运行环境
    1319. 连通网络的操作次数——并查集
    <leetcode c++>25. K 个一组翻转链表
    织梦dedecms手机站关闭自动生成首页index.html
    IIS7 IIS7.5 伪静态 web.config 配置方法不带WWW的301跳转到带WWW
    win7和xp一样有左下角显示桌面快捷方式
    Win7系统传真与扫描功能无法使用的处理方法
    织梦dedecms将列表页重复的第一页去除的方法
    秦岭土蜂蜜价格 秦岭土蜂蜜多少钱一斤
  • 原文地址:https://www.cnblogs.com/beta2013/p/3377369.html
Copyright © 2011-2022 走看看