Java数据库mySQL访问被拒绝

问题描述 投票:1回答:5

所以我试图连接到我的数据库并显示表中的项目。我得到的错误是:SQL异常:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:拒绝用户'Bob'@'%'访问数据库'TEST'

这种连接是否正确,如果是,那么凭据是错误的错误?如果他们错了怎么连接?谢谢

try 
     {  
        Class.forName("com.mysql.jdbc.Driver");        
        String url = "jdbc:mysql://THISISTHEHOSTNAME";
        String username = "Bob";
        String password = "password";
        Connection connection = DriverManager.getConnection(url, username, password);
         Statement stmt = null;
        ResultSet rs = null;
        //SQL query command
        String SQL = "SELECT * FROM TEST";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(SQL);
        while (rs.next())
        {
            System.out.println(rs.getString("ProductName") + " : " + rs.getString("UnitPrice"));
        }
     } 
     catch (SQLException e) 
     {
        System.out.println("SQL Exception: "+ e.toString());
     } 
     catch (ClassNotFoundException cE) 
     {
        System.out.println("Class Not Found Exception: "+ cE.toString());
     }
java mysql sql database
5个回答
5
投票

您需要为连接到mysql db的用户授予适当的权限。

您收到的消息是通知您,当您的用户能够连接到数据库服务器时,不允许访问数据库TEST。

在mysql控制台中运行以下命令将授予此类访问权限:

GRANT ALL ON TEST.* TO 'BOB'@'%'

这是非常宽容的,您应该记住,db用户应该具有尽可能少的权限,并且限制在最小范围的主机上。


0
投票

我使用ShowIP Firefox添加并使用IP而没有得到任何错误,但我想知道是否应该返回访问权限,我现在无法找到答案。


-1
投票

这里有几件事要做:

  1. 检查您的用户名和密码是否正确。
  2. 您是否将用户名添加到了正确的数据库? (这需要在CPanel SQL中完成)
  3. 您是否允许数据库从您的IP地址获取连接访问权限? (这也需要在CPanel SQL中完成)

-1
投票

在mysql中创建一个新用户检查所有全局权限>转到netbeans中的服务>数据库> localhost中的mysql服务器>右键单击connect并填写用户名密码

这项工作对我来说


-1
投票

请尝试以下代码:

import java.sql.*;
public class InsertPrepared {

   public static void main(String[] args)
   {
      try
      {
         Class.forName("com.mysql.jdbc.Driver");
         Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306//test","sanjib","");
         PreparedStatement stmt=con.prepareStatement("insert into employee values(???)");

         stmt.setInt(1,101);
         stmt.setString(2,"Sampa");

         int i=stmt.executeUpdate();
         System.out.println(i+"Records is inserted");
         con.close();
      }

      catch(Exception e)
      {
         System.out.println(e);
      }

   }
}
© www.soinside.com 2019 - 2024. All rights reserved.