java - SQL 连接错误,凭据正确,所有权限和访问权限均正确

标签 java mysql jdbc

此问题之前在 stackoverflow 上尚未得到解答,因此请不要将它们标记为重复。我之前已经尝试过所有解决方案。

我在 Android Studio 项目中使用了正确的凭据,但我总是收到带有未知错误 1045 的 SQLException。我不知道现在能做什么。这是抛出异常的行:

java.sql.Connection conn = getConnection("jdbc:mysql://png.services/dummy?user=dummy&password=test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC");

您可以在此处检查上面的凭据(它们是真实的:用户:虚拟密码:test): https://png.services/phpmyadmin/

到目前为止我做了什么:

  • 我检查过,用户可以从任何地方(不仅仅是本地主机)访问数据库
  • 用户拥有该表的所有权限
  • 在 python 中,连接使用相同的密码

我猜,连接字符串有问题。服务器的时区是 CEST。

无奈的堆栈跟踪:

    java.sql.SQLException: Unknown error 1045
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:270)
        at services.png.lena.database.MySQL.<clinit>(MySQL.java:32)
        at services.png.lena.MySQL_test.test_initMySQL(MySQL_test.java:46)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:27)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at 
 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

    java.lang.AssertionError: 
    Expected :services.png.lena.logic.DBResult@4c069e19
    Actual   :services.png.lena.logic.DBResult@10bdf9a6

最佳答案

简短回答:我使用了错误的字符。我必须更改 ... password=test?useUnicode=true ... 到 ... password=test&useUnicode=true ...

感谢戈德·汤普森

关于java - SQL 连接错误,凭据正确,所有权限和访问权限均正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043470/

相关文章:

java - javac 是否优化 "foo".length()?

java - 如何制作连续的Android ViewPager?

php - 我怎样才能让我的网络接收mysql数据

java - 从 JDBC 请求捕获值并用于下一个查询

java - 在准备好的 stament db2 中设置数组不工作 Java

java: 结果集错误

java - Eclipse:如何自动编译动态 Web 项目中的源代码

java - 文本文件字典Java

mysql - 无法在 MySQL 中为 AUTO_INCREMENT 设置初始值

mysql - MySQL中如何给表设置索引?