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网站
4:在 jboss-4.0.4\server\default\deploy下新建文件mysql-ds.xml
可从jboss-4.0.4\docs\examples\jca copy 修改
想配置多个连接池只要多加一个
其中内容如下:
test
jdbc:mysql://127.0.0.1:3306/test
com.mysql.jdbc.Driver
root
xxxxxxxx
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
mySQL
5:修改jboss-4.0.4\server\default\conf\ standardjaws.xml
java:/test
mySql
.....
修改jboss-4.0.4\server\default\conf\ standardjbosscmp-jdbc.xml
java:/test
mySql
修改jboss-4.0.4\server\default\conf\ login-config.xml
"org.jboss.resource.security.ConfiguredIdentityLoginModule"
flag = "required">
test
root
xxxxxxxx
jboss.jca:service=LocalTxCM,name=test
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.*"%>
My JSP 'MyJsp.jsp' starting page <%
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);
%>
8 :部署Web project
9:重新启动
10:访问:
【责编:Fanvei】
--------------------next---------------------