2013-11-28 74 views
-1
String name = ""; 
    String port = "3306"; 
    String user = "root"; 
    String pass = ""; 
    String dbname = "atm"; 
    String host="192.168.5.219"; // my local host ip address 
    // I want to do this only with IP address.. 
    try { 

     Class.forName("com.mysql.jdbc.Driver").newInstance(); 
     String url = "jdbc:mysql://"+host+":"+ port + "/" + dbname; 
     System.out.println("URL:" + url); 
     //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
     Connection con = DriverManager.getConnection(url, user, pass); 

     String qry2 = "select * from atm"; 
     Statement st = con.createStatement(); 
     ResultSet rs = st.executeQuery(qry2); 
     while (rs.next()) { 

      name = rs.getString("name"); 
      System.out.println("Name:" + name); 
      j.setText(""+name); 

     } 
     rs.close(); 
     st.close(); 
     con.close(); 


    } catch (Exception e) { 
     javax.swing.JOptionPane.showMessageDialog(null, e.getMessage()); 
     System.out.println(e.getMessage()); 
    } 

當我運行此我得到了一個錯誤:如何通過使用IP地址作爲主機名來訪問mysql數據?

error>> null, message from server: "Host 'DEEPAK-PC' is not allowed to connect to this MySQL server"

其實我想打一個程序,從連接到我在學校局域網其他電腦中獲取數據。所以這就是爲什麼我想用IP地址來做到這一點。

+0

這是不是Java的問題。是MYSQL用戶permision相關的問題。 檢查此[這裏](http://stackoverflow.com/questions/670949/remote-mysql-connection) – PbxMan

回答

1

你必須先授予權限從遠程位置訪問MySQL

試試這個

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.5.219' 
    IDENTIFIED BY PASSWORD 'some_characters' 
    WITH GRANT OPTION; 
FLUSH PRIVILEGES; 

檢查這個mysql site更多信息

相關問題