0% found this document useful (0 votes)
79 views

Laboratory Exercise 3: JDBC: at The End of The Exercise, The Students Should Be Able To

This document provides instructions for a laboratory exercise on using JDBC to connect to a Microsoft Access database from Java. The objectives are to create a table in Access, apply JDBC APIs to get a connection and run SQL statements. Students will set up the DriverManager to communicate via ODBC, get a connection using either a direct file path or a DSN, create a statement and execute queries to insert, select and display data from a result set. Sample code is provided to create a table, insert and select data, then drop the table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Laboratory Exercise 3: JDBC: at The End of The Exercise, The Students Should Be Able To

This document provides instructions for a laboratory exercise on using JDBC to connect to a Microsoft Access database from Java. The objectives are to create a table in Access, apply JDBC APIs to get a connection and run SQL statements. Students will set up the DriverManager to communicate via ODBC, get a connection using either a direct file path or a DSN, create a statement and execute queries to insert, select and display data from a result set. Sample code is provided to create a table, insert and select data, then drop the table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

F0102

Laboratory Exercise 3: JDBC

At the end of the exercise, the students should be able to:


 Create table using Microsoft Access
 Apply JDBC APIs
 Set up Java to understand ODBC
 Get a connection to our MS Access Database
 Run a SQL statement

Material:
 PC with Java SDK, MS Access, and JCreator installed.

Perform the following instructions:

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

2) After we set up the DriverManager, we need to get a Connection.

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.

Laboratory Exercise 3: JDBC * Property of STI


Page 1 of 4
F0102

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

2. Set the Access Database up as an ODBC DSN


MS Access databases can be connected to via ODBC. Instead of accessing the database directly,
we can access it via Data Source Name (DSN). Here's how to set up a DSN on your system:

1. Open Windows' ODBC Data Source Administrator.


2. In Windows 98, or NT, choose Start > Settings > Control Panel, then double-click the ODBC
Data Sources icon. Depending on your system, the icon could also be called ODBC or 32bit
ODBC.
3. In Windows 2000 or XP, choose Start > Settings > Control Panel > Administrative Tools >
Data Sources.
4. In the ODBC Data Source Administrator dialog box, click the System DSN tab.
5. Click Add to add a new DSN to the list.
6. Scroll down and select the Microsoft Access (.MDB) driver.
7. Type in the name "mdbTEST" (no quotes, but leave the cases the same) for the Data Source
Name.
8. Click CREATE and select a file to save the database to (mdbTEST.mdb"). This creates a new
blank MS Access database. Click OK.

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)
{

Laboratory Exercise 3: JDBC * Property of STI


Page 2 of 4
F0102

// change this to whatever your DSN is


String dataSourceName = "mdbTEST";
String dbURL = "jdbc:odbc:" + dataSourceName;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection(dbURL, "","");
}

catch (Exception err) {


System.out.println( "Error: " + err );
}
}
}
//save this code into a file called Test.java and compile it

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

Laboratory Exercise 3: JDBC * Property of STI


Page 3 of 4
F0102

{
/* 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) );
}

s.execute("drop table TEST12345");


s.close(); // close the Statement to let the database know we're done
with it
con.close(); // close the Connection to let the database know we're
done with it
}

catch (Exception err) {


System.out.println("ERROR: " + err);
}

}
//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.

Laboratory Exercise 3: JDBC * Property of STI


Page 4 of 4

You might also like