【问题标题】:Getting error while JDBC connection with mySql(mysql 5.7.22) through eclipse in ubuntu 18.04 [duplicate]在ubuntu 18.04中通过eclipse与mySql(mysql 5.7.22)进行JDBC连接时出错[重复]
【发布时间】:2018-12-15 16:35:32
【问题描述】:

请帮助解决这个错误。相同的代码在 Windows 中运行良好。但我不明白为什么它不在 Linux(Ubuntu 18.04) 中运行。

以下代码:

 import java.sql.*;
public class HelloWorld {

    public static void main(String[] args) {
        Connection con = null;
        Statement st = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
            String sql = "insert into mytable values('amsing@gmail.com','amit1234')";
            st = con.createStatement();
            int x = st.executeUpdate(sql);
            if(x==1) {
                System.out.println("Record inserted");
            }else {
                System.out.println("Record not inserted");
            }
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(st!=null)st.close();
                if(con!=null)con.close();
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }  
}  

运行代码时抛出此异常:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Sat Jul 07 12:52:40 IST 2018 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 'root'@'localhost'
    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 com.amitsingh.HelloWorld.main(HelloWorld.java:10)

【问题讨论】:

  • 你可以尝试不使用 CLass.forName 请删除它
  • 不工作。仍然出现错误。
  • 你把Class.forNAme改成com.mysql.cj.jdbc.Driver了吗
  • 是的。将 com.mysql.jdbc.Driver 更改为 com.mysql.cj.jdbc.Driver " **com.mysql.jdbc.Driver'。已弃用。新的驱动程序类是 `com.mysql.cj.jdbc.Driver '. 驱动程序通过 ................. 自动注册“消失了。但仍然得到其余的错误。

标签: java mysql eclipse jdbc ubuntu-18.04


【解决方案1】:

也许用户 root 没有足够的权限从 localhost 进行连接。 您可以选中此项以从命令行/shell 连接到数据库 使用以下命令进行连接:

mysql --host=localhost --user=root --password=root mydb

如果连接不成功,则应更新用户凭据。

出于测试目的,您可以将所有权限授予用户。连接mysql后即可完成。

GRANT ALL PRIVILEGES ON mydb.* TO 'root'@'%' WITH GRANT OPTION;

【讨论】:

  • 在终端上运行此命令后mysql已经启动
  • @Amit Singh :您是否尝试赋予用户更多权限?
  • @jivdm : 如何赋予用户更多权限?
猜你喜欢
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 2019-05-11
  • 2015-09-05
相关资源
最近更新 更多