/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.util;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

/**
 *
 * @author Administrator
 */
public class JdbcProperties {

    public static String driverClassName;
    public static String url;
    public static String username;
    public static String password;
    public static boolean autoCommitOnClose;
    public static int initialPoolSize;
    public static int minPoolSize;
    public static int maxPoolSize;
    public static int acquireIncrement;
    public static int checkoutTimeout;
    public static int maxIdleTime;
    public static int idleConnectionTestPeriod;

    static {
        Properties prop = new Properties();
        InputStream in = IfishFilePath.class.getResourceAsStream("/jdbc.properties");
        try {
            prop.load(in);
            //利用反射机制给属性赋值
            Field[] fields = JdbcProperties.class.getDeclaredFields();
            for (Field field : fields) {
                String name = field.getName();
                //获取property.properties的值
                String value = prop.getProperty(name).trim();
                //判断这个值希望塞入的变量类型，并进行转换
                Object value1 = getValue(value, field);
                //转换完成后塞入此变量
                field.set(IfishFilePath.class, value1);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static Object getValue(String value, Field field) {
        if (field.getType().equals(int.class)) {
            return Integer.valueOf(value);
        } else if (field.getType().equals(boolean.class)) {
            return Boolean.valueOf(value);
        }
        return value;
    }
}
