Posted on 2005-09-20 21:23
qiyadeng 阅读(762)
评论(1) 编辑 收藏 所属分类:
J2EE
我这里的配置分为以下几步:
第一步:从jboss-4.0.2\docs\examples\jca目录下的mysql-ds.xml复制文件到D:\jboss-4.0.2\server\default\deploy目录下并编辑如下:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/test</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-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
<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-4.0.2\server\default\conf下的三个文件:standardjbosscmp-jdbc.xml,standardjaws.xml,login-config.xml
standardjbosscmp-jdbc.xml修改部分:
<datasource>java:/MySqltDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
standardjaws.xml修改部分:
<datasource>java:/MySqltDS</datasource>
<type-mapping>mySQL</type-mapping>
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">root</module-option>
<module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MySqlDS</module-option>
</login-module>
</authentication>
</application-policy>
第三步:重启jboss没有出现异常,就会有如下显示,证明配置正确:
第四步:如果你还步放心,现在写个servlet试试吧:
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
try {
// Put your code here
Context cx = new InitialContext();
Object obj = cx.lookup("java:/MySqlDS");
DataSource ds = (DataSource)obj;
Connection connect = ds.getConnection();
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery("select * from customers");
while(rs.next()){
out.println(rs.getString("name"));
}
} catch (NamingException e) {
e.printStackTrace();
} catch(SQLException e1){
e1.printStackTrace();
}
out.flush();
out.close();