使用JDBC连接到远程MySQL(主机gator)服务器时出错

时间:2014-08-01 22:57:43

标签: java mysql eclipse jdbc

我正在使用Eclipse,我正在尝试通过Host Gator连接到我的网站mysql数据库。这是我在Main.java文件中到目前为止的代码。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.SQLException;
//import com.mysql.jdbc.Connection;

public class Main 
{   
    public static void main(String[] args) throws Exception
    {
        Class.forName("com.mysql.jdbc.Driver");     
        Connection con = DriverManager.getConnection("jdbc:mysql://19x.xxx.xxx.xxx:3306/kwinrow_storage","kwinrow_admin","xxxxxxx");
        PreparedStatement statement = con.prepareStatement("SELECT * FROM post_data WHERE type= 'mixtape' ORDER BY id DESC LIMIT 15");
        ResultSet result = statement.executeQuery();

        while(result.next())
        {
            System.out.println(result.getString(1) + " " + result.getString(2));
        }
    }
}

这是来自控制台的错误:

Exception in thread "main" java.sql.SQLException: Access denied for user 'kwinrow_admin'@'c-69-247-142-223.hsd1.tn.comcast.net' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1094)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4226)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1748)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2508)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2541)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2323)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Main.main(Main.java:19)

我做错了什么?我错过了什么

1 个答案:

答案 0 :(得分:0)

出于安全考虑,HostGator默认会拒绝您对数据库的连接请求。

您需要将您的IP地址添加到远程数据库访问主机列表中。

这是一个截图,以防你需要它:

enter image description here


您可以使用PDOmysqli作为api连接到您的数据库:

这是PDO中的一个例子:

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$query ='SELECT * FROM post_data WHERE type=:type ORDER BY id DESC LIMIT 15';
$stmt = $dbh->prepare($query);
if ($stmt->execute(array(':type'=>'mixtape'))) {
  $json = array();
  while ($row = $stmt->fetch()) {
    $json[] = $row;
  }
  echo json_encode($row);
}

我以 JSON 格式对数据进行了编码。

现在,您的应用程序负责向此脚本发出请求并解码JSON数据。