O'Reilly logo

Ivor Horton's Beginning Java™ 2, JDK™ 5th Edition by Ivor Horton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

25.7. Browsing a Database

It's time you put together another "proper" application with a decent GUI. As a final example on JDBC operations, you'll put together an application that will enable you to browse any relational database for which you have a JDBC driver available—plus the necessary authority to get at the data, of course. Along the way, you'll learn a bit more about how you can get hold of metadata for a database—you'll need that to start the browsing process off. You'll also explore some new components that will be useful in this context, as well as discover a few wrinkles about some that you are already familiar with.

Let's start by deciding the basic appearance of the application window. You'll need to provide for an input area where the database URL, user ID, and password can be entered. These will basically be single-line text input fields, so JTextField objects will do nicely. You can use JLabel objects to annotate the entry areas. The password entry ought to have some protection from prying eyes though, and you can provide this by using the JPasswordField class, which happens to be a subclass of JTextField. The main feature of a JPasswordField object is that the input data is displayed as asterisks, so you can't read what was typed.

You need a regular application window so you can derive the application class from JFrame. It will be convenient to locate all the input at the top of the window, and you can arrange the fields and their labels quite easily using

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required