Contents Index C++ API user authentication example Sharing MobiLink and UltraLite user IDs pdf/preface.pdf

UltraLite User's Guide
  Developing UltraLite Applications
    Adding user authentication to your application
      Managing user IDs and passwords

Java user authentication example

The following code fragment performs user management and authentication for an UltraLite Java application.

A complete sample can be found in the Samples\UltraLite\javaauth subdirectory of your SQL Anywhere directory. The code below is based on that in Samples\UltraLite\javaauth\Sample.java.

JdbcSupport.enableUserAuthentication();
// Create database environment
java.util.Properties p = new java.util.Properties();
p.put( "persist", "file" );
SampleDB db = new SampleDB( p );

// Get new user ID and password
try{ 
   conn = db.connect( "dba", "sql" );
   // Set user ID and password 
   // a real application would prompt the user.
   uid = "50";
   pwd = "pwd50";

   db.grant( uid, pwd );
   db.revoke( "dba" );
   conn.close();
} 
catch( SQLException e ){ 
   // dba connection failed - prompt for user ID and password
   uid = "50";
   pwd = "pwd50";
}

// Connect
conn = db.connect( uid, pwd );

The code carries out the following tasks:

  1. Opening the database object.

  2. Attempt to connect using the default user ID and password.

  3. If the connection attempt is successful, add a new user.

  4. Delete the default user from the UltraLite database.

  5. Disconnect. An updated user ID and password is now added to the database.

  6. Connect using the updated user ID and password.

For more information, see GrantConnectTo method, and RevokeConnectFrom method.


Contents Index C++ API user authentication example Sharing MobiLink and UltraLite user IDs pdf/preface.pdf