-1

I have created A .jar file which has my signed digital certificate inside it and I want to use that file in my project,how can I use it currently it's in &JAVA_HOME/jre7/lib/security folder.

Thanks for your help in advance :) .

anuj
  • 7
  • 1
  • 1
  • 5

2 Answers2

0

If the JAR is on your classpath, and the file is "foo.cert" sitting at the root, just do this...

InputStream is = this.getClass().getResourceAsStream("/foo.cert");
CertificateFactory factory = CertificateFactory.getInstance("X.509");  
X509Certificate cert = (X509Certificate) factory.generateCertificate( is);  

https://stackoverflow.com/a/4548791/791406

Community
  • 1
  • 1
raffian
  • 31,267
  • 26
  • 103
  • 174
  • why I'm getting this error?'code'Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty at java.security.cert.PKIXParameters.setTrustAnchors(Unknown Source) at java.security.cert.PKIXParameters.(Unknown Source) at java.security.cert.PKIXBuilderParameters.(Unknown Source) ... 61 more – anuj May 31 '13 at 12:55
  • edited answer, you need to provide more info, are you trying to load a 509 cert? see above, try that – raffian May 31 '13 at 13:35
  • I'm trying to connect using API and Ssl security – anuj May 31 '13 at 16:21
  • That's a totally different issue. You're not providing sufficient details on what you're doing but you expect people to help, it's just not possible. – raffian May 31 '13 at 16:24
0
  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 

And here is the class where I'm having connection string and other sql commands

    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;

    }


    }
anuj
  • 7
  • 1
  • 1
  • 5
  • Sorry @Raffian earlier for the un sufficient material now I have updated wht I have .I hope this helps and tell me what other information you needed . – anuj Jun 01 '13 at 08:22