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,不是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 ?? ?* @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数据源的方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 50元人民币可以存ATM机吗 有些地方可
- 下一篇: 中炬高新是国企吗