<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">配置MYSQL 的JNDI采取的方式时再META-INF文件夹下创建context.xml配置应用的jndi。优点每个应用独立JNDI</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">。</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">配置文件及其步骤如下:</span></span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">1、META-INF中创建</span></span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">context.xml内容如下: </span>
<?xml version="1.0" encoding="UTF-8"?>
<Context>
name="jdbc/mysql_default"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://127.0.0.1:3306/ccc?useUnicode=true&characterEncoding=utf-8"/>
</Context>
2、WEB-INF中web.xml文件中添加引用:
<!--Oracle数据库JNDI数据源引用 -->
<!--<resource-ref>-->
<!--<description>Oracle DB Connection</description>-->
<!--<res-ref-name>oracleDataSource</res-ref-name>-->
<!--<res-type>javax.sql.DataSource</res-type>-->
<!--<res-auth>Container</res-auth>-->
<!--</resource-ref>-->
<!--MySQL数据库JNDI数据源引用 -->
<resource-ref>
<description>MySQL DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<!--SQLServer数据库JNDI数据源引用 -->
<!--<resource-ref>-->
<!--<description>SQLServer DB Connection</description>-->
<!--<res-ref-name>sqlserverDataSource</res-ref-name>-->
<!--<res-type>javax.sql.DataSource</res-type>-->
<!--<res-auth>Container</res-auth>-->
<!--</resource-ref>-->
3、DAO(一个servlet或者专门的数据接入类)中读取配置及其创建connection和DataSource
import sun.jdbc.odbc.ee.ConnectionPool;
import javax.naming.*;
import javax.sql.DataSource;
import java.sql.*;
import java.sql.*;
import java.sql.SQLException;
import java.util.Hashtable;
/**
* Created by wuxituong on 2016-10-01.
*/
public class SQLDAO {
private static ConnectionPool instace;
private static DataSource ds;
//获得数据源
private static <span style="font-family: Arial, Helvetica, sans-serif;">DataSource </span><span style="font-family: Arial, Helvetica, sans-serif;">createDataSource()</span>
{
if (ds == null)
{
System.out.println("ds created");
try
{
Context ct = new InitialContext();
if (ct == null)
System.out.println("无配置环境");
Context envContext = (Context) ct.lookup("java:/comp/env");
ds = (<span style="font-family: Arial, Helvetica, sans-serif;">DataSource </span><span style="font-family: Arial, Helvetica, sans-serif;">) envContext.lookup("jdbc/mysql"); //根据名称取得数据源</span>
}
catch (NamingException e)
{
e.printStackTrace();
}
}
return ds;
}
//从连接池中取得连接对象
public static synchronized Connection getConnection() throws SQLException,NamingException{
Connection con=null;
try
{
System.out.print("正准备获取con");
//获取连接
con=(Connection)createDataSource().getConnection();
}
catch (Exception e)
{
e.printStackTrace();
System.out.print("获取连接失败!");
}
return con;
}
//释放连接
public static synchronized void freeConnection(Connection con){
try
{
con.close();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("关闭连接失败!");
}
}
}
4、使用DAO 记得在使用了con一定要con.close()将连接放回连接池,否则则会造成连接被占用完成,无法再获取con。导致服务器必须重启释放con
public ResultSet getResultSetData(String sql){
ResultSet rs= null;
try {
Statement sta = con.createStatement();
rs = sta.executeQuery(sql);
return rs;
}catch (Exception e){
System.out.println(e.getMessage());
return rs;
}
}
public int exeSql(String sql){
try {
Statement sta = con.createStatement();
return sta.executeUpdate(sql);
}catch (Exception e){
System.out.println(e.getMessage());
return -1;
}
}