(转)jboss5.1 mysql jndi的配置
1.将mysql的JDBC驱动放到的JBOSS_HOME\server\default\lib 下
2.把JBOSS_HOME\docs\examples\jca\mysql-ds.xml 复制到JBOSS_HOME\server\default\deploy目录下。修改mysql- ds.xml文件例:
<datasources>
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost :3306/databaseName ?characterEncoding=UTF-8
</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
</local-tx-datasource>
</datasources>此步骤需要注意的地方是jca目录下存在mssql-ds.xml这个文件,注意区分它与mysql-ds.xml
3.修改JBOSS_HOME\server\default\conf\standardjbosscmp-jdbc.xml
<jbosscmp-jdbc>
<defaults>
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySql</datasource-mapping>
</defaults></jbosscmp-jdbc>
说明:<datasource-mapping>mySql</datasource-mapping>的JBoss 5.0.1GA中是被注释掉了,需要将注释标记去掉4.最后再修改JBOSS_HOME\server\default\conf\login-config.xml:
将下面代码添加到<policy>中
<application-policyname="MySqlDbRealm">
<authentication>
<login-modulecode="org.jboss.resource.security.ConfiguredIdentityLoginModule"flag="required">
<module-optionname="principal">databaseName</module-option>
<module-optionname="userName">root</module-option>
<module-optionname="password">root</module-option>
<module-optionname="managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MySqlDS
</module-option>
</login-module>
</authentication>
</application-policy>5、测试代码
- import java.sql.*;
- import javax.naming.*;
- import javax.sql.DataSource;
- //一个用于查找数据源的工具类。
- public class DatabaseConn {
- public static synchronized Connection getConnection() throws
- Exception
- {
- try
- {
- Context ctx = new InitialContext(); //得到初始化上
- 下文
- Object obj = ctx.lookup("java:/MySqlDS");//查找连接池
- DataSource ds = (DataSource) obj;//转换成DataSource
- return ds.getConnection();
- }
- catch(SQLException e)
- {
- throw e;
- }
- catch(NamingException e)
- {
- throw e;
- }
- }
- }
- //showdata.jsp
- <%@ page contentType="text/html; charset=gb2312" %>
- <%@ page import="java.sql.*"%>
- <%@ page import="DataConn.*"%>
- <%
- Connection conn = DatabaseConn.getConnection();
- Statement stmt=conn.createStatement();
- ResultSet rs=stmt.executeQuery("select * from employee");
- while(rs.next())
- {
- out.println(rs.getInt("id"));
- out.println(rs.getString("name"));
- out.println(rs.getString("salary"));
- out.println(rs.getString("department"));
- out.println(rs.getInt("age")+"<br>");
- }
- conn.close();
- stmt.close();
- rs.close();
- %>