Unable to connect to LAN server; no exception is shown

时间:2019-02-18 00:28:37

标签: java mysql jdbc

I have tried to use another proyect and the connection is succesfull but from this proyect I am only able to connect to localhost mysql. I want it to work only on lan.

I am getting "Access denied for user 'root'@'192.168.1.70' (using password: YES)"

Code sample from not working proyect:

Connection con = null;
    Statement st = null; 
    ResultSet rs = null;
    try{ con = (Connection) DriverManager.getConnection("jdbc:mysql://"+home.credentials[0],home.credentials[1],home.credentials[2]);
        st = (Statement) con.createStatement();
        String s = "SELECT * FROM meta";
        rs = st.executeQuery(s);
        ResultSetMetaData rsmt = rs.getMetaData(); 
        while(rs.next()){
            int meta = rs.getInt("meta");
            goal.setText(Integer.toString(meta));
        }
     }catch(Exception e){}
        finally{
         try{ st.close(); 
         rs.close(); 
         con.close(); 
     }
        catch(Exception e){ JOptionPane.showMessageDialog(null, "Información no encontrada"); 
     } 
    }

Code sample from another proyect which connects succesfully

   try
    {
      // create our mysql database connection
      String myDriver = "org.gjt.mm.mysql.Driver";
      String myUrl = "jdbc:mysql://192.168.1.66:3306/jjeventoscore";
      Class.forName(myDriver);
      Connection conn = (Connection) DriverManager.getConnection(myUrl, "root", "");

      // our SQL SELECT query. 
      // if you only need a few columns, specify them by name instead of using "*"
      String query = "SELECT * FROM client";

      // create the java statement
      Statement st = (Statement) conn.createStatement();

      // execute the query, and get a java resultset
      ResultSet rs = st.executeQuery(query);

      // iterate through the java resultset
      while (rs.next()){
       String name = rs.getString("name");
            String last = rs.getString("last");
            String h_phone = rs.getString("h_phone");
            String m_phone = rs.getString("m_phone");
            String of_phone = rs.getString("of_phone");
            String ex_phone = rs.getString("ex_phone");
            String email = rs.getString("email");
            String medium = rs.getString("medium");
          System.out.println(name+" "+last);
      }
      st.close();
    }
    catch (Exception e)
    {
      System.err.println("Got an exception! ");
      System.err.println(e.getMessage());
    }
}

1 个答案:

答案 0 :(得分:1)

您需要向该数据库和该表上的用户授予特权。

在您的MySQL实例上具有特权:

USE jjeventoscore;
GRANT ALL ON jjeventoscore.* TO 'root'@'192.168.1.70';

或者尝试

GRANT ALL ON jjeventoscore.* TO 'root'@'192.168.1.70' IDENTIFIED BY '';

因为它说“使用密码:是”

另外,检查MySQL上的密码。它应该与此函数中的参数匹配

Connection conn = (Connection) DriverManager.getConnection(myUrl, "root", "");