|
This lesson converts the application, applet, and servlet examples from Lesson 6 to write to and read from a database using JDBC. JDBC is the Java database connectivity application programming interface (API) available in the Java® 2 Platform software. The code for this lesson is very similar to the code you saw in Lesson 6, but additional steps (beyond converting the file access code to database access code) include setting up the environment, creating a database table, and connecting to the database. Creating a database table is a database administration task that is not part of your program code. However, establishing a database connection and the resulting database access are. As in Lesson 6, the applet needs appropriate permissions to connect to the database. Which permissions it needs varies with the type of driver used to make the database connection.
Database Setup You need access to a database if you want to run the examples in this lesson. You can install a database on your machine or perhaps you have access to a database at work. Either way, you need a database driver and any relevant environment settings so your program can load the driver and locate the database. The program will also need database login information in the form of a user name and password. A database driver is software that lets a program establish a connection with a database. If you do not have the right driver for the database to which you want to connect, your program will be unable to establish the connection. Drivers either come with the database or are available from the Web. If you install your own database, consult the documentation for the driver for information on installation and any other environment settings you need for your platform. If you are using a database at work, consult your database administrator for this information.
To show you two ways to do it, the application example uses
the Connections to other databases will involve similar steps and code. Be sure to consult your documentation or system administrator if you need help connecting to the database. Create Database Table Once you have access to a database, create a table in it for the examples in this lesson. You need a table with one text field for storing character data.
TABLE DBA (
TEXT varchar2(100),
primary key (TEXT)
)
Database Access by Applications
This example converts the FileIO
program from Lesson 6 to write data to and read data from
a database. The top window below appears when you start
the Dba application, and the window
beneath it appears when you click the
When you click the
When Application Starts
After Writing Orange and Apple to Database The database access application needs code to establish the database connection and do the database read and write operations. Establishing a Database Connection
The JDBC
The
The
The call to
Final and Private Variables
The member variables used to establish the database connection above are
declared
final:
A
private:
A Writing and Reading Data
In the write operation, a
SQL commands are
In the read operation, a
Database Access by Applets The applet version of the example is like the application code described above except for the standard differences between applications and applets described in the Structure and Elements section of Lesson 3. However, if you run the applet without a policy file, you get a stack trace indicating permission errors. The Granting Applets Permission section in Lesson 6 introduced you to policy files and how to launch an applet with the permission it needs. The Lesson 6 applet example provided the policy file and told you how to launch the applet with it. This lesson shows you how to read the stack trace to determine the permissions you need in a policy file. To keep things interesting, this lesson has two versions of the database access applet: one uses the JDBC driver, and the other uses the the JDBC-ODBC bridge with an Open DataBase Connectivity (ODBC) driver. Both applets do the same operations to the same database table using different drivers. Each applet has its own policy file with different permission lists and has different requirements for locating the database driver JDBC Driver The JDBC driver is used from a program written exclusively in the Java language (Java program). It converts JDBC calls directly into the protocol used by the DBMS. This type of driver is available from the DBMS vendor and is usually packaged with the DBMS software.
Starting the Applet:
To successfully run, the DbaAppl.java
applet needs an available database driver and a policy file.
This section walks through the steps to get everything set up.
Here is the
And here is how to start the applet with appletviewer: appletviewer DbaAppl.html
Locating the Database Driver:
Assuming the driver is not available to the cannot find driver This error means the DriverManager looked for the JDBC driver in the directory where the applet HTML and class files are and could not find it. To correct this error, copy the driver to the directory where the applet files are, and if the driver is bundled in a zip file, unzip the zip file so the applet can access the driver. Once you have the driver in place, launch the applet again. appletviewer DbaAppl.html
Reading a Stack Trace:
Assuming the driver is locally available to the applet, if the
DbaAppl.java applet
is launched without a policy file, the following stack trace
is generated when the end user clicks the java.security.AccessControlException: access denied (java.net.SocketPermission developer resolve)
The first line in the above stack trace tells you access is denied.
This means this stack trace was generated because the applet tried to
access a system resource without the proper permission.
The second line means to correct this condition
you need a You can use Policy tool to create the policy file you need, or you can create it with an ASCII editor. Here is the policy file with the permission indicated by the stack trace:
Run the applet again, this time with a policy file named
appletviewer -J-Djava.security.policy=DbaApplPol
DbaAppl.html
You get a stack trace again, but this time it is a different error condition. java.security.AccessControlException: access denied (java.net.SocketPermission 129.144.176.176:1521 connect,resolve)
Now you need a
Here is the
Run the applet again. If you use the above policy file with the Socket permissions indicated, it works just fine.
appletviewer -J-Djava.security.policy=DbaApplPol
DbaAppl.html
JDBC-ODBC Bridge with ODBC Driver Open DataBase Connectivity (ODBC) is Microsoft's programming interface for accessing a large number of relational databases on numerous platforms. The JDBC-ODBC bridge is built into the Solaris and Windows versions of the Java platform so you can do two things:
The
Start the Applet:
Here is the
And here is how to start the applet: appletviewer DbaOdb.html
Reading a Stack Trace:
If the DbaOdbAppl.java applet
is launched without a policy file, the following stack trace
is generated when the end user clicks the java.security.AccessControlException: access denied (java.lang.RuntimePermission accessClassInPackage.sun.jdbc.odbc )
The first line in the above stack trace tells you access is denied.
This means this stack trace was generated because the applet tried to
access a system resource without the proper permission. The second
line means you need a You can use Policy tool to create the policy file you need, or you can create it with an ASCII editor. Here is the policy file with the permission indicated by the stack trace:
grant {
permission java.lang.RuntimePermission
"accessClassInPackage.sun.jdbc.odbc";
};
Run the applet again, this time with a policy file named
appletviewer -J-Djava.security.policy=DbaOdbPol
DbaOdb.html
You get a stack trace again, but this time it is a different error condition. java.security.AccessControlException: access denied (java.lang.RuntimePermission file.encoding read)
The stack trace means the applet needs read permission to the encoded
(binary) file.
Here is the
Run the applet again. If you use the above policy file with the Runtime and Property permissions indicated, it works just fine.
appletviewer -J-Djava.security.policy=DbaOdbPol
DbaOdb.html
Database Access by Servlets
As you learned in Lesson 6, servlets are under the security
policy in force for the web server under which they run. When
the database read and write code is added to the
The web server has to be configured to locate the database. Consult your web server documentation or database administrator for help. With Java WebServer 1.1.1, the configuration setup involves editing the startup scripts with such things as environment settings for loading the ODBC driver, and locating and connecting to the database.
More Information You can find more information on variable access settings in the Objects and Classes trail in The Java Tutorial
_______ [TOP] | ||||||||||||||||
|
| ||||||||||||