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



Similar documents
An Overview of Java. overview-1

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

Chapter 1 Java Program Design and Development

Java Application Developer Certificate Program Competencies

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

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

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

CS346: Database Programming.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

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

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

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

CS/CE 2336 Computer Science II

Elements of Advanced Java Programming

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

Database Access from a Programming Language:

Financial Management System

Java (12 Weeks) Introduction to Java Programming Language

Specialized Programme on Web Application Development using Open Source Tools

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

Java SE 8 Programming

CSC 551: Web Programming. Spring 2004

The JAVA Way: JDBC and SQLJ

Applets, RMI, JDBC Exam Review

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Java Interview Questions and Answers

Case Studies of Running the Platform. NetBeans UML Servlet JSP GlassFish EJB

Java CPD (I) Frans Coenen Department of Computer Science

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

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

There are some important differences between an applet and a standalone Java application, including the following:

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

Specialized Programme on Web Application Development using Open Source Tools

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

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

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

WIRIS quizzes web services Getting started with PHP and Java

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

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

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

FileMaker 11. ODBC and JDBC Guide

Heterogeneous Tools for Heterogeneous Network Management with WBEM

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Fundamentals of Java Programming

What Perl Programmers Should Know About Java

Database Programming with PL/SQL: Learning Objectives

Chapter 1 Fundamentals of Java Programming

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Introduction to Java Applets (Deitel chapter 3)

Syllabus for CS 134 Java Programming

Japan Communication India Skill Development Center

JavaScript By: A. Mousavi & P. Broomhead SERG, School of Engineering Design, Brunel University, UK

Real SQL Programming 1

Package sjdbc. R topics documented: February 20, 2015

Programming Database lectures for mathema

Japan Communication India Skill Development Center

Java Server Pages and Java Beans

Web Development and Core Java Lab Manual V th Semester

Tutorial for Spring DAO with JDBC

Oracle8/ SQLJ Programming

Web Development in Java

Accessing Your Database with JMP 10 JMP Discovery Conference 2012 Brian Corcoran SAS Institute

CPLEX Tutorial Handout

Glassfish, JAVA EE, Servlets, JSP, EJB

No no-argument constructor. No default constructor found

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

Cache Database: Introduction to a New Generation Database

Getting Started with Web Applications

Chapter 1 Programming Languages for Web Applications

Java EE Web Development Course Program

Introduction to Java

Java applets. SwIG Jing He

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Spring,2015. Apache Hive BY NATIA MAMAIASHVILI, LASHA AMASHUKELI & ALEKO CHAKHVASHVILI SUPERVAIZOR: PROF. NODAR MOMTSELIDZE

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Database Access Through Java Technologies

A Brief Introduction to MySQL

Building Web Applications, Servlets, JSP and JDBC

ODBC Client Driver Help Kepware, Inc.

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

Object Oriented Software Design

Applications of JAVA programming language to database management

JAVA Web Database Application Developer

Tutorial: Building a Web Application with Struts

An Eclipse Plug-In for Visualizing Java Code Dependencies on Relational Databases

Enterprise Java. Where, How, When (and When Not) to Apply Java in Client/Server Business Environments. Jeffrey Savit Sean Wilcox Bhuvana Jayaraman

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

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

Japan Communication India Skill Development Center

Object Oriented Software Design

Building a Multi-Threaded Web Server

FileMaker 14. ODBC and JDBC Guide

Session 12 Evolving IT Architectures: From Mainframes to Client-Server to Network Computing

Transcription:

Advanced Features Trenton Computer Festival May 1 sstt & 2 n d,, 2004 Michael P.. Redlich Senior Research Technician ExxonMobil Research & Engineering michael..p..redlich@exxonmobil..com

Table of Contents TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...6 JAVA 2 COLLECTIONS...8 REFERENCES FOR FURTHER READING...8 2

1 Introduction Java offers all of the advantages of object-oriented programming (OOP) by allowing the developer to create user-defined data types for modeling real world situations. However, the real power within Java is contained in its features. Four main topics will be covered in this document: Applets and Applications JavaBeans Exception handling Java Database Connectivity API There will also be an introduction to the Java 2 Collections. An example Java application was developed to demonstrate the content described in this document and the Introduction to Java document. The application encapsulates sports data such as team name, wins, losses, etc. The source code can be obtained from http://tcf.redlich.net/. 2 Applets and Applications A Java applet requires the use of a browser, and is invoked within the <applet></applet> HTML tag pair. The bytecode is executed with the JVM built-in to the browser. The initial point of execution is within a method called init(). A Java application is standalone and is normally executed from the command line using the local JVM. The initial point of execution is within a static method called main(). The method signature is. public static void main(string[] args)... This is, of course, similar to the C/C++ programming languages. However, the parameter list for handling command line arguments can be omitted in C/C++, but the parameter list must be supplied even if there is no intention of using command line arguments. Also missing from the parameter list is an integer for the number of arguments. It is possible to write a Java program that can be used as an applet and an application. It must, of course, contain both init() and main() methods. The following diagram describes the process of developing an applet: Text Editor Browser Applet Viewer.java file javac.class file.html file Text Editor 3

The methods required for developing an applet are shown in the general source code example below: // Java applet source code import java.applet.*; import java.awt.graphics;... public class MyApplet extends Applet implements Runnable public void MyApplet() // construction... public void init() // initialization... public void start() // starting... public void stop() // stopping... public void destroy() // destroying... public void paint(graphics G) // painting... // HTML file that invokes applet <html> <head> <title>my Applet</title> </head> <body> <hr> <applet code="myapplet.class" width="500" height="50"> <param name="text" value="welcome to my applet!"> </applet> <hr> </body> </html> 3 JavaBeans JavaBeans (or just Beans) is a method (sic) for developing reusable Java components that can be used in web applications, most notably within Java Server Pages (JSPs). Beans easily store and exchange information. In order for a Java class to be a bean, it must be developed according to the JavaBean specification: implements the Serializable interface contains a default constructor (for JSP pages) contains getter/setter methods for all the class members The bean must implement the Serializable interface so that the bean's current state can be written to disk and recreated between web server restarts. 4

A default constructor is required when the bean will be used within a JSP page. The getter and setter methods establish (set) and return (get) the current values of all the class members. These methods must follow a standard naming convention that is relative to each of the class members. The first letter of each class member is in lower case. The corresponding getter and setter methods start with the terms, get and set, and are completed with the member name containing an upper case first letter. The term, is, may be used for a getter method that returns a boolean value. For example: public class SportsBean implements Serializable private int win; private boolean empty; public Sport() public int getwin() return win; public void setwin(int win) this.win = win; public boolean isempty() return empty; public void setempty(boolean empty) this.empty = empty; In this example, the getter and setter methods for the class member, win, is getwin() and setwin() respectively. 4 Exception Handling Detecting and handling errors within an application has traditionally been implemented using return codes. For example, a function may return zero on success and non-zero on failure. This is, of course, how most of the standard C library functions are defined. However, detecting and handling errors this way can become cumbersome and tedious especially in larger applications. The application's program logic can be obscured as well. The exception handling mechanism in Java is a more robust method for handling errors than fastidiously checking for error codes. It is a convenient means for returning from deeply nested function calls when an exception is encountered. Unlike C++, exception handling was built-in to the Java programming language from the very beginning. Exception handling is implemented with the keywords try, throw, and catch. An exception is raised with a throw-expression at a point in the code where an error may occur. The throw-expression has the form: throw T; 5

where T can be any data type for which there is an exception handler defined for that type. A try-block is a section of code containing a throw-expression or a function containing a throw-expression. A catch clause defined immediately after the tryblock, handles exceptions. More than one catch clause can be defined. For example: public class ExceptionTest public static void main(string[] args) try initialize(); catch(exception exception) exception.printstacktrace(); public void initialize() throws Exception // contains code that may throw an Exception // type as specified The Java library contains an extremely exhaustive list of defined exceptions for all types of errors. Most exceptions are checked, i.e., the compiler enforces exception handling. If a certain method call is made without it being placed in a try block, the compiler will flag this as an error. The compiler does not enforce unchecked exceptions. Exceptions should be thrown for things that are truly exceptional. They should not be thrown to indicate special return values. 5 Java Database Connectivity (JDBC) The Java Database Connectivity (JDBC) API allows the developer to easily connect to, read, and manipulate popular databases (Microsoft Access and SQL Server, Oracle, etc.), spreadsheets, and flat files for use in applications. The JDK supplies a built-in driver for these databases that is used in conjunction with an Open Database Connectivity (ODBC) connection to a particular database. Other, more complex drivers (ones that don't require an ODBC connection, for example) must be obtained from a vendor, and referenced within the application. With only a few lines of code, a connection to a database can be made, a query can be executed, and a result set can be displayed: 6

public class DBTest static public void main(string[] args) String sql = "SELECT * FROM tbltimezones; try Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection connection = DriverManager.getConnection("jdbc:odbc:timezones","",""); Statement statement = connection.createstatement(); ResultSet result = statement.executequery(sql); while(result.next()) System.out.println(result.getDouble(1) + " " + result.getdouble(2)); connection.close(); catch(sqlexception exception) The driver for most databases using an ODBC connection can be accessed via the built-in JdbcOdbcDriver that is included in the Java Development Kit. The example above assumes that a database exists with an ODBC connection named timezones, which is referenced in the statement: Connection connection = DriverManager.getConnection("jdbc:odbc:timezones","",""); and returns an instance of type Connection. The empty strings in the second and third parameters of the getconnection() method are used for passing the userid and password of the database if they exist. The connection object is then used to return an instance of type Statement as in the statement: Statement statement = connection.createstatement(); The Statement object is used to ultimately obtain the desired result set based on a given query. In the example above, the query, "SELECT * FROM tbltimezones" is established as a string, and can be passed into the Statement object's executequery() method. A ResultSet object is returned upon a successful execution of the query. Examining all the rows of the result set is handled through a simple while loop: while(result.next()) System.out.println(result.getDouble(1) + " " + result.getdouble(2)); The ResultSet object's next() method returns a boolean to indicate if another row of data is available. The two calls to the getdouble() method assumes that values of type double are returned in columns 1 and 2 of the result set. Getter methods for all the built-in data types have been defined in ResultSet. Other examples include getint() and getstring(). The columns of the result set are one-based (as opposed to zero-based), that is the value of first column in each row is retrieved by getdouble(1) (or getstring(1) or getint(1)), and so on. The only drawback to the above example is that the data types for the columns of the result set must be known in advance. If the schema of the database is changed, the above code can break. Other options are available, such as calling stored procedures and obtaining the result sets meta data. 7

6 Java 2 Collections this section is under construction 7 References for Further Reading The references listed below are only a small sampling of resources where further information on Java can be obtained: Thinking in Java (book) Bruce Eckel ISBN http://www.bruceeckel.com/ Java Developer's Journal (monthly periodical) http://www.javadevelopersjournal.com/ Core Java 2, Volume I - Fundamentals (book) Cay S. Horstmann and Gary Cornell ISBN 0-13-081933-6 http://www.sun.com/books/catalog/horstmann6/ Core Java 2, Volume II - Advanced Features (book) Cay S. Horstmann and Gary Cornell ISBN 0-13-081934-4 http://www.sun.com/books/catalog/horstmann7/ The Java Tutorial for the Real World (book) Yakov Fain ISBN 0-9718439-0-2 http://www.smartdataprocessing.com/ 8