![]() Technology Fundamentals Welcome to the Java Developer Connection Java Technology Fundamentals Newsletter. This monthly newsletter provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to the JDC's New to Java Programming Center. CONTENTS
1. Java Programming Language Basics Connecting to a Database Using JDBC Technology
2. Making Sense of the Java Class Libraries
Interfaces Connection and DatabaseMetaData, and the
MetaTest Application
4. Java Bits JDBC and JDO
5. New to the Programming Center Building an Application, Part 4 Read articles, Tech Tips, trails, and tutorials that provide more information on the topics discussed here. Java Programming Language BasicsConnecting to a Database Using JDBC TechnologyJDBC is the library from the Java 2 Standard Edition (J2SE), used to communicate with SQL-based databases. To follow this article, you'll need access to a database. If you don't have one, MySQL is free, open source, and it works in many different operating environments. See For More Information below for links to download MySQL.
In addition to having a working database, you need a JDBC driver. While JDBC is designed to work with just about all databases, you need a driver that works with your specific database. Drivers come in many varieties, including some that work with multiple databases, and can be located through JDBC Technology - Drivers. See For More Information below for link. Essentially, the driver maps JDBC commands to the database you are using. If you happen to be using MySQL, you can get a MySQL driver from Sourceforge.net or MySQL.org.
With a database server running and a JDBC driver available, you
can work with the JDBC libraries. As with any classes, make the
JDBC driver classes available to the compiler and runtime
environment. Configure the CLASSPATH environment variable or copy
the appropriate JAR file with the classes to the
Once you've set up the environment for your database and driver, you can start coding. There are two tasks involved in getting started with JDBC: Step 1: Load the necessary JDBC driver. Just making the classes available to the runtime environment isn't sufficient. Step 2: Connect to the database. Connecting involves identifying the specific data source. Based on that data source, the appropriate driver is used.
As described below, there is more than one way to perform the tasks outlined in these two steps. Step 1: Loading the JDBC Driver
In their simplest sense, JDBC drivers are just classes. They must
implement the The first possible way to load a JDBC driver: Create an actual instance of the class. After identifying your database and finding the specific driver you want to use, you can hard code a call to the constructor.
For instance, with Driver d = new org.gjt.mm.mysql.Driver(); This technique works fine to load the driver, but it does have drawbacks. While JDBC is database-agnostic, you are requiring the user of your application to use MySQL. If the user happens to be an Oracle or Sybase user, well, too bad. They must still get MySQL to run your program.
Another problem is that by explicitly creating an instance of the
driver, you are actually causing two instances to be created. As
previously stated, the driver is required to register itself with
the The second possible way to load a JDBC driver:
To avoid creating two instances of the driver, pass the class name
as a string to the
Class.forName("org.gjt.mm.mysql.Driver");
In both cases above, you are hard coding the name of the database driver, but the second way is preferable. Since the driver is loaded dynamically at runtime, the classes for the driver do not have to be made available at compile time, and you don't have to hard code a string there. What you can do is get the driver name from the command line or elsewhere and pass it through. That way, you can test with MySQL on your desktop, and your customer can use Oracle on their server, without source code changes or recompilations. String driver = args[0]; Class.forName(driver); The third way to load a JDBC driver:
The first two loading techniques require you to pass the driver
names in from somewhere. There's a third way to load the driver
that doesn't require any source code. Specify the driver, or set
of drivers, by setting the java -Djdbc.drivers=org.gjt.mm.mysql.Driver ProgName
Now the driver is automatically available to the Step 2: Connecting to the Database
Data sources are identified by a URI which begins with "
In the case of the MySQL driver, the string is quite aptly " jdbc:mysql://[hostname][:port]/[dbname][?param1=value1][¶m2=value2]..... More specifically, if the host you want to connect to is your own machine, the database name is test, and you don't want to provide a username and password, the final string would look as follows: jdbc:mysql://localhost/test
Once you have the appropriate URI necessary to identify
the driver and let the driver connect to the data source, it's time
to make the actual connection to the source. The String uri = "jdbc:mysql://localhost/test"; Connection con = DriverManager.getConnection(uri, username, password); The name of the subprotocol, mysql here, is specified in the documentation for your JDBC driver. Once you've made a connection to your data source, you're ready to access it. There is, however, a second way to identify and connect to a data source: data sources can be acquired through the Java Naming and Directory Interface (JNDI) naming service.
When using JNDI to make a connection, instead of using
Once you have the connection, you access the database in the same
way as the You'll know when you have a JNDI environment available; it usually isn't set up to only look up a JDBC source.
To try out both ways of connecting to the database, MySQL comes
with a test suite. Look in the test suite package for the
statements and Test your knowledge about connecting to a database with JDBC with this online quiz. Making Sense of the Java Class LibrariesInterfaces
|
Methods to access database information are available through the
DatabaseMetaData associated with the connection.
See a possible solution to Challenge.
JDO (Java Data Objects) is an API for transparent database access. A programmer can write code using the Java programming language that transparently accesses the underlying data store, without using database-specific code.
The JDO and JDBC APIs are complementary approaches. These technologies have unique strengths and can be used by programmers with different skill sets and development objectives.
JDBC API provides greater flexibility by giving programmers direct control over database access and cache management. The JDBC API is a more mature technology, and is broadly accepted as a complete API.
JDO provides programmer convenience and compile-time type checking. In addition, JDO hides SQL from the programmer so that learning SQL is not necessary.
The JDO API is being created under the Java Community Process.
Learn how to create scroll bars, pop-up option boxes, and how to read from and write to files.
Building an Application, Part 4
Java Naming and Directory Interface (JNDI)
New to Java Programming Center
Building an Application Introduction
See one possible solution to the June Program Challenge.
For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the J2SE development kit.
IMPORTANT: Please read our Terms of Use and Privacy policies:
Copyright 2002 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA
Tell us what you think of this supplement.
Have a question about programming? Use Java Online Support.
- Note -
Sun respects your online time and privacy. The Java Developer Connection mailing lists are used for internal Sun Microsystems purposes only. You have received this email because you elected to subscribe.
- Subscribe/Unsubscribe -
To subscribe, go to the subscriptions page, choose the newsletters you want to subscribe to and click Update
To unsubscribe, go to the subscriptions page, uncheck the appropriate check box, and click Update
This document is protected by copyright.
Java Technology Fundamentals
June 2002
Sun, Sun Microsystems, Java, J2SE, JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.
|
| ||||||||||||