gpt4 book ai didi

java - 使用 JDBC 时拒绝访问数据库

转载 作者:太空宇宙 更新时间:2023-11-03 11:33:02 25 4
gpt4 key购买 nike

我正在开发一个名为 movieDB 的简单 mariaDB 数据库,并且有一个名为 customerAgent 的 mariaDB 用户。我知道 StackOverflow 上有很多类似的帐户,但我使用的不是 root 帐户,而是具有最低授予权限的普通帐户。


我可以像这样通过 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"); 行拒绝访问:

Access denied for user 'customerAgent'@'localhost' to database 'movidDB'

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

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;
}
}

类似问题中的一些答案说 JDBC 需要对数据库的所有 权限,但这听起来不太安全。是否必须拥有所有权限才能实现我在这里尝试做的事情?

最佳答案

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

关于java - 使用 JDBC 时拒绝访问数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48148333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com