我希望使用jdbc连接到最近通过ssl保护的远程mysql数据库。我找到了一个简单的示例java程序来测试连接。连接失败并抱怨无法找到密钥库文件。我验证密钥库确实是我在代码中所说的位置。至少我想我做到了。测试应用程序如下所示:
import java.io.File;
import java.sql.*;
public class TestMySQLSSL {
public static void main (String[] args)
{
Connection con = null;
System.getProperties().setProperty("javax.net.debug","all");
System.getProperties().setProperty("javax.net.ssl.keyStore","c:\\LiferayStuff\\bundles\\liferay-portal-6.0.6\\tomcat-6.0.29\\jrel.6.0_20\\keystore");
System.getProperties().setProperty("javax.net.ssl.keyStorePassword","####");
System.getProperties().setProperty("javax.net.ssl.trustStore","c:\\LiferayStuff\\bundles\\liferay-portal-6.0.6\\tomcat-6.0.29\\jrel.6.0_20\\truststore");
System.getProperties().setProperty("javax.net.ssl.trustStorePassword","####");
try
{
String url = "jdbc:mysql://xxx.xxx.xxx.xxx:3306/isc"+
"?verifyServerCertificate=true"+
"&useSSL=true"+
"&requireSSL=true";
String user = "*******";
String password = "******";
Class dbDriver = Class.forName("com.mysql.jdbc.Driver");
boolean filelives;
filelives = new File("c:/LiferayStuff/bundles/liferay-portal-6.0.6/tomcat-6.0.29/jre1.6.0_20/keystore").exists();
System.out.println("keystore " + filelives);
con = DriverManager.getConnection(url, user, password);
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
if (con != null)
{
try
{
con.close();
}
catch (Exception e){}
}
}
}
}
我得到的输出的第一位看起来像这样:
keystore true
keyStore is : c:/LiferayStuff/bundles/liferay-portal-6.0.6/tomcat-6.0.29/jrel.6.0_20/keystore
keyStore type is : jks
keyStore provider is :
default context init failed:java.security.PrivilegedActionException:java.io.FileNotFoundException: c:\LiferayStuff\bundles\liferay-portal-6.0.6\tomcat-6.0.29\jrel.6.0_20\keystore (The system cannot find the path specified)
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlyingexception:
** BEGIN NESTED EXCEPTION **
com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
密钥库文件存在,但我怀疑它可能有问题。我在Windows上运行应用程序。该文件是否存在权限问题?任何帮助,将不胜感激。
此致 Dave Semeraro
答案 0 :(得分:1)
事实证明,数据库管理员已经阻止了除服务器之外的所有IP访问。一旦我的IP被添加,我就可以获得上面的代码了。很抱歉浪费每个人的时间。