天天看點

Java使用Tomcat資料源的方式

1、  在tomcat中配置資料源,配置路徑是:e:\ucmsserver\tomcat\conf\server.xml,在如下位置添加:

Java使用Tomcat資料源的方式

資料源配置:

<resource name="jdbc/website"      

               type="javax.sql.datasource" 

               driverclassname="oracle.jdbc.driver.oracledriver"   

               url="jdbc:oracle:thin:@localhost:1521:orcl"   

               username="cmspro"    

               password="cmspro"  

               maxidle="10"  

               maxwait="10000"

               maxactive="350"

               removeabandoned="true"

               removeabandonedtimeout="180"

               logabandoned="true"

                               factory="org.apache.tomcat.dbcp.dbcp.basicdatasourcefactory" />

在tomcat\conf\catalina\localhost\下編寫wjdc.xml,内容如下:

<?xml version='1.0' encoding='utf-8'?>

<context displayname="wjdc" docbase="/wjdc" path="/wjdc" workdir="work/catalina/localhost/wjdc">

   <resourcelink name="jdbc/website" global="jdbc/website" type="javax.sql.datasource"/>

 </context>

如果想把項目不放置在tomcat中,需要做的操作是,可以類似:

<?xml version='1.0' encoding='utf-8'?>

<context displayname="wjdc" docbase="d:/ucmsserver/webapps/wjdc " path="/wjdc " workdir="work/catalina/localhost/wjdc ">

  <resourcelink name="jdbc/website" global="jdbc/website" type="javax.sql.datasource"/>

上面表示的是通路的工程是:d:/ucmsserver/webapps/wjdc,不是tomcat的webapps下的内容。

編寫資料源類:

package com.ucap.survey.utils;

import java.sql.connection;

import javax.naming.context;

import javax.naming.initialcontext;

import javax.sql.datasource;

/**

 * jdbcutils.java 資料源操作的工具類

 * @attention

 * @author toto

 * @date 2017-3-30

 * @note begin modify by 塗作權 2017-3-30 原始建立

 */

public final class jdbcutils {

         private static string datasourcename = null;

         /**

          * 獲得資料庫連接配接

          */

         public static connection getconnection() {

                   try {

                            context context = new initialcontext();

                            datasource datasource = (datasource) context.lookup("java:comp/env/" + getdatasourcename());

                            return datasource.getconnection();

                   } catch (exception e) {

                            e.printstacktrace();

                   }

                   return null;

         }

         public static string getdatasourcename() {

                   if (datasourcename == null) {

                            datasourcename = getpropertyfromfileutil.getproperty2("jdbc.properties", "datasourcename").trim();

                   return datasourcename;

}

資料庫操作是(ioptionstatisticsdao),代碼如下:

package com.ucap.survey.dao;

import java.util.list;

@suppresswarnings("unchecked")

public interface ioptionstatisticsdao {

   /**

    * 通過題目id,擷取選項資訊清單

    * @param opticid

    * @return

    * @attention方法的使用注意事項

    * @author toto

    * @date 2017-3-24

    * @note  begin modify by 塗作權,邱鵬飛 2017-3-24 原始建立

    */

   public list findoptionsname(string opticid);

    * 通過題目的id擷取每道題目錄的統計資訊

    * @param opticid       :題目id

    * @attention

    *

    * @note  begin modify by 塗作權,邱鵬飛   2017-3-24   原始建立

   public list findoptionvotenum(string opticid);

    * 擷取目前題目的總的投票數量

    * @param surveyid             :問卷id

    * @note  begin modify by 塗作權,邱鵬飛   2017-3-24 原始建立

   public integer findvotetotalnum(string surveyid);

代碼實作是(optionstatisticsdaoimpl):

package com.ucap.survey.dao.impl;

import java.sql.preparedstatement;

import java.sql.resultset;

import java.sql.sqlexception;

import java.util.arraylist;

import com.ucap.survey.bean.optioninfobean;

import com.ucap.survey.dao.ioptionstatisticsdao;

import com.ucap.survey.exception.daoexception;

import com.ucap.survey.utils.jdbcutils;

 * commentdao.java 獲得問卷的統計結果

 * @date 2017-3-24

 * @note begin modify by 塗作權  2017-3-24 原始建立

public class optionstatisticsdaoimpl implements ioptionstatisticsdao {

          * 通過題目id,擷取選項資訊清單

          * @param opticid

          * @return

          * @attention 方法的使用注意事項

          * @author toto

          * @date 2017-3-24

          * @note  begin modify by 塗作權,邱鵬飛 2017-3-24 原始建立

         public list<optioninfobean> findoptionsname(string opticid) {

                   try { 

                            connection conn = jdbcutils.getconnection();

                            string sql =  "select co.option_id optionid,co.option_content optionname " +

                                               "from cms_option co " +

                                               "where co.optic_id=? " +

                                               "and co.is_del = 0";

                            preparedstatement ps = conn.preparestatement(sql);

                            ps.setstring(1, opticid);

                            resultset rs = ps.executequery();

                            list<optioninfobean> list = new arraylist<optioninfobean>();

                            while (rs.next()) {

                                     optioninfobean infobean = new optioninfobean();

                                     string optionid = rs.getstring("optionid");

                                     string optionname = rs.getstring("optionname");

                                     infobean.setoptionid(optionid);

                                     infobean.setoptionname(optionname);

                                     list.add(infobean);

                            }

                            rs.close();

                            ps.close();

                            conn.close();

                            return list;

                   } catch (sqlexception e) {

                            throw new daoexception(e);

          * 通過題目的id擷取每道題目錄的統計資訊

          * @param opticid       :題目id

          * @attention

          *

          * @note  begin modify by 塗作權,邱鵬飛   2017-3-24   原始建立

         public list<optioninfobean> findoptionvotenum(string opticid) {

                            string sql =  "select count(t.option_id) votenum,t.option_id optionid from cms_voteresult t " +

                                                          "     where t.optic_id= ? " +

                                                          "   group by t.option_id";

                                     integer votenum = rs.getint("votenum");

                                     infobean.setvotenum(votenum);

          * 擷取目前問卷投票次數

          * @param surveyid         :問卷id

          * @note  begin modify by 塗作權,邱鵬飛   2017-3-24 原始建立

    public integer findvotetotalnum(string surveyid) {

                            string sql = "select count(t.vote_survey_id) totalnum from cms_vote_survey t where t.survey_id= ?";

                            ps.setstring(1, surveyid);

                            integer totalnum = 0;

                                     totalnum = rs.getint("totalnum");

                                     break;

                            return totalnum;

jdbc.properties的内容如下:

##配置資料源名稱,注意這裡的内容和資料源配置的xml中配置的内容是一樣的

datasourcename=jdbc/website

getpropertyfromfileutil的代碼如下:

import java.io.ioexception;

import java.io.inputstream;

import java.util.enumeration;

import java.util.linkedhashmap;

import java.util.map;

import java.util.properties;

import com.ucap.survey.exception.getpropertyfromfileexception;

 * @author <a href="mailto:[email protected]">塗作權</a>

 *

 * @version 1.2 2012-4-4

 * @mobileshop2

public final class getpropertyfromfileutil {

          * <p>此方法用于擷取指定檔案中指定屬性的整型值<p>

         public static int getproperty(string filename,string property) {    

                   //傳回dbtype,并傳回整型的資料

                   return integer.parseint(operate(filename,property));

          * <p>此方法用于擷取指定檔案中指定屬性的string值<p>

         public static string getproperty2(string filename,string property) {

                   return operate(filename,property);

          * @since version 1.2

          * @param filename:表示要獲得那個檔案中的資料

          * @param property:表示要獲得的是那個檔案的值

          * @return string型的屬性的值

         public static string operate(string filename,string property) {

                   /*

                   * 獲得輸入流

                    */

                   inputstream inputstream = getpropertyfromfileutil.class.getclassloader().getresourceasstream(filename);

                   properties prop = new properties();

                            prop.load(inputstream);

                            throw new getpropertyfromfileexception(e);

                   } finally {

                            if (inputstream != null) {

                                     try {

                                               inputstream.close();

                                     } catch (ioexception e) { 

                                               throw new getpropertyfromfileexception(e);

                                     }

                            inputstream = null;

                   return prop.getproperty(property);

          * 獲得屬性檔案中的所有參數的集合

         @deprecated

         public static enumeration<object> getproperties(string filename) {

                    * 獲得輸入流

                   inputstream inputstream = getpropertyfromfileutil.class.getclassloader().getresourceasstream(filename);

                            throw new getpropertyfromfileexception("getpropertyutilfromfileutil  prop.load步出錯了!!");

                                               throw new getpropertyfromfileexception("getpropertyutilfromfileutil 關閉inputstream時出錯了!");

                   return (enumeration<object>) prop.propertynames();

          * 通過檔案名和執行個體對象獲得所有的字段名稱

          * @param filename

          * @param clazz

         public static map<string,string> gettableinfofromfile(string filename,class clazz) {

                   map<string,string> tablefields = new linkedhashmap<string,string>();

                   //獲得表名稱

                  string tablename = clazz.getsimplename().tostring();

                   enumeration<object> properties = getproperties(filename);

                   while (properties.hasmoreelements()) {

                            //獲得屬性檔案中的key值

                            string key = properties.nextelement().tostring();

                            //如果key值是以表名稱開頭的,表示這些key對應的value全是這個表中的字段名稱

                            if (key.startswith(tablename) && !key.equals(tablename)) {

                                     string value = getpropertyfromfileutil.getproperty2(filename, key);

                                     tablefields.put(key, value);

                   return tablefields;

         public static string gettablename(string filename,class clazz) {

                   //獲得在配置檔案中的表名名稱對應的key

                   string tablekey = clazz.getsimplename().tostring();

                   //獲得tablememo.properties中的表名稱對應key值之後,通過這個key值獲得這個表名稱

                   return getproperty2(filename, tablekey);