JNDI创建数据库的详细步骤

来源:互联网 发布:软件体系结构经典书籍 编辑:程序博客网 时间:2024/06/05 05:33

配置JNDI连接池步骤:

1

tomcat/cof/context.xml<context>   </context>中配置如下

<!-- The contents of this file will be loaded for each web application -->

<Context>

 

    <!-- Default set of monitored resources -->

    <WatchedResource>WEB-INF/web.xml</WatchedResource>

        

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->

    <!--

    <Manager pathname=""/>

    -->       

 

<Resource name="mysql"

          auth="Container"

          type="javax.sql.DataSource"          factory="org.apache.commons.dbcp.BasicDataSourceFactory"

          driverClassName="com.mysql.jdbc.Driver"

          url="jdbc:mysql://localhost/phyllis?useUnicode=true&amp;characterEncoding=gb2312"

          username="root"

          password="root"

          maxActive="20"

          maxIdle="10"

          maxWait="-1"/>

</Context>     

 

2

 

在你的项目的web.xml文件中配置如下:

 

   <?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <servlet>

  <servlet-name>action</servlet-name>

  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

  <init-param>

   <param-name>config</param-name>

   <param-value>/WEB-INF/struts-config.xml</param-value>

  </init-param>

  <init-param>

   <param-name>debug</param-name>

   <param-value>3</param-value>

  </init-param>

  <init-param>

   <param-name>detail</param-name>

   <param-value>3</param-value>

  </init-param>

  <load-on-startup>0</load-on-startup>

 </servlet>

 <servlet-mapping>

  <servlet-name>action</servlet-name>

  <url-pattern>*.do</url-pattern>

 </servlet-mapping>

 <login-config>

  <auth-method>BASIC</auth-method>

 </login-config>

 <resource-ref>

  <description>shuju</description>

  <res-ref-name>mysql</res-ref-name>、、///这个名字在java文件中要用到,绑定context//(DataSource)context.lookup(“java:comp/env/mysql”);”java:comp/env”是固定的,mysql就是这里的res-ref-name在的值

  <res-type>javax.sql.DataSource</res-type>

  <res-auth>Container</res-auth>

 </resource-ref>

</web-app>

 

 

注意以上的文件中不可有多余的标记,想多个 >   < 等标记都会报错

 

 

3

 

import javax.sql.DataSource;

  import javax.naming.*;

  import java.sql.*;

public class DBConnection {

   private static DataSource ds=null;

   static { init();}

  

   public static void init(){

       try{

          Context context=new InitialContext();//create naming encironment

          if(context==null)

              throw new Exception (" not Context");

           ds=(DataSource)context.lookup("java:comp/env/mysql");

       }catch(Exception e){

          e.printStackTrace();

       }

   }

   public static Connection getConnection()throws SQLException{

       if(ds==null)

          throw new SQLException(" DataSource is null");

       else

          System.out.print("success return ds");

          return ds.getConnection();

   }

}

 

 

 

4

 

 

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Vector;

 

public class TestNaming {

 

  public static Vector getAll(){

      Connection con;

      Statement stmt;

      Vector vector=new Vector();

      try{

      con=DBConnection.getConnection();

      String sql="select id from member";

      stmt=con.createStatement();

      ResultSet rs=stmt.executeQuery(sql);

      while(rs.next()){

         vector.add(rs.getObject(1));

        

         //System.out.println(rs.getString(1)+"   "+rs.getString(2));

      }

     

  }catch(SQLException e){

      e.printStackTrace();

  }

    return vector;

 

}

}

 

注意这里的contextservlet中的类,所以要到web中发布,不能编写一个java static类测试

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 郑州买车怎么办暂住证 在青岛办暂住证怎么办 异地买车暂住证怎么办 广州暂住证过期了怎么办 杭州暂住证过期了怎么办 居住卡到期了怎么办 居住卡过期了怎么办 在合肥办暂住证怎么办 在西安办暂住证怎么办 杭州没办暂住证怎么办 暂住证超过2月怎么办 机动车大本丢了怎么办 北京老年卡2018怎么办 暂住卡过期了怎么办 我暂住证过期了怎么办 我没租房怎么办暂住证 孩子异地入学籍怎么办 孩子入不了学籍怎么办 常州没有暂住证怎么办牌照 东莞居住证过期了怎么办 杭州告知单过期怎么办 杭州暂住证到期了怎么办 在杭州租房怎么办暂住证 杭州暂住证丢了怎么办 在宾馆住怎么办暂住证 上海学车暂住证怎么办 环保车贴丢了怎么办 交通事故交警不处理怎么办 哈尔滨冰雪大世界夏天怎么办 下围棋没天赋怎么办 围棋计算力不好怎么办? 围棋3段后怎么办 遇到围棋中的断怎么办 手机出现等待中怎么办 小孩和同学打架怎么办 听到同学议论我怎么办 写小说对话枯燥怎么办 打架对面人多怎么办 雪纺衬衣透怎么办 合同到期不让走怎么办 澳洲rego到期了怎么办