溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何讀取java項目中的jdbc.properties文件

發(fā)布時間:2020-11-09 15:43:32 來源:億速云 閱讀:185 作者:Leah 欄目:開發(fā)技術

本篇文章為大家展示了如何讀取java項目中的jdbc.properties文件,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

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

上述內(nèi)容就是如何讀取java項目中的jdbc.properties文件,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI