我从浏览器创建了一个名为'test'的数据库,现在尝试使用JDBC驱动程序和语法从java代码访问它:
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "pw");
st = con.createStatement();
我收到错误:
Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test'
我搜索了它,我得到了可能的解决方案“创建名为'test'的数据库”,但那里已经存在。可能是什么原因以及如何处理它?</ p>
修改
我的conf文件有一个代码段:
# The following options will be passed to all MySQL clients
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[wampmysqld]
port = 3306
socket = /tmp/mysql.sock
表明端口确实是3306
答案 0 :(得分:-2)
您似乎忘记了自己的端口号。默认值为3306.因此您可以将其更改为
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "pw");
并查看是否有效。
希望它有所帮助。