今天在用jdbc 连接MySQL数据库时一直不停的在 Statement stmt = con.createStatement() ; 这一句上报空指针异常无法连接到数据库。
然后第一步用了http://outofmemory.cn/code-snippet/1085/java-usage-JDBC-connection-MYSQL-database这个网址上的一个方法,下载:mysql-connector-java-5.1.17-bin.jar JAR包,然后放进jdk1.6.0_37\jre\lib\ext 重启eclispe 就可以在JRE系统库中看到。不过没有将问题解决。
第二步参考了*上的一个解决方案,将问题解决掉了,就是重新在MySQL数据库新建一个用户,然后将在该用户名下重建了数据库。最后在代码中将连接的用户名及密码,用新建用户。而不去使用root用户。具体内容参见:
“Connect failed: Access denied for user 'root'@'localhost' (using password: YES)” from php function
up vote12down votefavorite7 | I wrote some function used by a php webpage, in order to interact with a mysql database. When I test them on my server I get this error:
I am able to use them on my pc (using XAMPP) and I can navigate through the tables of the database using the command line in the server. Instead, the webpage fails to connect. I've checked the password but with no results, it's correct (otherwise I could not log in to mysql from the command line). The call of the function is the following:
Have I to set something in my server? Thanks Edit: PHP version 5.3.3-7+squeeze1 mysql version: 5.1.49-3 both on debian phpmysql access-denied |
||||
migrated from superuser.com Jun 22 '11 at 20:11This question came from our site for computer enthusiasts and power users. |
|||||
|
8 Answers 8
activeoldestvotesup vote25down voteaccepted |
I solved in this way: I logged in with root username
I created a new user with
then I created the database
I granted privileges for new user for this database
Then I logged out root and logged in new user
I rebuilt my database using a script
And that's it.. Now from php works without problems with the call
Thanks to all for your time :) |