zoukankan      html  css  js  c++  java
  • Glassfish数据源配置详解

    本文环境:

    Win2003 + myeclipse6.01 + sqlserver2000(sp4) 

    1、 安装glassfish——启动,在管理控制台下配置如下

    首先配置连接池——大家把sqlserver的驱动包放在glassfish安装目录下的lib里。

    接着是配置JNDI(同时选中刚才配置好的连接池)

    2、测试

    常规方法测试1:在servlet中测试代码如下:

    try {

    Context ctx = new InitialContext();

    DataSource ds = (DataSource) ctx.lookup("myds");

    Connection conn = ds.getConnection();

    System.out.println(conn);

    ResultSet rs = conn.createStatement().executeQuery(

    "select * from admin ");

    while (rs.next()) {

    System.out.println(rs.getObject(2));

    System.out.println(rs.getObject(3));

    }

    } catch (Exception e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    常规方法测试2:在servlet中测试代码如下:

    public class HelloWorld extends HttpServlet {

        private @Resource(name="myds") DataSource nt;

        private @Resource(name="welcomeMessage") String welcomeMsg;

        protected void processRequest(HttpServletRequest request, HttpServletResponse response)

                throws ServletException, IOException {

            response.setContentType("text/html;charset=UTF-8");

            System.out.println("OK");

            PrintWriter out = response.getWriter();

            Connection conn = nt.getConnection();  

      }

  • 相关阅读:
    centos 7安装配置vsftpd
    lvs和haproxy机器必须注意的三个参数
    用python 脚本实现zabbix对java端口报警
    git的下载地址
    看的一篇很好的博客
    学习内容
    数组一些用法
    dom
    dom1
    for的基础
  • 原文地址:https://www.cnblogs.com/jymz/p/4293551.html
Copyright © 2011-2022 走看看