Menu

how to connect java with an encrypted access database(password)

Help
2015-07-15
2015-07-16
  • hamreen ahmad

    hamreen ahmad - 2015-07-15

    I've developed a java application(a dictionary) with an Access database to store the words of the dictionary and I'm getting ready to distribute it. but i want to encrypt my database with a password to prevent people to access my words. when i set a passwords the java code shows this Exception

    net.ucanaccess.jdbc.UcanaccessSQLException: Decoding not supported. Please choose a CodecProvider which supports reading the current database encoding.
    at net.ucanaccess.jdbc.UcanaccessDriver.connect(UcanaccessDriver.java:247)

    here is my connection code before encryption of my database with password ....

    String s1="jdbc:ucanaccess://";
    String user="";
    String pass="";
    String s4="words.accdb";

    public void connectToDB(){
    //database connection
    try {
    conn = DriverManager.getConnection(s1+s4,user,pass);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    //end of database connection
    }

    here is the code after the encryption with password for example 12345...

    String s1="jdbc:ucanaccess://";
    String user="";
    String pass="12345";
    String s4="words.accdb";

    public void connectToDB(){
    //database connection
    try {
    conn = DriverManager.getConnection(s1+s4,user,pass);
    } catch (SQLException e) {
    e.printStackTrace();
    }
    //end of database connection
    }

    can some one help me what should i do?????????

    thanks for all................................

     

    Last edit: hamreen ahmad 2015-07-15
  • Marco Amadei

    Marco Amadei - 2015-07-15

    Please, follow the steps described in the ucanaccess website http://ucanaccess.sourceforge.net/site.html, tab "Getting Started"
    connection parameter JackcessOpener.

     
    • hamreen ahmad

      hamreen ahmad - 2015-07-15

      thanks i saw the following code there
      may be this was the code that i need
      can you explain something in this code such as what is ///opt/prova1.mny; and where should i call that open methode????

      Example for UCanAccess2. Notice that you must use UCanAccess 2.x.x with jackcess-encrypt-2.x.x and all related dependecies
      package yourPackage.example;
      import java.io.File;
      import net.ucanaccess.jdbc.JackcessOpenerInterface;
      //imports from Jackcess Encrypt
      import com.healthmarketscience.jackcess.CryptCodecProvider;
      import com.healthmarketscience.jackcess.Database;
      import com.healthmarketscience.jackcess.DatabaseBuilder;
      import net.ucanaccess.jdbc.JackcessOpenerInterface;
      public class CryptCodecOpener implements JackcessOpenerInterface {
      public Database open(File fl,String pwd) throws IOException {
      DatabaseBuilder dbd =new DatabaseBuilder(fl);
      dbd.setAutoSync(false);
      dbd.setCodecProvider(new CryptCodecProvider(pwd));
      dbd.setReadOnly(false);
      return dbd.open();
      }

      //Notice that the parameter setting autosync =true is recommended with UCanAccess for performance reasons.
      //UCanAccess flushes the updates to disk at transaction end.
      //For more details about autosync parameter (and related tradeoff), see the Jackcess documentation.
      }
      Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
      Connection conn=
      DriverManager.getConnection("jdbc:ucanaccess:///opt/prova1.mny;jackcessOpener=yourPackage.example.CryptCodecOpener", "sa", pwd);
      ...

       

      Last edit: hamreen ahmad 2015-07-15
  • hamreen ahmad

    hamreen ahmad - 2015-07-15

    ...................

     

    Last edit: hamreen ahmad 2015-07-15
  • Marco Amadei

    Marco Amadei - 2015-07-15

    You shouldn't, you have just to pass the class which implements the JackcessOpenerInterface in the jdbc url as explained above; opt/prova1.mny is a db path on linux.

     
    • hamreen ahmad

      hamreen ahmad - 2015-07-15

      can you help me more please now my code is that

      ///.....imports

      public class Dictionary extends JFrame implements JackcessOpenerInterface{

      connection conn;
      ///............ more code

      @Override
      public Database open(File fl,String pwd) throws IOException {
      DatabaseBuilder dbd =new DatabaseBuilder(fl);
      dbd.setAutoSync(false);
      dbd.setCodecProvider(new CryptCodecProvider(pwd));
      dbd.setReadOnly(false);
      return dbd.open();
      }

      public void connectToDB(){
      try {
      open(new File("words.accdb"),"12345");
      //database connection
      Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
      conn=DriverManager.getConnection("jdbc:ucanaccess://words.accdb","","12345");

             } catch (Exception e) {
      
             }
      
      }//end of connectToDB
      
      //more code and main methode.....
      

      }//end of class

       

      Last edit: hamreen ahmad 2015-07-15
  • Marco Amadei

    Marco Amadei - 2015-07-16

    Okey, you're close to the solution.
    NOT
    conn=DriverManager.getConnection("jdbc:ucanaccess://words.accdb;","","12345");
    BUT
    DriverManager.getConnection("jdbc:ucanaccess://words.accdb;jackcessOpener=<package of="" Dictionary="">.Dictionary", "", "12345");

    replace <package of="" Dictionary=""> with the complete package of the Dictionary class e.g.
    com.company.prj.Dictionary.
    Notice that words.accdb may be replaced with the full path of the database.
    I've also answered on stackoverflow.

     

    Last edit: Marco Amadei 2015-07-16

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.