简体   繁体   中英

error when connecting to remote MySQL (host gator) server using JDBC

I am using Eclipse and I am trying to connect to my websites mysql database thru Host Gator. This is the code I have so far in Eclipse in my Main.java file.

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));
        }
    }
}

This is the error from the console:

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)

What am I doing wrong? What am I am I missing

For security purposes HostGator will deny your connection request to the database by default.

You need to add your ip address to the list Remote Database Access Hosts.

here is a screenshot just in case you need it:

在此处输入图片说明


You can use PDO or mysqli as an api to connect to your Database:

Here is an example in 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);
}

I encoded the data in JSON format.

Now your app is responsible for making a request to this script and the decode the JSON data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM