Heim  >  Artikel  >  Datenbank  >  jdbc-JDBC使用反射读取properties文件出错

jdbc-JDBC使用反射读取properties文件出错

WBOY
WBOYOriginal
2016-08-04 08:53:391359Durchsuche

jdbcjavajava反射mysql

使用反射获取类加载器来读取properties文件出现空指针异常,可以使用直接使用输入流读取properties文件,为什么教学视频中却可以使用反射?另外问下怎么在PC端提问,不是论坛发帖,单纯悬赏C币提问,我都是在手机上提,电脑上修改

<code>@Test public   void getConnection() throws Exception { /* * 读取配置文件来获取数据库连接 */    Properties properties = new Properties(); String driverClass = null; String jdbcUrl = null; String user = null; String password = null; InputStream in =    this.getClass().getClassLoader().getResourceAsStream("C:/Java/WprkSpace/JDBC/jdbc.properties"); properties.load(in); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("jdbcUrl"); user = properties.getProperty("user"); password = properties.getProperty("password"); Driver driver =  (Driver) Class.forName(driverClass).newInstance(); properties.put("user", user); properties.put("password", password); Connection connerction = driver.connect(jdbcUrl, properties); System.out.println( connerction); in.close(); }</code>

图片说明
图片说明
图片说明
图片说明

回复内容:

读取properties文件可以有多种方式,用IO或者用ClassLoader,绝对路径的用IO,相对路径可以用IO也可以用ClassLoader,因为相对路径是相对于Classpath的。一般来说用到ClassLoader的话都是用的相对路径了。

http://bbs.csdn.net/topics/391984276

使用PropertyPlaceholderConfigurer读取.properties文件(1)
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

路径改成相对于SRC的路径吧。

classloader去加载的是classpath下的资源,加*会加载jar中的,否则会扫描普通文件,希望对你理解有帮助

<code>        一般没有绝对路径这种用法吧,非要用试一试
        Properties props = new Properties(); 
        File file=new File("H:\\DESKTOP\\email.properties");
        props.load(this.getClass().getClassLoader().getResourceAsStream(file.getName()));
</code>

1,把jdbc.properties 放到src下 ,
2.

<code> public class CMConstant {
public static String getConfigureParameterFromJDBC(String paramString) {
        String str = CMConstant.getRootPath() + File.separator + "WEB-INF"
                + File.separator + "classes/config.properties";
        Properties localProperties = new Properties();
        try {
            FileInputStream localFileInputStream = new FileInputStream(str);
            localProperties.load(localFileInputStream);
            localFileInputStream.close();
        } catch (FileNotFoundException localFileNotFoundException) {
            logger.error("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
            localFileNotFoundException.printStackTrace();
            return null;
        } catch (IOException localIOException) {
            logger.error("装载文件--->失败!");
            localIOException.printStackTrace();
        }
        return localProperties.getProperty(paramString);
    }

    public static String getRootPath() {
        String result = null;
        try {
            result = CMConstant.class.getResource("CMConstant.class").toURI()
                    .getPath().toString();
        } catch (URISyntaxException e1) {
            e1.printStackTrace();
        }
        int index = result.indexOf("WEB-INF");
        if (index == -1) {
            index = result.indexOf("bin");
        }
        result = result.substring(1, index);
        if (result.endsWith("/"))
            result = result.substring(0, result.length() - 1);// 不包含最后的"/"
        return result;
    }
/**
     * 查找config/jdbc.properties里值
     * @Description: @param key
     * @Description: @return
     * @Last Modified: , Date Modified:
     */
    public static String getJDBCValue(String key) {
        String filePath = getRootPath() + File.separator + "WEB-INF\\classes"
                + File.separator + "config.properties";
        Properties propertie = new Properties();
        try {
            FileInputStream inputFile = new FileInputStream(filePath);
            propertie.load(inputFile);
            inputFile.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return null;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return propertie.getProperty(key);
    }
}
</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn