我嘗試使用另一個項目并且連接成功但是從這個項目我只能連接到 localhost mysql。我希望它只在局域網(wǎng)上工作。我收到“用戶'root'@'192.168.1.70'的訪問被拒絕(使用密碼:YES)”來自不工作項目的代碼示例: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"); } }來自另一個成功連接的項目的代碼示例 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); } st.close(); } catch (Exception e) { System.err.println("Got an exception! "); System.err.println(e.getMessage()); }}
1 回答

慕絲7291255
TA貢獻1859條經(jīng)驗 獲得超6個贊
您需要為該數(shù)據(jù)庫和該表上的用戶授予權限。
使用特權,在您的 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 上的密碼。它應該與此函數(shù)中的參數(shù)匹配
Connection conn = (Connection) DriverManager.getConnection(myUrl, "root", "");
添加回答
舉報
0/150
提交
取消