Laboratory Exercise 3: JDBC: at The End of The Exercise, The Students Should Be Able To
Laboratory Exercise 3: JDBC: at The End of The Exercise, The Students Should Be Able To
Material:
PC with Java SDK, MS Access, and JCreator installed.
1) First we need to set up Java to understand how to communicate with an ODBC data source.
• Set up your DriverManager to understand ODBC data sources
class Test
{
public static void main(String[] args)
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
//save this code into a file called Test.java and compile it
There are two ways to get a connection from your Microsoft Access Database:
1. Get a connection by accessing the Database Directly
One way to get a connection is to go directly after the MS Access database file. Here is a complete
sample program getting a connection to a MS Access database named mdbTEST.mdb. This
sample includes the lines required to set the DriverManager up for ODBC data sources.
import java.sql.*;
class Test
{
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// set this to a MS Access DB you have on your machine
String filename = "d:/java/mdbTEST.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver
(*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}"; //
add on to the end
// now we can get the connection from the DriverManager
Connection con = DriverManager.getConnection(database ,"","");
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
//save this code into a file called Test.java and compile it
Here's a complete program showing how to access your new DSN data source:
import java.sql.*;
public class Test
{
public static void main(String[] args)
{
3) Once you have gained access to the Database (been granted a connection), you are ready to try the
following:
o Run a SQL Statement on your Access Database
o Create a Statement from the connection you have made
o Get a ResultSet by executing a query (your insert/delete/etc. statement) on that statement
Now lets learn how to make a statement, execute a query and display a the ResultSet from that query.
Refer to the following complete program for an understanding of these concepts (details follow):
This code assumes that you have used the DSN method (Step 2 method 2) to create a DSN named
mdbTest. If you have not, you'll need to modify this code to work for a direct connection as explained in
Step 2 method 1.
import java.sql.*;
public class Test
{
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/* the next 3 lines are Step 2 method 2 from above - you could use the
direct
access method (Step 2 method 1) istead if you wanted */
String dataSourceName = "mdbTEST";
String dbURL = "jdbc:odbc:" + dataSourceName;
Connection con = DriverManager.getConnection(dbURL, "","");
// try and create a java.sql.Statement so we can run queries
Statement s = con.createStatement();
s.execute("create table TEST12345 ( column_name integer )"); // create
a table
s.execute("insert into TEST12345 values(1)"); // insert some data into
the table
s.execute("select column_name from TEST12345"); // select the data
from the table
ResultSet rs = s.getResultSet(); // get any ResultSet that came from
our query
if (rs != null) // if rs == null, then there is no ResultSet to view
while ( rs.next() ) // this will step through our data row-by-row
{
/* the next line will get the first column in our current row's
ResultSet
as a String ( getString( columnNumber) ) and output it to the
screen */
System.out.println("Data from column_name: " + rs.getString(1) );
}
}
//save this code into a file called Test.java and compile it
4) Compile and execute the program. Make a screenshot of the output and submit to your instructor
together with the source code.