[英]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中授予特權?
任何幫助,將不勝感激!
點擊Windows + r,然后在其中鍵入“ cmd”,然后按Enter。這將打開Windows的命令提示符。
然后去.. mysql -u用戶名-p press_enter_key
然后輸入密碼
即
第1行:mysql -u root -h localhost -p
第2行:管理員
其中“ admin”是您的密碼。
然后..
mysql>將所有在databasename。*上的權限授予“ password”標識的cmsuser @ localhost;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.