我嘗試使用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)
誰能幫我解決這個問題。 我正確地給所有憑據,即使它說拒絕訪問。