UltraLite User's Guide
Developing UltraLite Applications
Adding user authentication to your application
Managing user IDs and passwords
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:
Opening the database object.
Attempt to connect using the default user ID and password.
If the connection attempt is successful, add a new user.
Delete the default user from the UltraLite database.
Disconnect. An updated user ID and password is now added to the database.
Connect using the updated user ID and password.
For more information, see GrantConnectTo method, and RevokeConnectFrom method.