如何使用Android连接到MySQL的SSL连接?

时间:2015-03-15 07:34:06

标签: java android mysql ssl

我想使用SSL连接到MySQL数据库,但我不明白如何使用SSL密钥库下面的代码连接到数据库表单Android设备,我使用(非SSL)连接数据库成功,收集数据没有错误,请帮助,谢谢!

KeyStore trustSt = KeyStore.getInstance("BKS");
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
InputStream trustStoreStream = this.getResources().openRawResource(R.raw.truststore);
trustSt.load(trustStoreStream, "123456".toCharArray());
trustManagerFactory.init(trustSt);

KeyStore keyStore = KeyStore.getInstance("BKS");
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
InputStream keyStoreStream = this.getResources().openRawResource(R.raw.keystore);
keyStore.load(keyStoreStream, "123456".toCharArray());
keyManagerFactory.init(keyStore, "123456".toCharArray());

sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);


Connection con;

try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx:3306/db?verifyServerCertificate=true&useSSL=true&requireSSL=true&",db_user,db_password);

}catch(Exception e) {
    e.printStackTrace();
    tv.setText(e.toString());
}

0 个答案:

没有答案