首页 > 其他 > 详细

jndi的配置

时间:2014-10-20 21:16:10      阅读:273      评论:0      收藏:0      [点我收藏+]

tomcat的 配置,进入conf->context.xml

<Resource name="mysql"
    auth="Container" type="javax.sql.DataSource"
    driverClassName="com.mysql.jdbc.Driver"
    url="jdbc:mysql://localhost:3306/test"
    username="root"
    password="123456"
    maxActive="100"//最大活动数
    maxIdle="30"//最大的空闲
    maxWait="10000" //最大的等待时间
     />

 

web中调用

Context c=new InitialContext();
        DataSource ds=(DataSource)c.lookup("java:comp/env/mysql");
         out.println(ds.getConnection());

通过注记获取

@Resource(name="mysql",type=DataSource.class,authenticationType=AuthenticationType.CONTAINER)
public class Test extends HttpServlet {

    @Resource(name="mysql")
    private DataSource ds;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            Context c=new InitialContext();
            out.println(ds.getConnection().toString());
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        out.flush();
        out.close();
    }

 

jndi的配置

原文:http://www.cnblogs.com/danmao/p/4038572.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!