java tomcat 6配置JNDI

<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;
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值