java項目中讀取jdbc.properties文件操作
java內(nèi)容
Properties props = Resources.getResourceAsProperties("jdbc.properties"); String url = props.getProperty("jdbc.url"); String driver = props.getProperty("jdbc.driverClass"); String username = props.getProperty("jdbc.username"); String password = props.getProperty("jdbc.password"); Class.forName(driver).newInstance(); Connection conn = (Connection) DriverManager.getConnection(url, username, password);
jdbc.properties文件內(nèi)容
jdbc.driverClass = com.mysql.jdbc.Driver jdbc.url = jdbc\:mysql\://127.0.0.1\:3306/LY?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull&allowMultiQueries\=true jdbc.username = root jdbc.password = root jdbc.minPoolSize=2 jdbc.maxPoolSize=20 jdbc.checkoutTimeout=3000 jdbc.maxStatements=50 jdbc.testConnectionOnCheckin = false jdbc.idleConnectionTestPeriod =18000
補充知識:模仿com.alibaba.fastjson.JSONObject取值的PropertiesUtils
1.依賴于:fastjson
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.39</version> </dependency>
2.話不多說,上代碼
package com.gy.common.util; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.math.BigInteger; import java.sql.Timestamp; import java.util.Date; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.util.TypeUtils; /** * java讀取配置文件 * * @author Neo 2017-5-12 * @version 1.1 * */ @SuppressWarnings({ "unchecked", "rawtypes" }) public class PropertiesUtils { private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class); private static Properties properties; private static final String PROPERTIES_EGIS_FILE_NAME = "application/config.properties"; static { properties = new Properties(); InputStream scmsStream = null; try { scmsStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_EGIS_FILE_NAME); properties.load(scmsStream); logger.info("PropertiesUtils", "staitc init prop", properties.toString()); } catch (Exception e) { } finally { try { if (scmsStream != null) { scmsStream.close(); } } catch (Exception e) { } } } public static String getProperty(String key) { String result = properties.getProperty(key); return result; } public static String getProperty(String key, String defaultValue) { String result = properties.getProperty(key, defaultValue); return result; } public static String getProperties(String propertiesName, String key) { Properties props = new Properties(); InputStream inputStream = null; try { inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertiesName); props.load(inputStream); } catch (IOException e) { } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) { } } return props.getProperty(key); } public static Object getObject(String key, Class clazz) { Object obj = getProperty(key); return TypeUtils.castToJavaBean(obj, clazz); } public static Boolean getBoolean(String key) { Object value = getProperty(key); if (value == null) return null; else return TypeUtils.castToBoolean(value); } public static byte[] getBytes(String key) { Object value = getProperty(key); if (value == null) return null; else return TypeUtils.castToBytes(value); } public static boolean getBooleanValue(String key) { Object value = getProperty(key); if (value == null) return false; else return TypeUtils.castToBoolean(value).booleanValue(); } public static Byte getByte(String key) { Object value = getProperty(key); return TypeUtils.castToByte(value); } public static byte getByteValue(String key) { Object value = getProperty(key); if (value == null) return 0; else return TypeUtils.castToByte(value).byteValue(); } public static Short getShort(String key) { Object value = getProperty(key); return TypeUtils.castToShort(value); } public static short getShortValue(String key) { Object value = getProperty(key); if (value == null) return 0; else return TypeUtils.castToShort(value).shortValue(); } public static Integer getInteger(String key) { Object value = getProperty(key); return TypeUtils.castToInt(value); } public static int getIntValue(String key) { Object value = getProperty(key); if (value == null) return 0; else return TypeUtils.castToInt(value).intValue(); } public static Long getLong(String key) { Object value = getProperty(key); return TypeUtils.castToLong(value); } public static long getLongValue(String key) { Object value = getProperty(key); if (value == null) return 0L; else return TypeUtils.castToLong(value).longValue(); } public static Float getFloat(String key) { Object value = getProperty(key); return TypeUtils.castToFloat(value); } public static float getFloatValue(String key) { Object value = getProperty(key); if (value == null) return 0.0F; else return TypeUtils.castToFloat(value).floatValue(); } public static Double getDouble(String key) { Object value = getProperty(key); return TypeUtils.castToDouble(value); } public static double getDoubleValue(String key) { Object value = getProperty(key); if (value == null) return 0.0D; else return TypeUtils.castToDouble(value).doubleValue(); } public static BigDecimal getBigDecimal(String key) { Object value = getProperty(key); return TypeUtils.castToBigDecimal(value); } public static BigInteger getBigInteger(String key) { Object value = getProperty(key); return TypeUtils.castToBigInteger(value); } public static String getString(String key) { Object value = getProperty(key); if (value == null) return null; else return value.toString(); } public static Date getDate(String key) { Object value = getProperty(key); return TypeUtils.castToDate(value); } public static java.sql.Date getSqlDate(String key) { Object value = getProperty(key); return TypeUtils.castToSqlDate(value); } public static Timestamp getTimestamp(String key) { Object value = getProperty(key); return TypeUtils.castToTimestamp(value); } public static void main(String[] args) { if(getBooleanValue("isNeedLogin")) System.out.println("aaa"); } }
以上這篇java項目中讀取jdbc.properties文件操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
ActiveMQ基于zookeeper的主從(levelDB Master/Slave)搭建
這篇文章主要介紹了ActiveMQ基于zookeeper的主從levelDB Master/Slave搭建,以及Spring-boot下的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Java動態(tài)規(guī)劃之硬幣找零問題實現(xiàn)示例
本文主要介紹了Java動態(tài)規(guī)劃之硬幣找零問題實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08java實現(xiàn)ArrayList根據(jù)存儲對象排序功能示例
這篇文章主要介紹了java實現(xiàn)ArrayList根據(jù)存儲對象排序功能,結(jié)合實例形式分析了java針對ArrayList的相關(guān)運算、排序操作技巧,需要的朋友可以參考下2018-01-01Springboot打成war包并在tomcat中運行的部署方法
這篇文章主要介紹了Springboot打成war包并在tomcat中運行,在文中還給大家介紹了SpringBoot war包tomcat運行啟動報錯(Cannot determine embedded database driver class for database type NONE)的解決方法,需要的朋友可以參考下2018-01-01Java 時間格式轉(zhuǎn)換之impleDateFormat與Data API解析與使用
想必大家對 SimpleDateFormat 并不陌生。SimpleDateFormat 是 Java 中一個非常常用的類,他是以區(qū)域敏感的方式格式化和解析日期的具體類。 它允許格式化 (date -> text)、語法分析 (text -> date)和標準化2021-11-11