2016-03-05 72 views
0

我嘗試使用jdbc連接到openshift上的遠程數據庫。 這裏是我下面的代碼 -無法使用jdbc遠程連接到OpenShift mysql

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.HashMap; 

public class Conn { 
    public static String url  = "jdbc:mysql://127.9.77.2:3306/mysql"; 
    public static String user = "adminSKr7Tbz"; 
    public static String password = "h1xha****"; 

    public static void main(String[] args) throws ClassNotFoundException, SQLException { 
     // TODO Auto-generated method stub 
     Class.forName("com.mysql.jdbc.Driver"); 
     Connection conn = DriverManager.getConnection(url,user,password); 
     System.out.println("Connecting to database..."); 
     Statement stmt = conn.createStatement(); 
     ResultSet rs = stmt.executeQuery("select * from country LIMIT 10;"); 
     while (rs.next()) { 
      HashMap map = new HashMap(); 
      String name = rs.getString("Name"); 
      Integer col  = Integer.parseInt(rs.getString("Area")); 
      map.put("name",name); 
      map.put("value",col); 
      System.out.println(map); 

     } 
    } 
} 

我碰到下面的錯誤 -

Exception in thread "main" java.sql.SQLException: Access denied for user 'adminSKr7Tbz'@'localhost' (using password: YES) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:894) 
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3808) 
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1256) 
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032) 
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729) 
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) 
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302) 
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283) 
    at java.sql.DriverManager.getConnection(DriverManager.java:571) 
    at java.sql.DriverManager.getConnection(DriverManager.java:215) 
    at visualize.Conn.main(Conn.java:18) 

誰能幫我解決這個問題。 我正確地給所有憑據,即使它說拒絕訪問。

回答

0

MySQL可能只設置爲接受本地主機上的連接。您可以更新GRANT語句使用「用戶名@%」

0
String USERNAME = System.getEnv("OPENSHIFT_MYSQL_DB_USERNAME"); 
String PASSWORD = System.getEnv("OPENSHIFT_MYSQL_DB_PASSWORD"); 
String DB_NAME = System.getEnv("OPENSHIFT_APP_NAME"); 
String FORNAME_URL = "com.mysql.jdbc.Driver"; 
String URL = "jdbc:"+System.getEnv("OPENSHIFT_MYSQL_DB_URL")+ DB_NAME; 
Connection m_connection = DriverManager.getConnection(URL , USERNAME , PASSWORD); 

使用它像this.Openshift不會靜態IP中繼,因此他們不會接受通過該IP或本地主機的連接。此外,如果您在本地服務器上嘗試它,而不是在openshift上部署它,則必須使用ssh標記進行連接,並且我認爲這不被允許。