Search code examples
java-memidp

one time password in j2me application


I am working on an j2me application, where the users can set password only once and it should be the password through out the application life.

It is not password for each user, it is password of application, where I need to store only once.

How to set this password using database?


Solution

  • Will your database be on the phone? If so, you can use a RecordStore. A good article on it is http://developers.sun.com/mobility/midp/articles/databasemap/

    You can use a Simple Object Mapping to store your users login and password like:

    
        class User {
          private String login, password;
    
          // ... constructors, setters and getters
    
          public byte[] toByteArray() throws IOException {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream( bout );
    
            dout.writeUTF( login );
            dout.writeUTF( password );
    
            dout.close();
    
            return bout.toByteArray();
          }
    
          // fromByteArray method
        }
    
    

    For each new user you add a new entry to the RecordStore, but never changes or delete content from the RecordStore.

    Update after comments.

    You can use another Simple Object Mapping to store your application password like:

    
        class ApplicationPassword {
          private String password;
    
          // ... constructors, setter and getter
    
          public byte[] toByteArray() throws IOException {
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            DataOutputStream dout = new DataOutputStream( bout );
    
            dout.writeUTF( password );
    
            dout.close();
    
            return bout.toByteArray();
          }
    
          // fromByteArray method
        }
    
    

    You must write the returned byte array at your RecordStore only if the recordstore was just created.