jboss配置 mysql數據庫連接池實例
:配置:
JDK
JBoss
Mysql
Myeclipse
: 建立數據庫:
create database test;
use test;
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`Test_id` int(
) NOT NULL auto_increment
`Test_name` varchar(
) NOT NULL default
`Test_password` varchar(
) NOT NULL default
PRIMARY KEY (`Test_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin
;
INSERT INTO `test` VALUES (
test
test
)
(
test
test
);
:Copy MySQL的JDBC驅動放到jboss
\server\default\lib
可到MYsql網站下載
:在 jboss
\server\default\deploy下新建文件mysql
ds
xml
可從jboss
\docs\examples\jca copy 修改
想配置多個連接池只要多加一個<local
tx
datasource></local
tx
datasource>
其中內容如下:
<?xml version=
encoding=
UTF
?>
<!
$Id: mysql
ds
xml
v
/
/
:
:
acoliver Exp $
>
<!
Datasource config for MySQL using
available from:
l
>
<datasources>
<local
tx
datasource>
<jndi
name>test</jndi
name>
<connection
url>jdbc:mysql://
:
/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
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>
:修改jboss
\server\default\conf\ standardjaws
xml
<jaws>
<datasource>java:/test</datasource>
<type
mapping>mySql</type
mapping>
</jaws>
修改jboss
\server\default\conf\ standardjbosscmp
jdbc
xml
<jbosscmp
jdbc>
<defaults>
<datasource>java:/test</datasource>
<datasource
mapping>mySql</datasource
mapping>
</defaults>
</jbosscmp
jdbc>
修改jboss
\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>
: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 e
) {
System
out
print(
數據源配置
+ e
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) {
}
;
}
}
:新建JSP頁面:MyJsp
jsp
<%@ page language=
java
import=
java
util
*
pageEncoding=
GB
%>
<%@ page import=
java
sql
*
%>
<%@ page import=
com
db
*
%>
<!DOCTYPE HTML PUBLIC
//W
C//DTD HTML
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=
>
<meta http
equiv=
keywords
content=
keyword
keyword
keyword
>
<meta http
equiv=
description
content=
This is my page
>
<!
<link rel=
stylesheet
type=
text/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>
:部署Web project
:重新啟動服務器
:訪問:
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28391.html