Java 在Ubuntu18.04中,JDBC通过eclipse连接mySql(mySql 5.7.22)时出错

Java 在Ubuntu18.04中,JDBC通过eclipse连接mySql(mySql 5.7.22)时出错,java,mysql,eclipse,jdbc,ubuntu-18.04,Java,Mysql,Eclipse,Jdbc,Ubuntu 18.04,请帮助解决此错误。同样的代码在windows中运行良好。但是我不明白为什么它不能在Linux(Ubuntu18.04)上运行 以下代码: import java.sql.*; public class HelloWorld { public static void main(String[] args) { Connection con = null; Statement st = null; try { Cla

请帮助解决此错误。同样的代码在windows中运行良好。但是我不明白为什么它不能在Linux(Ubuntu18.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)
运行代码时引发此异常:

 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)
加载类'com.mysql.jdbc.Driver'。这是不赞成的。新的驱动程序类是'com.mysql.cj.jdbc.driver'。驱动程序通过SPI自动注册,通常不需要手动加载驱动程序类。
Sat Jul 07 12:52:40 IST 2018警告:不建议在没有服务器身份验证的情况下建立SSL连接。根据MySQL 5.5.45+、5.6.26+和5.7.6+的要求,如果未设置explicit选项,默认情况下必须建立SSL连接。为了符合不使用SSL的现有应用程序,verifyServerCertificate属性设置为“false”。您需要通过设置useSSL=false显式禁用SSL,或者设置useSSL=true并为服务器证书验证提供信任库。
java.sql.SQLException:拒绝用户“root”@“localhost”的访问
位于com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
位于com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
位于com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
位于com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
位于com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:444)
位于com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
位于com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
位于java.sql.DriverManager.getConnection(DriverManager.java:664)
位于java.sql.DriverManager.getConnection(DriverManager.java:247)
位于com.amitsigh.HelloWorld.main(HelloWorld.java:10)
可能用户
根用户
没有足够的权限从本地主机进行连接。
您可以选中此选项以从命令行/shell连接到数据库
使用以下命令进行连接:

mysql --host=localhost --user=root --password=root mydb
如果连接未成功,则应更新用户证书

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

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

如果没有CLass.forName,你可以试试吗?请删除它不起作用。仍然出现错误。是否将Class.forNAme更改为com.mysql.cj.jdbc.Driveryes。将com.mysql.jdbc.Driver更改为com.mysql.cj.jdbc.Driver“**com.mysql.jdbc.Driver”后,已弃用。新的驱动程序类为`com.mysql.cj.jdbc.Driver'。该驱动程序将通过………..自动注册。…..“消失”。在终端上运行此命令后,mysql可能会出现重复的错误started@Amit辛格:你试图给用户更多的权限吗?@jivdm:如何给用户更多的权限?