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

时间:2013-08-12 20:23:42

标签: java mysql jdbc phpmyadmin

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,来自服务器的消息:“Host'mobile'301-126-255-40.near.illinois.edu'不允许连接到此MySQL服务器”

我搜索了这个,似乎我必须授予数据库权限,但我不知道我的MySQL命令提示符在哪里。有没有办法在phpMyAdmin中授予权限?
任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

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

然后去..  mysql -u user_name -p press_enter_key

然后输入密码

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

第2行:管理员

其中'admin'是您的密码。

然后..

的MySQL>将所有数据库名。*授予由'password'标识的cmsuser @ localhost;