![](/img/trans.png)
[英]Java SSL connection solution when Certificate Authority not in JRE
[英]Java MySQL Connection with SSL Authority Certificate
我正在嘗試使用Java Rest WebService
連接到JDBC
。
首先,我使用用戶名和密碼以及一個額外的細節使用MySQL Workbench成功連接,我需要添加“用於SSL的證書頒發機構文件的路徑”並工作,我能夠連接和使用。我具有使用遠程數據庫的證書。
現在,我嘗試使用Java進行連接,而這就是我嘗試的方式:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConexaoBD {
// Define o Metodo de Conexao quando a classe � chamada
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public Connection obtemConexao() throws SQLException {
System.getProperties().setProperty("javax.net.ssl.trustStore","/mysqlcertificate.pem");
return DriverManager.getConnection("jdbc:mysql://myHost", "myUsername", "myPassword");
}
}
我研究了URL中的useuse == true,但沒有起作用,或者這個system.getproperties
,我還在項目包和類包中添加了.pem
文件(因為我不知道我需要的位置是什么放置文件)。
我應該怎么做才能連接? 如果沒有此SSL,數據庫將拒絕我的連接,而我試圖以這種方式連接另一個類:
Connection conn = null;
Statement stm = null;
try {
ConexaoBD bd = new ConexaoBD();
conn = bd.obtemConexao();
誰能幫我?
我的情況是相似的,但不相同。 我可以從工作台連接到MySQL,但不能從我的代碼連接到MySQL。 如果可用,在工作台中選擇的唯一選項是useSSL。 在這種情況下,以下代碼對我有用。
private String db_server = BaseMethods.getSystemData("db_server");
private String db_user = BaseMethods.getSystemData("db_user");
private String db_password = BaseMethods.getSystemData("db_password");
private void connectToDb() throws Exception {
String jdbcDriver = "com.mysql.jdbc.Driver";
String dbUrl = "jdbc:mysql://" + db_server +
"?verifyServerCertificate=false" +
"&useSSL=true" +
"&requireSSL=true";
System.setProperty(jdbcDriver, "");
Class.forName(jdbcDriver).newInstance();
Connection conn = DriverManager.getConnection(dbUrl, db_user, db_password);
Statement statement = conn.createStatement();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.