SSL DB2 连接失败
SSL DB2 connection failed
我正在尝试在 IBM Bluemix 上使用 SSL 连接到 DB2 数据库。
当我第一次尝试不使用 SSL 进行连接时,它不起作用。阅读文档后,我意识到它连接到启用了 SSL 的数据库。
我尝试使用以下代码连接到数据库:
public boolean connect() {
try {
String url = "jdbc:db2://" + serverName + ":" + port + "/" + dbName+
":securityMechanism=9";
connection = DriverManager.getConnection(url, userName, passWord);
st = connection.createStatement();
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
}
return false;
}
我仍然不太确定如何使用上面代码提供的 SSL 证书。
我尝试搜索示例,但大多数解释要么不清楚,要么用于其他数据库系统。
根据 SQLDB documentation,如果您使用最新的 com.ibm.db2.jcc.DB2Driver 和 JDBC连接,目前的SSL证书是和驱动捆绑在一起的,不需要手动安装。
以下代码段向您展示了如何使用 VCAP_SERVICES 提供的连接详细信息通过 SSL 连接到 SQLDB。
public class SSLTEST {
/**
* @param args
*/
public static void main(String[] args) {
String ServerName = "hostname or IP address";
int PortNumber = 50001;
String DatabaseName = "SQLDB";
String user = "your_user_id_from_VCAP_SERVICES";
String userPassword = "your_password_from_VCAP_SERVICES";
java.util.Properties properties = new java.util.Properties();
properties.put("user", "user ID that has access to SQLDB");
properties.put("password", "password for the user ID that has access to SQLDB");
properties.put("sslConnection", "true");
String url = "jdbc:db2://" + ServerName + ":"+ PortNumber + "/" +
DatabaseName + ":" + traceFileLocation + ";";
java.sql.Connection con = null;
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
}
catch ( Exception e )
{
System.out.println("Error: failed to load Db2 jcc driver.");
}
try
{
System.out.println("url: " + url);
con = java.sql.DriverManager.getConnection(url, properties);
if (con != null) {
System.out.println("Success");
} else {
System.out.println("Failed to make the connection");
}
con.close();
}
catch (Exception e)
{
if (con != null) {
try {
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
e.printStackTrace();
}
}
如果您使用的是 Liberty,则会为您生成一个数据源,您可以使用 jndi 进行查找。
@Resource(lookup = "jdbc/mydb")
private DataSource myDataSource;
Connection c = myDataSource.getConnection();
"mydb" 是 SQLDB 服务的名称
https://developer.ibm.com/bluemix/2014/02/07/java-db2-10-minutes/
最后我使用数据源连接到数据库。
上下文 ic = new InitialContext();
数据源 db = (数据源)context.lookup("jdbc/MyDatabase");
我正在尝试在 IBM Bluemix 上使用 SSL 连接到 DB2 数据库。
当我第一次尝试不使用 SSL 进行连接时,它不起作用。阅读文档后,我意识到它连接到启用了 SSL 的数据库。
我尝试使用以下代码连接到数据库:
public boolean connect() {
try {
String url = "jdbc:db2://" + serverName + ":" + port + "/" + dbName+
":securityMechanism=9";
connection = DriverManager.getConnection(url, userName, passWord);
st = connection.createStatement();
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
}
return false;
}
我仍然不太确定如何使用上面代码提供的 SSL 证书。
我尝试搜索示例,但大多数解释要么不清楚,要么用于其他数据库系统。
根据 SQLDB documentation,如果您使用最新的 com.ibm.db2.jcc.DB2Driver 和 JDBC连接,目前的SSL证书是和驱动捆绑在一起的,不需要手动安装。
以下代码段向您展示了如何使用 VCAP_SERVICES 提供的连接详细信息通过 SSL 连接到 SQLDB。
public class SSLTEST {
/**
* @param args
*/
public static void main(String[] args) {
String ServerName = "hostname or IP address";
int PortNumber = 50001;
String DatabaseName = "SQLDB";
String user = "your_user_id_from_VCAP_SERVICES";
String userPassword = "your_password_from_VCAP_SERVICES";
java.util.Properties properties = new java.util.Properties();
properties.put("user", "user ID that has access to SQLDB");
properties.put("password", "password for the user ID that has access to SQLDB");
properties.put("sslConnection", "true");
String url = "jdbc:db2://" + ServerName + ":"+ PortNumber + "/" +
DatabaseName + ":" + traceFileLocation + ";";
java.sql.Connection con = null;
try
{
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
}
catch ( Exception e )
{
System.out.println("Error: failed to load Db2 jcc driver.");
}
try
{
System.out.println("url: " + url);
con = java.sql.DriverManager.getConnection(url, properties);
if (con != null) {
System.out.println("Success");
} else {
System.out.println("Failed to make the connection");
}
con.close();
}
catch (Exception e)
{
if (con != null) {
try {
con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
e.printStackTrace();
}
}
如果您使用的是 Liberty,则会为您生成一个数据源,您可以使用 jndi 进行查找。
@Resource(lookup = "jdbc/mydb")
private DataSource myDataSource;
Connection c = myDataSource.getConnection();
"mydb" 是 SQLDB 服务的名称
https://developer.ibm.com/bluemix/2014/02/07/java-db2-10-minutes/
最后我使用数据源连接到数据库。 上下文 ic = new InitialContext(); 数据源 db = (数据源)context.lookup("jdbc/MyDatabase");