简体   繁体   English

使用java获取mysql连接错误

[英]Get error in mysql connection using java

I am connecting to mysql using java. 我正在使用Java连接到mysql。 If I connect to mysql with the same PC in which mysql was installed, it works fine. 如果我使用安装了mysql的同一台PC连接到mysql,则工作正常。 But when I connect to that mysql instance from a different pc I get access denied. 但是,当我从另一台PC连接到该mysql实例时,访问被拒绝。 I mention that both PCs are in the same network. 我提到两台PC都在同一网络中。

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. 与网络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. 其中evokanti是用户名,而EVOKANTILALR是我的电脑名称。

But i donot know that why it takes my pc name ieEVOKANTILALR in connection ???? 但是我不知道为什么要在连接中使用我的电脑名称ieEVOKANTILALR?

´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'@'%';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM