0

please help to solve this error. the same code is working fine in windows. but i'm not understanding why its not running in Linux(Ubuntu 18.04) .

The Following code:

 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();
            }
        }
    }  
}  

Throws this exception while running the code:

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)
Amit Singh
  • 81
  • 2
  • 11
  • Can you try without CLass.forName please just remove it – Youcef LAIDANI Jul 07 '18 at 07:43
  • not working. Still getting error. – Amit Singh Jul 07 '18 at 07:45
  • Did you change the Class.forNAme to com.mysql.cj.jdbc.Driver – Youcef LAIDANI Jul 07 '18 at 07:53
  • yes. after changing com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver " **com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via ................. " gone. But still getting rest of that error. – Amit Singh Jul 07 '18 at 08:03
  • possible duplicate of [java.sql.SQLException: Access denied for user 'root'@'localhost'](https://stackoverflow.com/questions/11922323/java-sql-sqlexception-access-denied-for-user-rootlocalhost-using-password) – raviraja Jul 07 '18 at 08:17

1 Answers1

0

Perhaps the user root has no sufficient rights to connect from localhost. You can check this to connect to the database form the command line / shell Use the following command to connect:

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

If the connection is not succesfull then the user credentionals should be updated.

For testing purposes you could give all rights to the user. This can be done after connected to mysql.

GRANT ALL PRIVILEGES ON mydb.* TO 'root'@'%' WITH GRANT OPTION;
jljvdm
  • 66
  • 7