0

I am connecting to mysql using java. If I connect to mysql with the same PC in which mysql was installed, it works fine. But when I connect to that mysql instance from a different pc I get access denied. I mention that both PCs are in the same network.

public class Temp {

    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://192.168.0.96:3306/mobile_erp_db";

    // Database credentials
    static final String USER = "evokanti";
    static final String PASS = "evosys12";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        try {
            // STEP 2: Register JDBC driver
            Class.forName("com.mysql.jdbc.Driver");

            // STEP 3: Open a connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // STEP 4: Execute a query
            System.out.println("Creating statement...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT * FROM menu";
            ResultSet rs = stmt.executeQuery(sql);

            // STEP 5: Extract data from result set
            while (rs.next()) {
                // Retrieve by column name
                String first = rs.getString(2);

                // Display values
                System.out.println(", first: " + first);
            }
            // STEP 6: Clean-up environment
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // Handle errors for JDBC
            System.out.println("SQLException");
            se.printStackTrace();
        } catch (Exception e) {
            // Handle errors for Class.forName
            System.out.println("Exception");
            e.printStackTrace();
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            }// nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            }// end finally try
        }// end try
        System.out.println("Goodbye!");
    }// end main
}// end class

output when I connect with network pc.

java.sql.SQLException: Access denied for user 'evokanti'@'EVOKANTILALR' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at com.db.Temp.main(Temp.java:30)

in this above error which has first line :

java.sql.SQLException: Access denied for user 'evokanti'@'EVOKANTILALR' (using password: YES) 

where evokanti is username and EVOKANTILALR is my pc name.

But i donot know that why it takes my pc name i.e.EVOKANTILALR in connection ????

5
  • Make sure that "192.168.0.96" is not your machine IP address. Also make sure you have compiled your program and clean it before running it again. Commented Dec 11, 2014 at 9:17
  • Thanks for reply. yes, I am sure that 192.168.0.96 is IP of mysql where installed and also i have compiled before running. Commented Dec 11, 2014 at 9:19
  • is the port enable on your firewall's second machine? Commented Dec 11, 2014 at 9:31
  • Check that you are providing right credentials ? Commented Dec 11, 2014 at 9:37
  • Can you try to ping your machine EVOKANTILALR and check what is the ip address of your machine ? Commented Dec 11, 2014 at 9:51

1 Answer 1

1

´Make sure the user is created correctly and has the rights , for example:

CREATE USER 'evokanti'@'%' IDENTIFIED BY 'password';
GRANT ALL ON evokanti.* TO 'evokanti'@'%';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.