Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Tutorial: Advanced Java Programming Advanced Java Programming and Database connection and Database connection Eran Toch Methodologies in the Development of Information Systems November 2003
25
Embed
Advanced Java Programming – Eran Toch Methodologies in Information System Development Tutorial: Advanced Java Programming and Database connection Eran.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Advanced Java Programming – Eran TochMethodologies in Information System Development
• Definition: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
• What’s wrong with using the return value for error handling?– Advantage 1: Separating Error Handling Code from
"Regular" Code – Advantage 2: Propagating Errors Up the Call Stack – Advantage 3: Grouping Error Types and Error
Differentiation
Advanced Java Programming – Eran TochMethodologies in Information System Development
4
Exceptions – Advantage 1Exceptions – Advantage 1
Separating Error Handling Code from "Regular" Code:
errorCodeType readFile { initialize errorCode = 0; open the file; if (theFileIsOpen) { determine the length of the file; if (gotTheFileLength) { allocate that much memory; if (gotEnoughMemory) { read the file into memory; if (readFailed) { errorCode = -1; } } else { errorCode = -2; }. . .
readFile { try { open the file; determine its size; allocate that much memory; read the file into memory; close the file; } catch (fileOpenFailed) { doSomething; } catch (sizeDeterminationFailed) { doSomething; } catch (memoryAllocationFailed) { doSomething; } catch (readFailed) { doSomething; } catch (fileCloseFailed) { doSomething; }}
Without Exception With Exception
Advanced Java Programming – Eran TochMethodologies in Information System Development
5
The The trytry Block Block
• try block:
• A try statement must be accompanied by at least one catch block or one finally block.
try { System.out.println("Entering try statement"); out = new PrintWriter(new FileWriter("OutFile.txt"));}for (int i = 0; i < size; i++)
Advanced Java Programming – Eran TochMethodologies in Information System Development
12
Exceptions and JavaDocExceptions and JavaDoc
• Exception can be documented by Javadoc using the @exception statement
/** * regular javadoc text… * @throwsExceptionIf the Driver was not found. * @throwsSQLExceptionIf the the <code>DriverManager.getConnection * </code> method returned an error. */public void createConnection()throws SQLException, Exception{
Advanced Java Programming – Eran TochMethodologies in Information System Development
13
AgendaAgenda
• Exceptions and Error Handling– What is it and why do we need it?– The try, catch, finally procedure
• Database Access with JDBC– Installations– Connecting and querying the database– Complete example
• References
Advanced Java Programming – Eran TochMethodologies in Information System Development
public void closeConnection(){ if (conn != null){ try { conn.close(); } catch (SQLException sqlEx){} conn = null; }}
Cleaning off is best done in the “finally” clause
Cleaning off ResultSet
Cleaning off Statement, after the ResultSet
Cleaning off the connection, in a different method (why?)
Advanced Java Programming – Eran TochMethodologies in Information System Development
23
Example – Test ClientExample – Test Client
public class Test { public static void main(String[] args) { SQLConnect connect = new SQLConnect(); connect.createConnection(); String allPhones = connect.getPhones(); connect.closeConnection(); System.out.println("phones:"); System.out.println(allPhones); }}
Outputphones:+972-4-9831894
Advanced Java Programming – Eran TochMethodologies in Information System Development
24
AgendaAgenda
• Exceptions and Error Handling– What is it and why do we need it?– The try, catch, finally procedure
• Database Access with JDBC– Installations– Connecting and querying the database– Complete example
• References
Advanced Java Programming – Eran TochMethodologies in Information System Development
25
ReferencesReferences
• Exception handling in the Java tutorial:http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html