• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mysql 搞代码 4年前 (2022-01-09) 27次浏览 已收录 0个评论
文章目录[隐藏]

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.gaodaima.com/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("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");            localFileNotFou<i>本文来源gaodai$ma#com搞$$代**码网</i>ndException.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>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:jdbc-JDBC使用反射读取properties文件出错

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址