gpt4 book ai didi

java - JDBC:连接到远程 mySQL 数据库?

转载 作者:行者123 更新时间:2023-11-29 00:27:40 25 4
gpt4 key购买 nike

This is my MySQL database information.

This is my web server hosting information.

    public void readDataBase() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://185.28.21.11/u505743489_db?"+ "user=u505743489_db&password=password");

// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// Result set get the result of the SQL query
resultSet = statement
.executeQuery("select * from FEEDBACK.COMMENTS");
writeResultSet(resultSet);

// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into FEEDBACK.COMMENTS values (default, ?, ?, ?, ? , ?, ?)");
// "myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS");
// Parameters start with 1
preparedStatement.setString(1, "Test");
preparedStatement.setString(2, "TestEmail");
preparedStatement.setString(3, "TestWebpage");
preparedStatement.setDate(4, new java.sql.Date(2009, 12, 11));
preparedStatement.setString(5, "TestSummary");
preparedStatement.setString(6, "TestComment");
preparedStatement.executeUpdate();

preparedStatement = connect
.prepareStatement("SELECT myuser, webpage, datum, summary, COMMENTS from FEEDBACK.COMMENTS");
resultSet = preparedStatement.executeQuery();
writeResultSet(resultSet);

// Remove again the insert comment
preparedStatement = connect
.prepareStatement("delete from FEEDBACK.COMMENTS where myuser= ? ; ");
preparedStatement.setString(1, "Test");
preparedStatement.executeUpdate();

resultSet = statement
.executeQuery("select * from FEEDBACK.COMMENTS");
writeMetaData(resultSet);

} catch (Exception e) {
throw e;
} finally {
close();
}

}

我试过这段代码,但它给出了:
线程“main”中的异常 java.sql.SQLException:null,来自服务器的消息:“不允许主机‘mobile-130-126-255-40.near.illinois.edu’连接到此 MySQL 服务器”

我在谷歌上搜索了一下,似乎我必须授予数据库权限,但我不知道我的 MySQL 命令提示符在哪里。有什么方法可以在 phpMyAdmin 中授予权限?
如有任何帮助,我们将不胜感激!

最佳答案

点击windows+r然后在其中键入“cmd”并按回车键。这将打开 Windows 命令提示符。

然后去.. mysql -u 用户名-p press_enter_key

然后输入密码

第 1 行:mysql -u root -h localhost -p

第 2 行:管理员

“admin”是您的密码。

然后..

mysql> grant all on databasename.* to cmsuser@localhost identified by 'password';

关于java - JDBC:连接到远程 mySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18196032/

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