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文件,结果为
其中url的值不同
2、使用自己创建的ini文件,结果为
结论原因可能是文件的编码不同,java的unicode编码 windows 文件是GBK编码.具体原因待定