日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java使用Tomcat数据源的方式

發布時間:2024/9/27 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java使用Tomcat数据源的方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、? 在tomcat中配置數據源,配置路徑是:E:\UCMSServer\tomcat\conf\server.xml,在如下位置添加:

數據源配置:

<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"/>

?</Context>

上面表示的是訪問的工程是:D:/UCMSServer/webapps/wjdc,不是tomcatwebapps下的內容。

?

編寫數據源類:

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

?? ?* @return

?? ?* @attention

?? ?*

?? ?* @author toto

?? ?* @date 2017-3-24

?? ?* @note? begin modify by 涂作權,邱鵬飛?? 2017-3-24?? 原始創建

?? ?*/

?? public List findOptionVoteNum(String opticId);

??

?? /**

?? ?* 獲取當前題目的總的投票數量

?? ?* @param surveyId???????????? :問卷id

?? ?* @return

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

?? ?* @author toto

?? ?* @date 2017-3-24

?? ?* @note? begin modify by 涂作權,邱鵬飛?? 2017-3-24 原始創建

?? ?*/

?? public Integer findVoteTotalNum(String surveyId);

??

}

?

代碼實現是(OptionStatisticsDaoImpl):

package com.ucap.survey.dao.impl;

?

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

?

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 獲得問卷的統計結果

?* @attention

?* @author toto

?* @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

???????? ?* @return

???????? ?* @attention

???????? ?*

???????? ?* @author toto

???????? ?* @date 2017-3-24

???????? ?* @note? begin modify by 涂作權,邱鵬飛?? 2017-3-24?? 原始創建

???????? ?*/

???????? public List<OptionInfoBean> findOptionVoteNum(String opticId) {

??????????????????

?????????????????? try {

??????????????????????????? Connection conn = JdbcUtils.getConnection();

???????????????????????????

??????????????????????????? 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";

???????????????????????????

??????????????????????????? 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");

???????????????????????????????????? Integer voteNum = rs.getInt("voteNum");

????????????????????????????????????

???????????????????????????????????? infoBean.setOptionId(optionId);

???????????????????????????????????? infoBean.setVoteNum(voteNum);

???????????????????????????????????? list.add(infoBean);

??????????????????????????? }

???????????????????????????

??????????????????????????? rs.close();

??????????????????????????? ps.close();

??????????????????????????? conn.close();

???????????????????????????

??????????????????????????? return list;

?????????????????? } catch (SQLException e) {

??????????????????????????? throw new DaoException(e);

?????????????????? }

???????? }

????????

???????? /**

???????? ?* 獲取當前問卷投票次數

???????? ?* @param surveyId???????? :問卷id

???????? ?* @return

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

???????? ?* @author toto

???????? ?* @date 2017-3-24

???????? ?* @note? begin modify by 涂作權,邱鵬飛?? 2017-3-24 原始創建

???????? ?*/

??? public Integer findVoteTotalNum(String surveyId) {

??????????????????

?????????????????? try {

??????????????????????????? Connection conn = JdbcUtils.getConnection();

???????????????????????????

??????????????????????????? String sql = "select count(t.vote_survey_id) totalNum from CMS_VOTE_SURVEY t where t.survey_id= ?";

???????????????????????????

??????????????????????????? PreparedStatement ps = conn.prepareStatement(sql);

??????????????????????????? ps.setString(1, surveyId);

???????????????????????????

??????????????????????????? ResultSet rs = ps.executeQuery();

???????????????????????????

??????????????????????????? Integer totalNum = 0;

??????????????????????????? while (rs.next()) {

???????????????????????????????????? totalNum = rs.getInt("totalNum");

???????????????????????????????????? break;

??????????????????????????? }

???????????????????????????

??????????????????????????? rs.close();

??????????????????????????? ps.close();

??????????????????????????? conn.close();

???????????????????????????

??????????????????????????? return totalNum;

?????????????????? } catch (SQLException e) {

??????????????????????????? throw new DaoException(e);

?????????????????? }

???????? }

}

?

jdbc.properties的內容如下:

##配置數據源名稱,注意這里的內容和數據源配置的xml中配置的內容是一樣的

dataSourceName=jdbc/website

?

GetPropertyFromFileUtil的代碼如下:

package com.ucap.survey.utils;

?

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:1032610746@qq.com">涂作權</a>

?*

?* @version 1.2 2012-4-4

?* @mobileshop2

?*/

@SuppressWarnings("unchecked")

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();

??????????????????

?????????????????? try {

??????????????????????????? prop.load(inputStream);

?????????????????? } catch (Exception e) {

??????????????????????????? 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);

?????????????????? Properties prop = new Properties();

??????????????????

?????????????????? try {

??????????????????????????? prop.load(inputStream);

?????????????????? } catch (Exception e) {

??????????????????????????? throw new GetPropertyFromFileException("GetPropertyUtilFromFileUtil? prop.load步出錯了!!");

?????????????????? } finally {

??????????????????????????? if (inputStream != null) {

???????????????????????????????????? try {

?????????????????????????????????????????????? inputStream.close();

???????????????????????????????????? } catch (IOException e) {?

?????????????????????????????????????????????? throw new GetPropertyFromFileException("GetPropertyUtilFromFileUtil 關閉InputStream時出錯了!");

???????????????????????????????????? }

??????????????????????????? }

??????????????????????????? inputStream = null;

?????????????????? }

?????????????????? return (Enumeration<Object>) prop.propertyNames();

???????? }

????????

???????? /**

???????? ?* 通過文件名和實例對象獲得所有的字段名稱

???????? ?* @param fileName

???????? ?* @param clazz

???????? ?* @return

???????? ?*/

???????? 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);

???????? }

}

?

?

?

?

?

總結

以上是生活随笔為你收集整理的Java使用Tomcat数据源的方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲毛片大全 | 国产亚洲av在线 | 国产嫩草在线 | 男男h黄动漫啪啪无遮挡软件 | 99热黄色 | 亚洲欧美自拍偷拍 | 欧美岛国国产 | 手机在线小视频 | 自拍毛片 | 日日夜夜免费视频 | 日本不卡123| 久久精品一区二区在线观看 | 高h大肚孕期孕妇play | 麻豆精品国产传媒mv男同 | 亚洲の无码国产の无码步美 | 婷婷影院在线观看 | 久久99视频 | 日韩欧美黄色片 | 伊人久久精品一区二区三区 | 国产在线18 | 国产91在线视频观看 | xxxx黄色片| 亚洲AV乱码国产精品观看麻豆 | 日韩一区二区在线观看 | 奇米狠狠干| 亚洲女成人图区 | 我不卡一区二区 | 久久精品视频一区 | 亚洲精品久久久中文字幕痴女 | 123成人网 | 国产噜噜噜噜久久久久久久久 | 影音先锋中文字幕一区二区 | 在线观看福利片 | 在线观看av黄色 | av亚州 | 日韩精品中文字幕一区 | 动漫美女无遮挡免费 | 好吊色视频在线观看 | 亚洲无码精品一区二区三区 | 九草在线视频 | 男男h黄动漫啪啪无遮挡软件 | 午夜av免费观看 | 日本人性爱视频 | 色美av| 亚洲.www| 综合网在线视频 | 五级黄高潮片90分钟视频 | 久一国产 | 天天天天躁天天爱天天碰2018 | 欧美性高潮视频 | 日本一区二区三区免费看 | 日日操夜夜爱 | 一本一道精品欧美中文字幕 | 91福利网站 | 四虎网站在线观看 | 天天干天天拍 | 亚洲第八页 | 人人超碰97 | 日韩av一区二区在线播放 | 美女搡bbb又爽又猛又黄www | 日本精品久久久久久 | 香蕉大人久久国产成人av | 91九色pron | 国产精品人人做人人爽人人添 | 91视频com | 精品午夜久久 | 亚洲国产精品一区二区久久hs | 精品人妻av一区二区 | 韩国毛片视频 | av五月| 天天艹天天爽 | 欧美日韩a级片 | 国产在线资源 | 热播之家 | 视频一区三区 | 日本视频在线 | 少妇闺蜜换浪荡h肉辣文 | 久久窝窝| 香蕉国产在线视频 | 日韩女优中文字幕 | 国产精品久久777777毛茸茸 | 夜色成人| 韩国三级与黑人 | 国产小视频在线免费观看 | 亚洲国产精品美女 | 免费欧美一级视频 | 那里有毛片看 | 国产美女无遮挡网站 | 日产精品久久久久久久蜜臀 | 入禽太深免费视频 | 国产奶头好大揉着好爽视频 | 久久久久久亚洲av毛片大全 | 国产乱码精品一区二三赶尸艳谈 | 色伊人久久 | 亚洲网址在线观看 | 日本一本不卡 | 蜜色影院 | 日韩不卡视频一区二区 | 国产在线一二区 |