我正在尝试使用jdbc连接到openshift上的远程数据库。 这是我的代码 -
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)
任何人都可以帮我解决这个问题。 我正确地提供了所有凭据,即便如此它也说Access Denied。
答案 0 :(得分:0)
MySQL可能只设置为接受本地主机上的连接。您是否可以更新grant语句以使用'user @%'
答案 1 :(得分: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);
像这样使用它.Openhift不会在静态IP上中继,所以他们不会通过该ip或localhost接受你的连接。此外,如果您在本地服务器上尝试它而不是在openshift上部署它,您必须使用ssh令牌进行连接,我认为这是不允许的。