我已經創建了一個.jar文件,裏面有我簽名的數字證書,我想在我的項目中使用該文件,我目前如何使用它在& JAVA_HOME/jre7/lib/security文件夾中。如何在我的項目中使用可執行JAR文件?
感謝您的幫助提前:)。
我已經創建了一個.jar文件,裏面有我簽名的數字證書,我想在我的項目中使用該文件,我目前如何使用它在& JAVA_HOME/jre7/lib/security文件夾中。如何在我的項目中使用可執行JAR文件?
感謝您的幫助提前:)。
如果JAR在你的類路徑中,該文件是 「foo.cert」 坐在根,只是這樣做......
InputStream is = this.getClass().getResourceAsStream("/foo.cert");
CertificateFactory factory = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) factory.generateCertificate(is);
爲什麼我得到這個錯誤?'code'Caused by:java.security.InvalidAlgorithmParameterException:trustAnchors參數必須是非空的 \t at java.security.cert.PKIXParameters。 setTrustAnchors(Unknown Source) \t at java.security.cert.PKIXParameters。
已編輯答案,您需要提供更多信息,您是否試圖加載509證書?見上面,試試 – raffian
我試圖用API和Ssl安全連接 – anuj
static Connection DBConnectionstring()
{
Connection con = null;
String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=My-PC; password=; integratedSecurity=true; encrypt=true; trustStore=truststore.jks;trustStorePassword=pass; hostNameInCertificate=certificatekey;";
try
{
con = DriverManager.getConnection(conUrl);
JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}//connection string method ends
,這裏是我有連接字符串和其他sql命令的類
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.net.ssl.*;
import java.io.*;
import java.net.*;
import java.security.*;
import javax.swing.JOptionPane;
public abstract class ConnectionDB
{
public static void main(String[] args)
{
try {
insertRecordIntoDbUserTable();
selectfromdb();
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
}
private static void GuiContain()
{
}
private static void insertRecordIntoDbUserTable() throws SQLException
{
Connection con = null;
Statement statement = null;
String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('username','pass1')";
try
{
con = DBConnectionstring();
statement = con.createStatement();
System.out.println(insertTableSQL);
statement.executeUpdate(insertTableSQL);
//System.out.println("Record's inserted into Login Details table!");
JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
if (statement != null)
{
statement.close();
//System.out.println("XXXXX...Statement is terminated..XXXXX");
JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING_MESSAGE);
}
if (con != null)
{
con.close();
//System.out.println("Connection is Closed!!..");
JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING_MESSAGE);
}
}
}
private static void selectfromdb() throws SQLException
{
Statement stmt = DBConnectionstring().createStatement();
ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
while (rs.next())
{
String lastName = rs.getString("Username");
String Pass = rs.getString("Password");
System.out.println(lastName + "" + Pass + "\n");
}
}
static Connection DBConnectionstring()
{
Connection con = null;
String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=my-PC; password=; integratedSecurity=true;";
try
{
con = DriverManager.getConnection(conUrl);
JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING_MESSAGE);
}
catch (SQLException e)
{
e.printStackTrace();
}
return con;
}
}
對不起@Raffian先前沒有足夠的材料,現在我已經更新了我的。我希望這有助於並告訴我你需要什麼其他信息。 – anuj
想讀取位於JAR中的文件嗎? – raffian
是的,我想讀取JAR中的內容 – anuj