• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > jdbc-JDBC使用反射读取properties文件出错

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

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了jdbc java java反射 mysql等相关知识,希望本文的分享对您有所帮助
jdbcjavajava反射mysql

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

@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(); }

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

回复内容:

读取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中的,否则会扫描普通文件,希望对你理解有帮助

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

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

 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);
    }
}
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2018-12-05PHP 连接MAMP时数据库服务连接不上该怎么办?
  • 2018-12-05关于节点配置的详细介绍
  • 2018-12-05mysql之通过配置文件链接数据库的示例详解
  • 2018-12-05php Closure类的使用方法
  • 2017-08-25Mysql中各类型对应JDBC类型编号
  • 2018-12-05Python快速教程(补充篇01):Python的序列的方法
  • 2018-12-05可以定时重启MSSQL的脚本或者程序
  • 2018-12-05Oracle 9i轻松取得建表和索引的DDL语句
  • 2018-12-05安装SQL2005提示 找不到任何SQL2005组件的问题解决方案
  • 2018-12-05解析MySQL动态修改varchar长度的方法

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • MySQL数据库如何锁定和解锁数据库表?
    • sqlserver 导出插入脚本代码
    • 关于分区功能的10篇文章推荐
    • MYSQL 修改root密码命令小结
    • mysql 常用命令用法总结脚本之家整理版
    • SQL server 2008 数据安全(备份和恢复数据库)
    • SQL server2000的理论认识
    • mysql-MYSQL 5.7 root 密码 忘记
    • MySQL笔记之修改数据的解决方法
    • Oracle数据库索引的维护

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有