JDBC:连接到远程mySQL数据库吗? [英] JDBC: connect to remote mySQL database?
问题描述
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();
}
}
我尝试了这段代码,但是它给出了:
线程主"中的异常java.sql.SQLException:null,来自服务器的消息:不允许主机'mobile-130-126-255-40.near.illinois.edu'连接到该MySQL服务器"
我用谷歌搜索,似乎必须授予数据库特权,但是我不知道我的MySQL命令提示符在哪里.有什么办法可以在phpMyAdmin中授予特权?
任何帮助将不胜感激!
I tried this code, but it gives:
Exception in thread "main" java.sql.SQLException: null, message from server: "Host 'mobile-130-126-255-40.near.illinois.edu' is not allowed to connect to this MySQL server"
I googled about this and it seems that I have to grant privileges to the database, but I don't know where my MySQL command prompt is. Is there any way to grant privileges in phpMyAdmin?
Any help would be appreciated!
推荐答案
点击Windows + r 然后在其中键入"cmd"并按Enter.这将打开Windows的命令提示符.
hit windows+r then type "cmd" in it and hit enter.. this will open command prompt for windows.
然后去.. mysql -u用户名-p press_enter_key
then go.. mysql -u user_name -p press_enter_key
然后输入密码
即
第1行:mysql -u根-h本地主机-p
line-1 : mysql -u root -h localhost -p
第2行:管理员
其中"admin"是您的密码.
where 'admin' is your password.
然后..
mysql>将所有在databasename.*上的权限授予由密码"标识的cmsuser @ localhost;
mysql> grant all on databasename.* to cmsuser@localhost identified by 'password';
这篇关于JDBC:连接到远程mySQL数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!