Java Tutorial for Beginners

 

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.

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
{

public static void main(String[] args) {
String url = "jdbc:odbc:JavaCourse";
String query = "select Account, Surname, Firstname from Customer";

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =DriverManager.getConnection(url, "","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
System.out.println("Account No: " + rs.getString(1));
System.out.println("Surname: " + rs.getString(2));
System.out.println("First Name: " + rs.getString(3));
}
}
catch(SQLException ex)
{
while (ex!=null)
{
System.out.println ("SQL Exception: " + ex.getMessage ());
ex = ex.getNextException();
}
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}

}

}

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