Resin 服务器配置Mysql数据库连接

来源:互联网 发布:混凝土销售软件 编辑:程序博客网 时间:2024/05/17 03:46

新建一个resin.conf文件,在里面添加如下xml配置信息:

  <resource-ref>
    <res-ref-name>jdbc/comstep</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <init-param driver-name="org.gjt.mm.mysql.Driver"/>
    <init-param url="jdbc:mysql://localhost/comstep"/>
    <init-param user="root"/>
    <init-param password="comstep"/>
    <init-param characterEncoding="gbk"/>
    <init-param useUnicode="true"/>
    <init-param serverVersion="Sql7"/>
    <init-param max-connections="5"/>
    <init-param max-idle-time="30"/>
  </resource-ref>
 
在初始化的servlet中,使用Jndi 对数据库资源绑定到对象:
 
     /*
     * 初始化需要的 jndi 节点。
     */

    try
    {
      Context env = (Context) new InitialContext().lookup("java:comp/env");
      env.createSubcontext("comstep");
    }
    catch (NamingException e)
    {
      System.out.println("NamingException");
      return;
    }
    /*
     * 初始化数据库接入点。
     */
    try
    {
      String resRef = getInitParameter("resource-ref");
    
      Context env = (Context) new InitialContext().lookup("java:comp/env");
      env.bind("comstep/DataConnection", dataConnection);
      System.out.println("DataConnection binded");
    }
    catch (Exception e)
    {
      System.out.println("DataConnection bind error");
      return;
    }
 
在Jsp中使用:
 
      Context env = (Context) new InitialContext().lookup("java:comp/env");
    dataConnection= (DataConnection)env.lookup("comstep/DataConnection");
 
 
[注]:DataConnection是自己写的一个类,这里没有列出。主要说一下jndi对数据库的绑定。
原创粉丝点击