How To Use The Database In Jdbc.Com On A Microsoft Gdbdns.Com (Amd64) On A Pcode (Amd32) On An Ubuntu 8.2.2 (Amd66) On Microsoft



Similar documents
Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access

SQL and Java. Database Systems Lecture 19 Natasha Alechina

JDBC. It is connected by the Native Module of dependent form of h/w like.dll or.so. ex) OCI driver for local connection to Oracle

CSC 443 Data Base Management Systems. Basic SQL

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

The JAVA Way: JDBC and SQLJ

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Lab Assignment Creating a Relational Database Schema from ER Diagram, Populating the Database and Querying over the database with SQL

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

CHAPTER 3. Relational Database Management System: Oracle. 3.1 COMPANY Database

COSC344 Database Theory and Applications. Java and SQL. Lecture 12

More SQL: Assertions, Views, and Programming Techniques

CS/CE 2336 Computer Science II

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Self-test Database application programming with JDBC

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.

ER & EER to Relational Mapping. Chapter 9 1

Using Netbeans and the Derby Database for Projects Contents

Setting Up ALERE with Client/Server Data

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

JDBC (Java / SQL Programming) CS 377: Database Systems

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

CS346: Database Programming.

Part 4: Database Language - SQL

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

1 SQL Data Types and Schemas

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Introduction Web Portal Main Page Group Management Create group Modify Group Member List... 5

Introduction to SQL: Data Retrieving

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Java and Microsoft Access SQL Tutorial

featuring data privacy Andres Avelino Campos Sainz A Project submitted in partial fulfillment of the requirements for the degree of

Java DataBase Connectivity (JDBC)

Making Oracle and JDBC Work For You

CS2506 Operating Systems II Lab 8, 8 th Tue/03 /2011 Java API

New York University Computer Science Department Courant Institute of Mathematical Sciences

ODBC Client Driver Help Kepware, Inc.

SQL-99: Schema Definition, Basic Constraints, and Queries

How To Create A Table In Sql (Ahem)

Introduction to Hadoop on the cloud using BigInsights on BlueMix Feb , 2014

7 Web Databases. Access to Web Databases: Servlets, Applets. Java Server Pages PHP, PEAR. Languages: Java, PHP, Python,...

ODBC Driver Version 4 Manual

Tutorial: How to Use SQL Server Management Studio from Home

Brazil + JDBC Juin 2001, douin@cnam.fr

Applets, RMI, JDBC Exam Review

FileMaker 11. ODBC and JDBC Guide

Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

Working With Derby. Version 10.2 Derby Document build: December 11, 2006, 7:06:09 AM (PST)

LSINF1124 Projet de programmation

Microsoft SQL Server Features that can be used with the IBM i

Package sjdbc. R topics documented: February 20, 2015

ODBC Reference Guide

Abstract. Introduction. Web Technology and Thin Clients. What s New in Java Version 1.1

Application Development A Cocktail of Java and MCP. MCP Guru Series Dan Meyer & Pramod Nair

DEVELOPING MULTITHREADED DATABASE APPLICATION USING JAVA TOOLS AND ORACLE DATABASE MANAGEMENT SYSTEM IN INTRANET ENVIRONMENT

Querying Databases Using the DB Query and JDBC Query Nodes

CS 338 Join, Aggregate and Group SQL Queries

Migrating helpdesk to a new server

Summary on Chapter 4 Basic SQL

FileMaker 13. ODBC and JDBC Guide

ReportBy ODBC Connection setup

CHAPTER 8: SQL-99: SCHEMA DEFINITION, BASIC CONSTRAINTS, AND QUERIES

FileMaker 14. ODBC and JDBC Guide

How to Connect to CDL SQL Server Database via Internet

Knocker main application User manual

How To Use A Sas Server On A Java Computer Or A Java.Net Computer (Sas) On A Microsoft Microsoft Server (Sasa) On An Ipo (Sauge) Or A Microsas (Sask

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

FileMaker 12. ODBC and JDBC Guide

SimbaEngine SDK 9.4. Build a C++ ODBC Driver for SQL-Based Data Sources in 5 Days. Last Revised: October Simba Technologies Inc.

CIMHT_006 How to Configure the Database Logger Proficy HMI/SCADA CIMPLICITY

Step 2: Open the ODBC Data Source Administrator Panel

VIEWS virtual relation data duplication consistency problems

2. Unzip the file using a program that supports long filenames, such as WinZip. Do not use DOS.

SQL Nested & Complex Queries. CS 377: Database Systems

Overview of Web Services API

Installation Instruction STATISTICA Enterprise Small Business

INTRODUCTION: SQL SERVER ACCESS / LOGIN ACCOUNT INFO:

Apéndice C: Código Fuente del Programa DBConnection.java

Developing SQL and PL/SQL with JDeveloper

ASP.NET Programming with C# and SQL Server

Titanium Schedule Client Import Guide

Converting InfoPlus.21 Data to a Microsoft SQL Server 2000 Database

Figure 14.1 Simplified version of the

STATISTICA VERSION 12 STATISTICA ENTERPRISE SMALL BUSINESS INSTALLATION INSTRUCTIONS

Using SQL Server Management Studio

Lab Manual. Database Systems COT-313 & Database Management Systems Lab IT-216

The Relational Algebra

FileMaker 8. Installing FileMaker 8 ODBC and JDBC Client Drivers

Figure 1. Accessing via External Tables with in-database MapReduce

Database Access Through Java Technologies

1 Changes in this release

Transcription:

CS 7700 Transaction Design for Microsoft Access Database with JDBC Purpose The purpose of this tutorial is to introduce the process of developing transactions for a Microsoft Access Database with Java Database Connectivity (JDBC). Problem The XYZ Corporation needs a simple database system to store departments, employees, projects, and dependents information. To support the corporation s business objectives, users need to be able to perform the following transactions: Add new records into a current table Delete records from a current table Modify records in a current table Retrieve records from a current table 1

Database Schema This company relational database schema comes from the textbook (Fundamentals of Database systems, 6th Edition, by Ramez Elmasri and Shamkant Navathe). EMPLOYEE Fname Minit Lname Ssn Bdate Address Sex Salary Super_ssn Dno DEPARTMENT Dname Dnumber Mgr_ssn Mgr_start_date DEPT_LOCATIONS Dnumber Dlocation PROJECT Pname Pnumber Plocation Dnum WORKS_ON Essn Pno Hours DEPENDENT Essn Depentdent_name Sex Bdate Relationship 2

1. Introduction Java Database Connectivity (JDBC) API is the industry standard for databaseindependent connectivity between the Java programming language and a wide range of databases which also called the data sources. Data source accesses within the Java program require JDBC drivers. Open Database Connectivity (ODBC) is a standard protocol for programs (such as Microsoft Access) to obtain access to SQL database servers (such as Microsoft SQL Server or Oracle). One of the JDBC drivers, JDBC-ODBC bridge, is applied in this tutorial. The JDBC- ODBC bridge employs an ODBC driver to connect to a target database and translates JDBC method calls into ODBC function calls. It is usually used for a database lacking a JDBC driver. 2. Create Database and tables Look over the CS 4700/6700 tutorial for the step by step description for the process of creating a database in Microsoft Access. 3. Connect JDBC to Microsoft Access For a 64-bit operating system with 64-bit Microsoft Access, the 64-bit Microsoft Access Database Engine 2010, AccessDatabaseEngine_x64.exe, should be installed first. It can be downloaded at http://www.microsoft.com/en-us/download/details.aspx?id=13255 3

Open Control Panel, and select Administrative Tools, then select and open Data Sources (ODBC). Note: A 64-bit Windows operating system has two odbcad32.exe files: 32-bit and 64- bit.the default shortcut in Administrative Tools is for the 64-bit one. 32-bit Microsoft Access users need to open the 32-bit ODBC Administrator. To do so, right click on Data Sources (ODBC) and go to its properties. In properties, change following terms and click OK : Target from %windir%\system32\odbcad32.exe to %windir%\syswow64\odbcad32.exe Start in from %windir%\system32 to %windir%\syswow64 After these path changes, 32-bit ODBC Administrator can be open by the shortcut in Administrative Tools. 4

Once open the Data Sources (ODBC), in the pop-up ODBC Data Source Administrator, go to System DSN then click on Add button. 5

Then in the pop-up window, select Microsoft Access Driver (*.mdb, *.accdb) then click on Finish. On the pop-up ODBC Microsoft Access Setup page, type your desired Data Source Name (JDBCdsn is the name used here). This is the name you will be using in the Java code to connect to the database, so ideally try to keep the database name and the DSN name to be the same. And then click on Select button. 6

Then find and select the Access database file you created previously, like the Company.accdb here and click OK. Then click OK in the ODBC Microsoft Access Setup page. Finaly, the Data Sourse you created appears in the ODBC Data Source Administrator as shown below. 7

4. Design Transactions Now the next step is to design and execute transactions based on a java program and embed SQL queries. Before being able to process JDBC function calls with Java, it is necessary to import the JDBC class libraries java.sql.* which can be found at http://docs.oracle.com/javase/7/docs/api/java/sql/statement.html Note: 1. The JDBC-ODBC Bridge has been removed in JDK 8. So JDK 7 or less should be used as the Java platform. 2. If 32-bit Microsoft Access Database (or Data Source) is used, then keep your Microsoft Access Driver (or ODBC Driver), JDK and even Java IDE all in 32-bit.While if 64-bit Data source is in use, keep them all in 64-bit. Generally, a complete transaction includes five main parts. They are: loading the driver, establishing a connection to data source, executing queries, committing transactions, and closing the connection. 1) Loading the driver To establish a connection with the data source, first you must load the driver. try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println("driver loaded"); } //end of try catch(java.lang.classnotfoundexception e) System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); 2) Establishing a connection When a connection is created, it is in auto-commit mode. This means that each individual SQL statement is treated as a transaction and is automatically committed right after it is executed. The way to allow two or more statements to be grouped into a transaction is to disable auto-commit mode. Once auto-commit mode is disabled, no SQL statements are 8

committed until you call the method commit explicitly. This is demonstrated in the following lines of codes, where conn is an active connection: This is the same name you previously defined for you data source try conn = DriverManager.getConnection("jdbc:odbc:JDBCdsn"); System.out.println("Connected to the database"); conn.setautocommit(false); This removes the auto-commit mode catch (SQLException se) System.out.println("Not connected to database"); System.out.println(se); 3) Executing queries In JDBC, the Statement Objects define the methods and properties than enable you to send SQL commands and receive data from your database. First, a Statement object should be create using the createstatement()method. Once a Statement is created, There are three execute methods can be used to execute a SQL statement: boolean execute(string SQL) : Returns a boolean value of true if a ResultSet object can be retrieved; otherwise, it returns false. Use this method to execute SQL DDL statements or when you need to use truly dynamic SQL. int executeupdate(string SQL) : Returns the numbers of rows affected by the execution of the SQL statement. Use this method to execute SQL statements for which you expect to get a number of rows affected - for example, an insert, update, or delete statement. ResultSet executequery(string SQL) : Returns a ResultSet object. Use this method when you expect to get a result set, as you would with a SELECT statement. A ResultSet object is a table of data representing a database result set.the ResultSet interface provides methods for retrieving and manipulating the results of executed queries. 9

The data in a ResultSet object is accessed through a cursor, which points to its current row of data in the ResultSet. When a ResultSet object is first created, the cursor is positioned before the first row. The next method moves the cursor to the next row. The ResultSet interface provides getter methods (getboolean, getlong, and so on) for retrieving column values from the current row. Values can be retrieved using either the index number of the column or the name of the column. Columns are numbered from 1. Column names used as input to getter methods are case insensitive. When a getter method is called with a column name and several columns have the same name, the value of the first matching column will be returned. The getter method of the appropriate type retrieves the value in each column. For example, the first column in each row of ResultSet rs is Dname, which stores a value of SQL type TEXT. The method for retrieving the value is getstring. The second column in each row stores a value of SQL type INT, and the method for retrieving values of that type is getint. Each time the method next is invoked, the next row becomes the current row, and the loop continues until there are no more rows in rs. An example code segment for a select operation is shown below. try Statement stmt = conn.createstatement(); ResultSet rs = stmt.executequery("select * FROM Department"); while( rs.next() ) System.out.print(rs.getString("Dname")) ; System.out.print(rs.getInt("Dnumber")) ; System.out.println( rs.getstring("mgr_ssn")) ; System.out.println( rs.getdate("mgr_start_date")) ; rs.close() ; stmt.close() ; catch (Exception excep) System.out.println("Failed to execute query\n"+excep); }//catch 10

For insert, delete and update operations, two argument should be added to the createstatement method. The first argument indicates the type of a ResultSet object: TYPE_FORWARD_ONLY, TYPE_SCROLL_INSENSITIVE, and TYPE_SCROLL_SENSITIVE. The second argument is one of two ResultSet constants for specifying whether a result set is read-only or updatable: CONCUR_READ_ONLY and CONCUR_UPDATABLE. The point to remember here is that if you specify a type, you must also specify whether it is read-only or updatable. Also, you must specify the type first, because both parameters are of type int, the compiler will not complain if you switch the order. Specifying the constant TYPE_FORWARD_ONLY creates a nonscrollable result set, that is, one in which the cursor moves only forward. If you do not specify any constants for the type and updatability of a ResultSet object, you will automatically get one that is TYPE_FORWARD_ONLY and CONCUR_READ_ONLY. The details of three Resultset type are given below. TYPE_FORWARD_ONLY The result set is not scrollable; its cursor moves forward only, from before the first row to after the last row. The rows contained in the result set depend on how the underlying database materializes the results. That is, it contains the rows that satisfy the query at either the time the query is executed or as the rows are retrieved. TYPE_SCROLL_INSENSITIVE The result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. TYPE_SCROLL_SENSITIVE The result set is scrollable; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. An example code segment for insert, delete and update operations is given below. 11

try Statement stmt =conn.createstatement(resultset.type_scroll_insensitive, ResultSet.CONCUR_UPDATABLE); stmt.executeupdate(query); System.out.println("Executed the query successfully"); stmt.close() ; catch (Exception excep) System.out.println("Failed to execute query"+excep); 4) Committing transactions After all the SQL statements are executed, method commit should be call. Then all statements executed after the previous call to the method commit are included in the current transaction and committed together as a unit. try conn.commit(); System.out.println("Changes successfully committed"); catch (Exception e) System.out.println("Failed to commit changes: n" + e); System.exit(0); 5) Closing the connection Once the transactions are finished, the connection with the data source can be closed by the following code. try conn.close(); System.out.println("Connection successfully closed "); catch (Exception excep) System.out.println("Unable to close connection: n" + excep); System.exit(0); 12

5. Example: A sample code which can be used to insert, delete, update and retrieve records in the department table is given below. Please pay attention to the previously described parts and see how they can be put together in a program. import java.io.*; import java.sql.*; import java.text.*; public class newdatabase static boolean exit = false; static Connection conn = null; public static void main(string[] args) String choice; int N=0; while(!exit) displayconsole(); choice = captureinput(); try N = Integer.parseInt(choice); catch(exception e) System.err.println("Enter valid option"+e); displayconsole(); switchcase(n); }// end of main public static void displayconsole() //boolean exit = false; System.out.println("**************************************************** ***"); System.out.println("Welcome to Database tutorial "); System.out.println("******************************************************"); System.out.println("1. Insert record to database"); System.out.println("2. Delete record from database"); System.out.println("3. Update record in database"); System.out.println("4. Retrieve record in database"); System.out.println("5. Exit"); System.out.println("Enter your choice:"); 13

}// end of displayconsole public static String captureinput() String choice=""; try BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); choice = in.readline(); }//try catch(ioexception e) System.out.println("Enter valid numeric choice"); return choice; }//end of captureinput public static java.sql.date Inputdate() java.util.date utildate=null; java.sql.date sqldate=null; SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); try String userinput = captureinput(); utildate = formatter.parse(userinput); sqldate = new java.sql.date(utildate.gettime()); } catch (ParseException e) // execution will come here if the String that is given // does not match the expected format. e.printstacktrace(); } return sqldate; }//end of captureinput public static void switchcase(int N) switch (N) case 1: String Dname; int Dnumber; String Mg_ssn; java.sql.date Mgr_start_date; boolean quit=false; while(!quit) System.out.println("Enter Department Name:"); Dname = captureinput(); System.out.println("Enter Department Number:"); Dnumber = Integer.parseInt(captureInput()); System.out.println("Enter Department Manager SSN"); 14

Mg_ssn = captureinput(); System.out.println("Enter Manage Start Date (mm/dd/yyyy):"); Mgr_start_date = Inputdate(); String query="insert INTO Department VALUES('"+Dname+"','"+Dnumber+"','"+Mg_ssn+"','"+Mgr_start_date+"')"; //System.out.println(query); loaddriver(); openconnection(); try Statement stmt = conn.createstatement(resultset.type_scroll_insensitive,resultset.concur_updata BLE); stmt.executeupdate(query); System.out.println("Executed the query successfully"); ResultSet rs = stmt.executequery("select * FROM Department"); while( rs.next() ) System.out.print(rs.getString("Dname")) ; System.out.print(rs.getInt("Dnumber")); System.out.println( rs.getstring("mgr_ssn")) ; System.out.println( rs.getdate("mgr_start_date")); rs.close() ; stmt.close() ; catch (Exception excep) System.out.println("A record with this primary key value already exists or table to which you are trying to make changes is opened\n" + excep); System.out.println("Failed to execute query"); commitdb(); closeconnection(); System.out.println("Do you want to go back to main menu,press y to do so"); String ch=captureinput(); if(ch.equals("y") ch.equals("y")) quit=true; }//end of if break; }//end of case 1 case 2: 15

String Dname; int Dnumber=0; String Mgr_ssn; java.sql.date Mgr_start_date; int i; String ch; boolean quit=false; while(!quit) boolean done=false; loaddriver(); openconnection(); try Statement stmt = conn.createstatement(resultset.type_scroll_insensitive,resultset.concur_updata BLE); ResultSet rs = stmt.executequery("select * FROM Department"); while( rs.next() ) System.out.println(rs.getString("Dname")+"\t"+rs.getInt("Dnumber")+"\t"+rs.get String("Mgr_ssn")+"\t"+rs.getDate("Mgr_start_date")) ; while(!done) rs.first(); System.out.println("Enter a valid department number of the record you want to delete"); i = Integer.parseInt(captureInput()); Mgr_start_date=rs.getDate("Mgr_start_date"); }//end of if }//end of rs while }//end of done while while( rs.next() ) if(rs.getint("dnumber")==i) done=true; Dname=rs.getString("Dname"); Dnumber=i; Mgr_ssn=rs.getString("Mgr_ssn"); Dnumber="+Dnumber+""; String query="delete FROM Department WHERE //System.out.println(query); stmt.executeupdate(query); System.out.println("Executed the query successfully"); rs = stmt.executequery("select * FROM Department"); while( rs.next() ) System.out.print(rs.getString("Dname")) ; System.out.print(rs.getInt("Dnumber")); 16

System.out.println( rs.getstring("mgr_ssn")) ; System.out.println( rs.getdate("mgr_start_date")); rs.close() ; stmt.close() ; catch (Exception excep) System.out.println("The table to which you are trying to make changes is open,please close\n" + excep); System.out.println("Failed to execute query"); }//catch commitdb(); closeconnection(); System.out.println("Do you want to go back to main menu,press y to do so"); String op=captureinput(); if(op.equals("y") op.equals("y")) quit=true; }//end of if break; }//end of case 2 case 3: String Dname=null; int Dnumber=0; String Mgr_ssn=null; java.sql.date Mgr_start_date=null; java.sql.date newdate; int i=0; String ch; String query; boolean quit=false; while(!quit) boolean done=false; loaddriver(); openconnection(); try Statement stmt = conn.createstatement(resultset.type_scroll_insensitive,resultset.concur_updata BLE); ResultSet rs = stmt.executequery("select * FROM Department"); while( rs.next() ) System.out.println(rs.getString("Dname")+"\t"+rs.getInt("Dnumber")+"\t"+rs.get String("Mgr_ssn")+"\t"+rs.getDate("Mgr_start_date")) ; while(!done) 17

rs.first(); System.out.println("Enter a valid department number of the record you want to update:"); i = Integer.parseInt(captureInput()); while( rs.next() ) if(rs.getint("dnumber")==i) done=true; Dname=rs.getString("Dname"); Dnumber=i; Mgr_ssn=rs.getString("Mgr_ssn"); Mgr_start_date=rs.getDate("Mgr_start_date"); }//end of if }//end of rs while }//end of done while System.out.println("Enter a new department number you want to update the record to:"); i = Integer.parseInt(captureInput()); Dnumber=i; query="update Department SET Dnumber='"+Dnumber+"' WHERE Dname='"+Dname+"'"; //System.out.println(query); stmt.executeupdate(query); System.out.println("DO you want to update the Dname of the record,press y to update"); ch = captureinput(); if(ch.equals("y") ch.equals("y")) System.out.println("Enter a new department name you want to update the record to:"); ch = captureinput(); Dname=ch; }//end of if System.out.println("DO you want to update the Mgr_ssn of the record,press y to update"); ch = captureinput(); if(ch.equals("y") ch.equals("y")) System.out.println("Enter a new manager ssn you want to update the record to:"); ch = captureinput(); Mgr_ssn=ch; }//end of if System.out.println("DO you want to update the Mgr_start_date of the record,press y to update"); ch = captureinput(); if(ch.equals("y") ch.equals("y")) System.out.println("Enter a new manage start date you want to update the record to:"); newdate = Inputdate(); Mgr_start_date=newdate; }//end of if 18

query="update Department SET Dname='"+Dname+"',Mgr_ssn='"+Mgr_ssn+"',Mgr_start_date='"+Mgr_start_date+"' WHERE Dnumber="+Dnumber+""; //System.out.println(query); stmt.executeupdate(query); System.out.println("Executed the query successfully"); rs = stmt.executequery("select * FROM Department"); while( rs.next() ) System.out.print(rs.getString("Dname")) ; System.out.print(rs.getInt("Dnumber")); System.out.println( rs.getstring("mgr_ssn")) ; System.out.println( rs.getdate("mgr_start_date")); rs.close() ; stmt.close() ; catch (Exception excep) System.out.println("A record with this primary key value already exists or table to which you are trying to make changes is opened\n" + excep); System.out.println("Failed to execute query"); }//catch commitdb(); closeconnection(); System.out.println("Do you want to go back to main menu,press y to do so"); String op=captureinput(); if(op.equals("y") op.equals("y")) quit=true; }//end of if break; }//end of case 3 case 4: loaddriver(); openconnection(); try Statement stmt = conn.createstatement(); ResultSet rs = stmt.executequery("select * FROM Department"); while( rs.next() ) System.out.print(rs.getString("Dname")) ; System.out.print(rs.getInt("Dnumber")) ; 19

System.out.println( rs.getstring("mgr_ssn")) ; System.out.println( rs.getdate("mgr_start_date")) ; query\n"+excep); rs.close() ; stmt.close() ; catch (Exception excep) System.out.println("Failed to execute }//catch commitdb(); closeconnection(); System.out.println("Do you want to go back to main menu,press any key to do so"); String op=captureinput(); break; }//end of case 4 case 5: exit=true; System.out.println("Have a nice day!"); break; }//end of case 5 }//end of switch }//end of switchcase public static void loaddriver() try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); System.out.println("driver loaded"); } //end of try catch(java.lang.classnotfoundexception e) System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); }// end of loaddriver public static void openconnection() try conn = DriverManager.getConnection("jdbc:odbc:JDBCdsn"); System.out.println("Connected to the database"); conn.setautocommit(false); catch (SQLException se) System.out.println("Not connected to database"); System.out.println(se); }//end of openconnection 20

public static void commitdb() try conn.commit(); System.out.println("Changes successfully committed"); catch (Exception e) System.out.println("Failed to commit changes: n" + e); System.exit(0); }//end of commitdb public static void closeconnection() try conn.close(); System.out.println("Connection successfully closed "); catch (Exception excep) System.out.println("Unable to close connection: n" + excep); System.exit(0); }//end of closeconnection }//end of Database 21