首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

解决Java应用程序中的SQLException:Access denied for user ‘root‘@‘localhost‘ 错误

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at BookManagement.<init>(BookManagement.java:23) at BookManagement.main(BookManagement.java:66)

02
您找到你想要的搜索结果了吗?
是的
没有找到

JDBC的配置(包括db.properties等)

3.1数据库URL 在连接数据库时必须使用各种与数据库类型相关的参数,比如主机名、端口号和数据库名。JDBC使用了一种与普通URL相类似的语法来描述数据源。 e.g. 常用数据库URL Derby: jdbc:derby://localhost:1527/COREJAVA;create=true PostgreSQL: jdbc:postgresql:COREJAVA MySQL: jdbc:mysql://host:port/database Oracle: jdbc:oracle:thin:@host:port:databse JDBC URL的语法一般为: jdbc:subprotocol:other stuff subprotocol用于指明连接到数据库的特定驱动程序。 other stuff参数的格式随所使用的subprotocol不同而不同。 3.2 驱动程序JAR文件 在运行访问数据库的程序时,需要将驱动程序的JAR文件包括到类路径中(编译时并不需要整个JAR文件) 从命令行启动时,只需要使用下面的命令 java -classpath .;driverJar ProgramName 通过;分号,将当前路径(由 . 字符标示的路径)与驱动程序的JAR文件分隔开。 3.3 启动数据库 数据库服务器在连接之前需要先启动 Derby数据库的启动步骤 (1)打开命令shell(linux)或cmd(windows)窗口C:\"Program Files"\Sun\JavaDB\lib (2)找到derbyrun.jar,一般在JavaDB中(C:\Program Files\Sun\JavaDB\lib) (3)启动服务 : java -jar derbyrun.jar server start (4)配置文件db.properties ij.driver=org.apache.derby.jdbc.ClientDriver ij.protocol=jdbc:derby://localhost:1527/ ij.database=DBNAME;create=true 注意 : 只有配置文件名和database可以使用任意名 (5)在另一个shell/cmd窗口中运行Derby的交互式脚本执行工具 : java -jar derbyrun.jar ij -p db.properties 注意 : 打开交互式执行脚本工具之后,会在derbyrun.jar所在目录下创建以配置文件中ij.database的值命名的文件夹。 (6)在打开的窗口中可以输入SQL语句,以;分号结尾。 (7)退出编辑器EXIT; (8)关闭服务器 : java -jar derbyrun.jar server shutdown 3.4 注册驱动器类 情况一:某些JDBC的JAR文件将自动注册驱动器类(Java Standard Edition Service Provider),包含META-INF/services/java.sql.Driver文件的JAR文件可以自动注册。 e.g.Derby中lib目录下JAR包derby.jar中包含java.sql.Driver文件。该文件中"org.apache.derby.jdbc.AutoloadedDriver"为Derby的JDBC驱动程序实现名字。 情况二:如果驱动程序JAR不支持自动注册,需要找出数据库提供商使用的JDBC驱动器的名字。 典型的名字如下: Oracle:oracle.jdbc.driver.OracleDriver SQLServer:com.microsoft.jdbc.sqlserver.SQLServerDriver MySQL:org.gjt.mm.mysql.Driver 或com.mysql.jdbc.Driver 注:这里实际上都是调用的com.mysql.jdbc.Driver,下面为org.gjt.mm.mysql.Driver源码

01

解决MySQL连接问题:Access Denied和SSL警告;MySQL数据库连接失败:Access Denied异常的解决方法;如何在Java应用程序中正确配置MySQL数据库连接

报错“Connected to the target VM, address: '127.0.0.1:59549', transport: 'socket' Wed Sep 13 16:56:02 CST 2023 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. java.sql.SQLException: Access denied for user 'username'@'localhost' (using password: YES) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at BookManagement.<init>(BookManagement.java:21) at BookManagement.main(BookManagement.java:62) Disconnected from the target VM, address: '127.0.0.1:59549', transport: 'socket' 进程已结束,退出代码 0

01

解决Java应用程序中的SQLException:服务器时区值未识别问题;MySQL连接问题:服务器时区值 ‘Öйú±ê׼ʱ¼ä‘ 未被识别的解决方法

java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:87) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:61) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:71) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862) at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230) at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at BookManagement.<init>(BookManagement.java:22) at BookManagement.main(BookManagement.java:64) Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.cj.exceptions.ExceptionFactory.cre

01
领券