Java 使用JDBC时拒绝访问数据库

Java 使用JDBC时拒绝访问数据库,java,mysql,shell,jdbc,mariadb,Java,Mysql,Shell,Jdbc,Mariadb,我正在处理一个名为movieDB的简单mariab数据库,其中有一个名为customerAgent的mariab用户。我知道StackOverflow上有很多类似的帐户,但我使用的不是根帐户,而是具有最低权限的普通帐户 我可以通过SSH访问终端中的数据库movieDB,如下所示: [root@myServer]# mysql -ucustomerAgent -p123 Welcome to the MariaDB monitor. Commands end with ; or \g. Yo

我正在处理一个名为
movieDB
的简单mariab数据库,其中有一个名为
customerAgent
的mariab用户。我知道StackOverflow上有很多类似的帐户,但我使用的不是根帐户,而是具有最低权限的普通帐户


我可以通过SSH访问终端中的数据库
movieDB
,如下所示:

[root@myServer]# mysql -ucustomerAgent -p123

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 102
Server version: 10.2.12-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> USE movieDB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [movieDB]> show grants;
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for customerAgent@localhost                                                                                         |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT Customer_Role TO 'customerAgent'@'localhost'                                                                   |
| GRANT USAGE ON *.* TO 'customerAgent'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257'       |
| GRANT USAGE ON *.* TO 'Customer_Role'                                                                                      |
| GRANT SELECT ON `movieDB`.* TO 'Customer_Role'                                                                             |
| GRANT SELECT, INSERT ON `movieDB`.`orders` TO 'Customer_Role'                                                              |
+----------------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.00 sec)

MariaDB [movieDB]> select current_role();
+----------------+
| current_role() |
+----------------+
| Customer_Role  |
+----------------+
1 row in set (0.00 sec)

MariaDB [movieDB]> 

但是,当我在本地主机上执行JDBC代码时,在
stmt.execute(“USE movieDB”)行拒绝访问

java JDBC代码是:(我已经删除了类中一些不必要的内容,但是如果我遗漏了任何重要的内容,请指出!)



类似问题的一些答案说JDBC需要数据库上的所有特权,但这听起来既不安全也不安全。必须拥有所有特权才能实现我在这里尝试的目标吗?

您的问题是数据库movidDB不存在。是否应该是movieDB

您的问题是数据库movieDB不存在。是否应该是movieDB

您读过例外情况吗<代码>movidDB
!=
movieDB
…是的,我确实读过这个例外,我是多么愚蠢,我错过了打字错误!!!天哪,我花了整个下午的时间在谷歌的几十个搜索中寻找答案,所以答案……在使用JDBC时,你不应该使用像
use
这样的命令,你应该通过在URL中指定直接连接到movieDB,或者使用
setCatalog(…)
,另请参见。使用
use
可能会使连接处于不正确的状态,这可能会导致以后很难识别的错误。@markrottveel谢谢,Mark!非常感谢你的建议!我正在重写连接部分..你读过例外吗<代码>移动数据库!=
movieDB
…是的,我确实读过这个例外,我是多么愚蠢,我错过了打字错误!!!天哪,我花了整个下午的时间在谷歌的几十个搜索中寻找答案,所以答案……在使用JDBC时,你不应该使用像
use
这样的命令,你应该通过在URL中指定直接连接到movieDB,或者使用
setCatalog(…)
,另请参见。使用
use
可能会使连接处于不正确的状态,这可能会导致以后很难识别的错误。@markrottveel谢谢,Mark!非常感谢你的建议!我正在重写连接部分…是的,应该是movieDB!!我觉得自己太笨了,竟然错过了!它在这里被保存了下来,直到永远。干得好:)是的,应该是电影数据库!!我觉得自己太笨了,竟然错过了!它在这里被保存了下来,直到永远。干得好:)
Access denied for user 'customerAgent'@'localhost' to database 'movidDB'
import java.sql.*;
import java.util.*;

Class movieDBFoundation {
    static private String DBServerAddress = "localhost";
    static private Connection conn;

    static private String getDBServerAddress() {
        return DBServerAddress;
    }

    public static void main(String[] args) {

        System.out.println("Connection started.");

        if(DBConnect()) {
            System.out.println("Connection succedded.");
        }
        else {
            System.out.println("Connection failed.");
            return;
        }            
    }

    static private Boolean DBConnect() {
        String connectString = "jdbc:mysql://" + getDBServerAddress() + ":3306/"
              + "?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&&useSSL=false";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(connectString, "customerAgent", "123");
            System.out.println("Connection reached.");
            Statement stmt;
            stmt = conn.createStatement();
            String SQL = "USE movidDB";
            stmt.execute(SQL);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return false;
    }
}