Using Databases with Java
Java provides support for industry-standard databases that respond to SQL statements via JDBC (Java Database Connectivity).The JDBC-ODBC bridge allows Java to interface with any ODBC database. However, this connection is somewhat inefficient, since two drivers are required to process the database access. Many database suppliers such as Informix, Oracle and Sybase provide JDBC interfaces that communicate directly with the database server. However, these are not part of the Java language and must be obtained from the vendor.
From a programming point of view, all databases are treated alike. This course uses a connection to Microsoft Access to illustrate database concepts, but, other than the setting up of the URL which implements the database connection, all databases will behave in the same way.
To use Microsoft Access with Java, you will first have to set up an ODBC
connection as below. Other databases may need similar steps to create the
connection.
- From the control panel, select ODBC 32 bit
- This will bring up the ODBC Source Administrator
- From the User DSN tab, choose Add
- This will bring up the Create new Data Source window.
- Choose Microsoft Access and click Finish
- This will bring up the Microsoft Access setup window.
- Enter a data source name (DSN) and description. The name will be used to refer to the database when setting up an URL for the connection.
- In the Database panel, choose Select. This will bring up a file browser from which the database can be selected. Select the database and click OK
- From the Access setup screen, click OK
- From the Data Source Administrator screen, click OK
The following sample program retrieves the account number, surname and first names for all rows in a Customer table in a database pointed to by a DSN of JavaCourse
import java.sql.*; }
public class test1
{
String query =
"select Account, Surname, Firstname from Customer";
try
{
Connection con =DriverManager.getConnection(url, "","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
System.out.println("Surname: " + rs.getString(2));
System.out.println("First Name: " + rs.getString(3));
catch(SQLException ex)
{
{
ex = ex.getNextException();
catch(java.lang.Exception ex)
{
Notes:
- Programs that use databases must import the java.sql package
- An url is needed to point to the DSN set up in the previous step. For this
to work, we would need
to have set up a DSN called JavaCourse to point to the database containing
the Customer table.
String url = "jdbc:odbc:JavaCourse";
- The statement below is used to find the correct class to deal
with the connection. This statement
may be different when you are using JDBC connectivity supplied by a
databadse vendor.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- The following statement creates a connection to the database
Connection con =DriverManager.getConnection(url, "","");
- A Statement object is needed to enable your programs
to run SQL statements.
Statement stmt = con.createStatement();
- A ResultSet object stores the result of running an SQL statement.
It has a next method that allows
you to page through the results one row at a time. It also provides
get methods to allow you to
access the actual data.
ResultSet rs = stmt.executeQuery(query);
- Any programs that deal with the java.sql classes must check for
SQLExceptions and deal with them.
Hopefully this short tutorial will have got you started with the Java language, and given you an idea of how to find out more for yourself