我尝试使用另一个项目并且连接成功但是从这个项目我只能连接到 localhost mysql。我希望它只在局域网上工作。我收到“用户'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条经验 获得超6个赞
您需要为该数据库和该表上的用户授予权限。
使用特权,在您的 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", "");
添加回答
举报
0/150
提交
取消
