's
built-in authentication mechanism is suitable only for development and testing
purposes. It is strongly recommended that production systems rely on LDAP or a
user-defined class for authentication. It is also strongly recommended that
production systems protect network connections with SSL/TLS.
This example uses one program to set properties and a second program to
perform database operations. A similar example that uses the embedded
driver, in
, is a single long
program. Either example would work equally well in the other format.
See
for an example similar to this one that uses SQL authorization.
The first program, AuthExampleClient1.java, does the
following:
- Creates a database named authClientDB, using the client
driver.
- Sets database properties that create users with different levels of access
(read-only and full access), require authentication, and set the default access
level to no access.
- Closes the connection and shuts down the database.
The second program, AuthExampleClient2.java, does the
following:
- Tries to connect to the database without a username and password, raising
an exception.
- Connects to the database as a user with read-only access; the connection
succeeds, but an attempt to create a table raises an exception.
- Connects to the database as a user with full access; this user can create
and populate a table.
- Removes the table.
- Closes the connection and shuts down the database.
Make sure that the javac command is in your path, then
compile the programs as follows:
javac AuthExampleClient1.java
javac AuthExampleClient2.java
Before you run the programs, start the
Network
Server as described in step 2 of "Activity 4: Create and run a JDBC program
using the client driver and Network Server" in
. When you run the
programs, make sure that
%DERBY_HOME%\lib\derbyclient.jar (or
$DERBY_HOME/lib/derbyclient.jar) is in your classpath. For
example, you might use the following commands on a Windows system:
java -cp .;%DERBY_HOME%\lib\derbyclient.jar AuthExampleClient1
java -cp .;%DERBY_HOME%\lib\derbyclient.jar AuthExampleClient2