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