java参数配置错误_java使用配置文件中参数连接数据库异常

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/select_test

root=root

pass=xhb

在使用ini文件中初始化连接数据库需要的参数如上所示,在使用properties类进行读取,期间出现了access denied for user 'root@localhost'(using password: YES)数据库密码错误无法进入                  Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 找不到Driver类com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Incorrect database name 'select_test ' 没有select_test数据库问题,但直接书写相关参数能连接数据库并读取数据,在对读取的数据和固定的参数字符串进行对比输出的内容一致但equals 和hashCode 值不同 ?

开始ini文件是用eclipse创建文件夹和创建文件在向文件中写入参数,后来在java项目中直接创建文件夹和ini文件进行读取和连接数据库则正常,则写了如下测试

1 packagetestten;2

3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.util.Properties;6

7 public classTest {8

9 privateString driver ;10 privateString url ;11 privateString root ;12 privateString pass ;13

14 public void init()throwsException{15

16 Properties p = newProperties();17 p.load(new FileInputStream(new File("E:\\学习\\lzy-java\\java实验\\project7\\TestJava\\properties\\mysql.ini"))) ;18 driver=p.getProperty("driver");19 url=p.getProperty("url");20 root=p.getProperty("root");21 pass=p.getProperty("pass");22

23 System.out.println("取得的 driver 值"+driver +"其哈希值"+driver.hashCode()+"相同字符创的哈希值"+"com.mysql.jdbc.Driver".hashCode());24 System.out.println("取得的 url 值"+url +"其哈希值"+url.hashCode()+"相同字符创的哈希值"+"jdbc:mysql://127.0.0.1:3306/select_test".hashCode());25 System.out.println("取得的 root 值"+root +"其哈希值"+root.hashCode()+"相同字符创的哈希值"+"root".hashCode());26 System.out.println("取得的pass 值"+pass +"其哈希值"+pass.hashCode()+"相同字符创的哈希值"+"xhb".hashCode());27

28 }29

30 public static void main(String[] args) throwsException{31 //TODO Auto-generated method stub

32 newTest().init();33 }34

35 }

1、使用eclipse创建ini文件,结果为

cdd4e8c2ef8f0ad36c359a4b3bdb7ab3.png

其中url的值不同

2、使用自己创建的ini文件,结果为

21975b524b3b70e8ce5f7171e8d75090.png

结论原因可能是文件的编码不同,java的unicode编码 windows 文件是GBK编码.具体原因待定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值