Pick a Free OS

User login


An Introduction to JDBC


renamed and included in the exjava directory. Another directory,

exgwe, contains the source and classes for the Gwe Mysql JDBC driver.

These classes make use of the classes in the exjava directory. Add the

directory in which the tar file was unpacked to the CLASSPATH

environment variable. This completes the installation of the JDBC


A Sample JDBC Program:

This example below assumes you have some familiarity with Java. The

Java code below loads the JDBC driver class, establishes a connection

with a database, builds an SQL statement, submits the statement, and

retrieves the results. We assume there is an existing database and a

table populated with some data.

import java.net.URL;

import exjava.sql.*;

import exgwe.sql.*;

public class jdbc_test {

public static void main (String argv[] )


try {

// Load the gweMysqlDriver JDBC Driver


// Create the url of the database we wish to connect to

String url = "jdbc:mysql://localhost:3306/test";

// Set up a connection

Connection con = DriverManager.getConnection(url, "userid","pass");

// Create statement

Statement stmt = con.createStatement();

// Execute query

String query = "select name, phone from test_table";

// Obtain the result set

ResultSet rs = stmt.executeQuery(query);

// Loop through each result row

while (rs.next() )

{ System.out.println( rs.getString(0), rs.getInt(1) ); }

// Close result set


// Close statement


// Close connection



catch( Exception e ) { e.printStackTrace(); }



In the first line, the class object for the JDBC driver is loaded by

passing its fully qualified name to the Class.forname() method. This

method loads the class, if it is not already loaded and returns a

Class object for it. In the next line, the database URL string is

constructed. It is in the form "jdbc:subprotocol name: hostname:

port/database name/other parameters". The subprotocol name is mysql

since we are using the Mysql database. The hostname is localhost in