java项目中读取jdbc.properties文件操作
java内容
Propertiesprops=Resources.getResourceAsProperties("jdbc.properties"); Stringurl=props.getProperty("jdbc.url"); Stringdriver=props.getProperty("jdbc.driverClass"); Stringusername=props.getProperty("jdbc.username"); Stringpassword=props.getProperty("jdbc.password"); Class.forName(driver).newInstance(); Connectionconn=(Connection)DriverManager.getConnection(url,username,password);
jdbc.properties文件内容
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
com.alibaba fastjson 1.2.39
2.话不多说,上代码
packagecom.gy.common.util; importjava.io.IOException; importjava.io.InputStream; importjava.math.BigDecimal; importjava.math.BigInteger; importjava.sql.Timestamp; importjava.util.Date; importjava.util.Properties; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importcom.alibaba.fastjson.util.TypeUtils; /** *java读取配置文件 * *@authorNeo2017-5-12 *@version1.1 * */ @SuppressWarnings({"unchecked","rawtypes"}) publicclassPropertiesUtils{ privatestaticLoggerlogger=LoggerFactory.getLogger(PropertiesUtils.class); privatestaticPropertiesproperties; privatestaticfinalStringPROPERTIES_EGIS_FILE_NAME="application/config.properties"; static{ properties=newProperties(); InputStreamscmsStream=null; try{ scmsStream=PropertiesUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_EGIS_FILE_NAME); properties.load(scmsStream); logger.info("PropertiesUtils","staitcinitprop",properties.toString()); }catch(Exceptione){ }finally{ try{ if(scmsStream!=null){ scmsStream.close(); } }catch(Exceptione){ } } } publicstaticStringgetProperty(Stringkey){ Stringresult=properties.getProperty(key); returnresult; } publicstaticStringgetProperty(Stringkey,StringdefaultValue){ Stringresult=properties.getProperty(key,defaultValue); returnresult; } publicstaticStringgetProperties(StringpropertiesName,Stringkey){ Propertiesprops=newProperties(); InputStreaminputStream=null; try{ inputStream=PropertiesUtils.class.getClassLoader().getResourceAsStream(propertiesName); props.load(inputStream); }catch(IOExceptione){ }finally{ try{ if(inputStream!=null){ inputStream.close(); } }catch(Exceptione){ } } returnprops.getProperty(key); } publicstaticObjectgetObject(Stringkey,Classclazz){ Objectobj=getProperty(key); returnTypeUtils.castToJavaBean(obj,clazz); } publicstaticBooleangetBoolean(Stringkey){ Objectvalue=getProperty(key); if(value==null) returnnull; else returnTypeUtils.castToBoolean(value); } publicstaticbyte[]getBytes(Stringkey){ Objectvalue=getProperty(key); if(value==null) returnnull; else returnTypeUtils.castToBytes(value); } publicstaticbooleangetBooleanValue(Stringkey){ Objectvalue=getProperty(key); if(value==null) returnfalse; else returnTypeUtils.castToBoolean(value).booleanValue(); } publicstaticBytegetByte(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToByte(value); } publicstaticbytegetByteValue(Stringkey){ Objectvalue=getProperty(key); if(value==null) return0; else returnTypeUtils.castToByte(value).byteValue(); } publicstaticShortgetShort(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToShort(value); } publicstaticshortgetShortValue(Stringkey){ Objectvalue=getProperty(key); if(value==null) return0; else returnTypeUtils.castToShort(value).shortValue(); } publicstaticIntegergetInteger(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToInt(value); } publicstaticintgetIntValue(Stringkey){ Objectvalue=getProperty(key); if(value==null) return0; else returnTypeUtils.castToInt(value).intValue(); } publicstaticLonggetLong(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToLong(value); } publicstaticlonggetLongValue(Stringkey){ Objectvalue=getProperty(key); if(value==null) return0L; else returnTypeUtils.castToLong(value).longValue(); } publicstaticFloatgetFloat(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToFloat(value); } publicstaticfloatgetFloatValue(Stringkey){ Objectvalue=getProperty(key); if(value==null) return0.0F; else returnTypeUtils.castToFloat(value).floatValue(); } publicstaticDoublegetDouble(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToDouble(value); } publicstaticdoublegetDoubleValue(Stringkey){ Objectvalue=getProperty(key); if(value==null) return0.0D; else returnTypeUtils.castToDouble(value).doubleValue(); } publicstaticBigDecimalgetBigDecimal(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToBigDecimal(value); } publicstaticBigIntegergetBigInteger(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToBigInteger(value); } publicstaticStringgetString(Stringkey){ Objectvalue=getProperty(key); if(value==null) returnnull; else returnvalue.toString(); } publicstaticDategetDate(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToDate(value); } publicstaticjava.sql.DategetSqlDate(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToSqlDate(value); } publicstaticTimestampgetTimestamp(Stringkey){ Objectvalue=getProperty(key); returnTypeUtils.castToTimestamp(value); } publicstaticvoidmain(String[]args){ if(getBooleanValue("isNeedLogin")) System.out.println("aaa"); } }
以上这篇java项目中读取jdbc.properties文件操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。