我最近在Amazon RDS上部署了一个数据库实例,我正试图抓住它。按照文档上的说明操作后,我尝试连接到该数据库实例,但出于某种原因,显示服务器版本的简单程序挂起在连接上。
这是我的代码:
import java.sql.*;
public class AWSTest {
public static void main(String[] args) {
System.out.println(getVersion());
}
public static String getVersion() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("Driver Error: " + e.getMessage());
return VERSION_NOT_FOUND;
}
System.out.println(CONNECTING_MESSAGE);
try (Connection con = DriverManager.getConnection(DB_URL,
USER,
PASS);
Statement stmt = con.createStatement();) {
System.out.println("Getting server version...");
try (ResultSet rs = stmt.executeQuery(VERSION_QUERY);) {
rs.next();
return rs.getString(1);
}
} catch (SQLException se) {
System.out.println("SQL Error: " + se.getErrorCode() + " "
+ se.getMessage());
return VERSION_NOT_FOUND;
}
}
static final String DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://**************.*******."
+ "*******.rds.amazonaws.com:3306";
private static final String VERSION_QUERY = "Select VERSION()";
static final String USER = "*******";
static final String PASS = "*******";
private static final String VERSION_NOT_FOUND = "Version was not found";
public static final String GETTING_DRIVER_MESSAGE = "Getting driver...";
public static final String CONNECTING_MESSAGE = "Connecting to server...";
}
我的程序挂在Connection con = DriverManager.getConnection(DB_URL, USER, PASS);
行,我的主要问题是它甚至没有抛出异常,只是停留在那里。
USER
,PASS
和DB_URL
绝对正确。
答案 0 :(得分:3)
听起来你的ip / port被阻止/丢弃了。通常情况下,连接什么也不做(与拒绝登录或登录失败相反)。确保您的安全组已正确设置。 http://aws.amazon.com/rds/faqs/#31