Client/Server Database Systems Part 1 Java Primer COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 1 Client/Server Client/ServerDBS DBSand andJava: Java:What Whatwe wewill willcover cover?? ) Overview of Client/Server Systems ) Middleware ) Java COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 2 References References • (OHE) Orfali,R., Harkey, D. and Edwards, J. (1999) Client/Server Survival • • • • Guide (Third Edition). John Wiley and Son. Ch.11 p.237-249 (CDK) Coulouris, G., Dollimore, J. and Kindberg, T. (2001) Distributed Systems – Concepts and Design (Third Edition). Addison-Wesley (OV) Ozsu, M.T., and Valduriez, O. (1999) Principles of Distributed Database Systems (Second Edition). Prentice-Hall. www.javasoft.com Orfali, R. and Harkey, D (1998) Client/Server Programming with JAVA and CORBA (2nd Ed.) Wiley Ch. 23, 25 COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 3 DBMS DBMSImplementation ImplementationAlternatives Alternatives Distribution Logically integrated and homogeneous multiple DBMSs Distributed homogeneous DBMS Distributed homogeneous federated DBMS Distributed homogeneous multidatabase system Distributed heterogeneous DBMS Autonomy Distributed heterogeneous federated DBMS Multidatabase system Single site Heterogeneous homogeneous integrated DBMS Single site federated DBMS Heterogeneous multidatabase heterogeneous Heterogeneity system federated DBMS COT5200: DISTRIBUTED DATABASE SYSTEMS Distributed heterogeneous multidatabase system Client/Server Database Systems 4 Heterogeneity Heterogeneity • Differences in ♦ networks ♦ hardware ♦ operating systems ♦ programming languages ♦ implementation by different developers • Overcome by standards • Middleware is the embodiment of standards ♦ provide a mask over heterogeneity ♦ provides a uniform computation model AUTONOMY • Execution Autonomy • Design Autonomy COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 5 Transparency Transparency • Access transparency • • • • • • • ♦ local and remote resources access the same way Location or network Concurrency Replication Failure Mobility or migration Performance Scalability COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 6 C/S C/Sand andHeterogeneity Heterogeneityand andAutonomy Autonomy • D,I,Ho - Distributed Oracle ⇒ Network ⇒ OS ⇒ Hardware • D,I,He - Oracle Gateway Technology ⇒ DBMS • D,A,Ho - ODBC, Gateway Technology ⇒ DBMS ⇒ Data Model ⇒ Control connection properties and management within program • D,A,Ho - DOT, TPMS ⇒ Design ⇒ Provides services to control and manage connection properties COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 7 Styles Stylesof ofClient ClientServer ServerComputing Computing Distributed Presentation Data Management Remote Presentation DM Distributed Logic Remote Data Management DM DM ODBC/JDBC Application Logic Presentation AL Servlets JSP Presentation AL P COT5200: DISTRIBUTED DATABASE SYSTEMS AL P DM Distributed Oracle DM CORBA Stored Procedures EJB Thin Client 2T+ TPMS 2 T or DOT 3T Distributed Database Fat Client 2T AL AL P P Client/Server Database Systems 8 Software Softwarelayers layers • Software architecture ♦ the structuring of software as layers and more recently in terms of services offered and requested between processes • Services are implemented via protocols • FIG 2.1 from CDK Applications, services Middleware Operating System Computer and network hardware COT5200: DISTRIBUTED DATABASE SYSTEMS Platform Client/Server Database Systems 9 Middleware Middleware • Software which is an intermediary between the client and the database server. • Provides an API (application programming interface) on both sides which enables client and server to talk to each other. • Allows a standard approach to database access between disparate servers and client types. • “Database driver” similar to device driver software. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 10 55 Technologies Technologies to to Create Create C/S C/S • Database Servers ♦ ODBC/JDBC • Transaction Processing Monitors (TPMS) ♦ CICS/Encina/Tuxedo • Groupware ♦ Lotus Notes/Microsoft Exchange • Distributed Objects ♦ Oracle Application Server 4.0, CORBA, COM • Web Application Servers ♦ Websphere, Microsoft Transaction Server COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 11 Java Java • Overview • Classes ♦ declaring classes ♦ declaring methods • Constructors • Inheritance • Java syntax • Data types • Invoking Methods • Flow of control statements COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 12 References References • OHE 1999 p. 594 ff • CH 1999 Ch. 3 • www.javasoft.com COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 13 Java Java Overview Overview :: History History • James Gosling, the creator of Java • In the spring of 1991 the team members adopt the name Green for the project and break off almost all communication with the rest of Sun. The prototype hardware for the Green project is called Star-Seven. • The mission of the Green Project as stated in its business plan is ♦ To develop and license an operating environment for consumer devices that enables services and information to be persuasively presented via the emerging digital infrastructure. • Much of the syntax of Java was based on the C++ programming language. Many of its dynamic object oriented concepts evolved from SmallTalk, Objective C, and Lisp. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 14 Java Overview JavaOveriew:History Overview :: History History Java • • • • • Gosling begins extending the C++ compiler to make it more platform independent. He realizes that this approach will not be sufficient and develops a new interpreter nicknamed Oak. By the summer of 1994 the team realizes that the platform independent technology they have developed can add some "really cool" features to a World Wide Web browser, so they begin working on the WebRunner browser. In the fall of 1994 the WebRunner browser, renamed HotJava, is demonstrated for Sun's executives. Java and HotJava are formally announced in May of 1995 at SunWorld '95. Since Java was introduced to the world, it has been evolving and expanding at an ever increasing rate. The first version of the Java Developers Kit, JDK 1.0, was released in November 1995. In December 1996, the Java API's acquired an extensive set of new capabilities, with the release of JDK 1.1. The Java Developer's Kit (JDK) from Sun Microsystems is a set of tools which allows you to create, compile, test, and debug your Java programs. It also includes a standard set of packages that are included in any Java Virtual Machine. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 15 Advantages Advantagesof ofJava Java • • • • • • • • • • Simple - C++ minus complexity Robust Architectural-Neutral Portable Dynamic High-Performance Interpreted Object-Oriented Familiar Threaded COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 16 Object Object Orientation Orientation and and Java Java • • • • • • Easier to maintain and reuse code from programs which have been developed based on the principles of OOP ♦ inheritance ♦ overriding ♦ overloading Objects - are software bundles that combine data with operations that relate to the data. Software objects often relate to or model real world objects. Objects are created based on a template called a class. Classes - are a template or blueprint for creating an object. A class defines what types of data are included in the object and specifies the operations the object performs. Encapsulation is the process shielding an object's data from direct outside access. (Data Hidding) Inheritance - A new class can be derived from an existing class. The new class inherits all the properties from the existing class Polymorphism The ability of a method to perform different actions depending on what object it is applied to is called polymorphism. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 17 Event EventDriven DrivenProgramming Programming • User and system actions trigger events that are said to be handled by objects in the computer program. • Examples of events include clicking the mouse, dragging the mouse, and pressing a key on the keyboard. • Objects of any type can register as event listeners. Event notification properties are encapsulated in Event objects. Event notifications are propagated from Source to Listener via a method call on the Listener. • These methods are grouped in EventListener interfaces that inherit from java.util.EventListener. • Normal event delivery is multicast. Example Registering interest in a mouse event public void button1_mouseClicked(MouseEvent e) { Dialog1 dialog1 = new Dialog1(this); dialog1.show(); } COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 18 Objects Objects • • • • • Objects have attributes - properties Object have state can also be described in terms of the state they are in. An objects attributes and its state are described by instances of variables Software objects interact and communicate with each other. When object A wants object B to perform a task, it must cause one of object B's methods to be executed. To communicate between objects requires three things. First you must know the name of the object whose method is to be called. Second, you must know the name of the method itself. Lastly, you must supply any information [parameters] the method requires to perform its tasks properly. No other information or context is required. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 19 Classes Classes • A class is a template or blueprint for creating a software object. A class • • • defines the types of data that will be included in the object and specifies the operations the object will perform. Creating an object from a class is known as instantiating an object. INHERITANCE When you extend a subclass from a superclass, you do not actually copy the superclass to the subclass, but the data and methods belonging to the existing superclass are directly available to the subclass. If an object created from a subclass receives a message to change gears, the Change Gears method of the superclass is called upon to perform the task. Inheritance also provides a mechanism by which methods of the superclass can be overridden or replaced with new methods in the subclass. This allows you to inherit some but not all of the behavior of the superclass. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 20 Java Java System System Architecture Architecture • After you write a Java program, you pass the program through the Java • • • compiler. The compiler then reads the statements in the program and translates them into a machine independent format called bytecode. Bytecode files, which are very compact, are easily transported through a distributed system like the Internet. You load and run the resulting bytecode file to execute the Java program. The bytecode file is then passed to the verifier to ensure that it can execute correctly without violating any of Java's rules or security considerations. Any bytecode file that fails this rigorous scrutiny is rejected at this point. Java bytecode is interpreted by the Java Virtual Machine. Interpretation of program code by another program is slower than directly executing processor instructions. To speed execution, some Java environments include a "Just In Time" compiler that translates bytecodes to processor instructions before execution. This can dramatically increase processing speed of Java program code, but will generally have no effect on network load speed. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 21 JAVA JAVAClasses Classes Syntax: <classModifier> class className [ <extends className> <implements interfaceList>] { <memberDataList>; <memberFunctionsList>; } Example public class HelloWorld extends Object { /** * main * @param args */ public static void main(String[] args) { System.out.println (”Hello World."); } } COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 22 JAVA JAVAClasses Classes public class HelloWorld extends Object { /** * Constructor */ public HelloWorld() {} static void hello() {System.out.println ("Hello World"); } /** main * @param args */ public static void main(String[] args) { HelloWorld hello1 = new HelloWorld(); hello1.hello(); } } COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 23 Methods Methods return-type method-name (<parameter-list>) { [statements;] } public void displayHelloWorld() { System.out.println (”Hello World."); } • A method may return a value to the object that invoked the method. The data type of the value that the method returns must be specified. If the method does not return any value, the keyword void is used. • If an access modifier is needed to set the method's visibility, it must precede the return type. • The method name is followed by a parameter list which must be enclosed within parentheses. The parentheses must be present even if no parameters are required. • The parameters of the parameter list are separated by commas. Each parameter must include the type of the parameter followed by a variable name to receive the value of the parameter. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 24 Invoking Invoking Methods Methods public class HelloWorld extends Object { /** Constructor */ public HelloWorld() {} String helloVar; void hello(String helloVar) {System.out.println (helloVar); /** main * @param args */ public static void main(String[] args) { String helloVar = "Hello World"; HelloWorld hello1 = new HelloWorld(); hello1.hello(helloVar); } } COT5200: DISTRIBUTED DATABASE SYSTEMS } Client/Server Database Systems 25 Constructors Constructors • A constructor is a method that dynamically initializes the instance • • variables of an object. A constructor has the same name as the name of the class to which it belongs and is called automatically each time an object of that class comes into existence. The operator "new" followed by a constructor invokes the constructor. The name of the constructor is always the same as the name of the class that contains the constructor. The new operator returns a reference to a brand new object unless the program was unable to find available memory for allocating the new object. Notice that the constructor's syntax does not include a return type, since constructors never return a value. Also, constructors are not invoked the way other methods are invoked. They cannot be called directly, but must be invoked in the process of creating a new object. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 26 Packages Packages • • • • • • Java includes standard software libraries called packages that execute specific commands. Packages are a way to group Java classes together into more manageable pieces. You can import the packages you need. Java will find the proper bytecode files and make them available to your program. There are two varieties of import statements: a single type import and a type import on demand. Java package names are translated into a pathname on the local system where the files reside. This is done by taking each part of the package name and placing a file separator between them. For example, the name COM.mindq.shapes translates to the directory name COM\mindq\shapes. The CLASSPATH environment variable specifies the root directory for all classes. For example, if the CLASSPATH environment variable is set to C:\classes, then the complete path name for the Shape class is: C:\classes\COM\mindq\shapes\Shape.class. This is where the Java compiler will find the Shape class. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 27 Packages Packages • The import statement loads a Java package. The package that defines a • • • basic applet is java.applet. This package must be imported before an applet can be created. The java.awt package is a standard Java windowing package called the Abstract Windowing Toolkit (AWT). package Kathpack; import minq.login; import java.awt.event.*; COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 28 Access AccessModifiers Modifiers • Java provides a number of access modifiers to help you set the level of • • • access you want for classes as well as the fields, methods and constructors in your classes. There are three specified and one default level of access. As you move down the levels, wider access is provided. Fields, methods or constructors declared private are strictly controlled, which means they provide no access outside their class. Java also provides a default access which is used when no access modifier is present. Java's package structure determines accessibility when default access is used. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. Public classes as well as the fields, methods and constructors declared public within a public class are visible to any class in the Java program, whether these classes are in the same package or in another package. In order to access classes, members, and constructors in other packages, the name of the element and a path may have to be provided. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 29 Access AccessModifiers Modifiers • The access to classes, constructors, methods and fields are regulated • • • • using access modifiers. You use access modifiers to enforce a modular structure on your Java code. To take advantage of encapsulation, you should minimize access whenever possible. private : fields, methods or constructors declared private are strictly controlled, which means they provide no access outside their class. protected; fields, methods and constructors declared protected in a superclass can be accessed by subclasses. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member's class. default = friendly, package fields, methods and constructors declared protected in a superclass can be accessed by subclasses. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member's class. Public COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 30 Inheritance Inheritance • Each subclass inherits the state and behavior of the superclass. For • • • example, subclasses derived from the vehicle class would inherit instance variables that contain data about speed and fuel. The subclasses would also inherit the behaviors, or methods, of the vehicle class: moving, braking, and changing gears. FINAL - stops overidding When you don't want to allow changes to your information. Java allows you to mark fields, methods and classes as final. Once marked as final, these items cannot be overridden. Combining Java's ability to create class variables with its ability to mark variables as final, provides a powerful way to create constants. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 31 Java JavaSyntaxSyntax-Keywords Keywords • • • • • • • • • • • • • • • abstract boolean break byte byvalue case cast catch char class const continue default do double COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 32 Java Javasyntax syntax • Keywords in Java are case sensitive and must all be lower case. • The semicolon, which is used as a statement terminator, notifies the • • • • compiler that a statement has completed. Comments Comments between /** and */ are designed for capture by automatic documentation generation tools. such as javadoc. Block style comments begin with /* and terminate with */. They can span multiple lines. Line style comments begin with // and terminate at the end of the line. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 33 Variables Variables • Variable names, also known as identifiers, must be unique within each • • • • section or scope of the Java program Variables also have a type, which indicates the kind of information they can store. Java contains built-in types for storing simple kinds of data, such as integers and characters. But you can also create variables that store references to more complex data which is stored in the form of objects. You declare a variable by specifying its data type and by assigning it a name. If you don't specify a value when the variable is declared, it will be assigned the default value for its data type. You can place a variable declaration anywhere within a program, as long as you declare the variable before using it. int radius = 0; COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 34 Java JavaPrimitive PrimitiveData DataTypes Types • Integer ♦ byte ♦ short ♦ int ♦ long • Floating Point ♦ float ♦ double • Character - 16 bit unicode • Boolean COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 35 Strings Strings • Strings in Java are very different than in the C programming language. Strings are objects of the java.lang.String class. Strings, as with arrays, are another object with special syntax. The Java compiler treats Strings almost like a primitive type. A double-quoted constant causes the compiler to create a String class object automatically. The keyword new is therefore not required. Garbage Collection • The address, or location, of an object is stored in a reference variable. Objects can be referenced by any number of reference variables. When there are no references remaining for an object, Java will automatically destroy the object and free its space to be reused. This is known as garbage collection. • Ball noBall; • Ball yesBall = new Ball ( ); COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 36 Type TypeConversion Conversion • Type conversion allows a value to be changed from one primitive data • type to another. Conversion can occur explicitly, as specified by the program, or implicitly, by Java itself. Conversions occur under four circumstances: the programmer uses a type cast operator, explicit promotion, an arithmetic operator is used with arguments of different data types, arithmetic promotion, a value of one type is assigned to a value of a different type, assignment promotion, or a method is called with argument types that differ from the expected types, also assignment promotion. Assignment promotion and arithmetic promotion only perform type widening promotions, meaning that no loss of information will occur. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 37 Identifier IdentifierNaming NamingRules Rules • User created items, such as variables and classes, are identified by their • • • • • name, or identifier. Names can consist of upper and lower case letters, digits, dollar sign ($) and the underscore ( _ ) character Names must begin with a letter, dollar sign, or an underscore Names are case sensitive Keywords cannot be used as identifiers Java specifically requires that names can be of any length, with no limit imposed by the compiler COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 38 Specific Specificnaming namingconventions conventionswithin withinyour yourJava Javasource sourcecode code • Local variable and parameter names should be short meaningful • • • • • sequences of lowercase letters. Class and Interface names should be descriptive noun or noun phrases, in mixed case, with the first letter of each word capitalized. Field names that are not final should be mixed case, with a lowercase first letter and the first letter of subsequent words capitalized. The names of constants in interfaces should be and final variables of classes may be all uppercase, with components separated by underscore ( _ ) characters. Names of packages that are only for local use should have a first component that begins with a lowercase letter. However, that component should not be the identifier java. Method names should be descriptive verbs or verb phrases, in mixed case, with the first letter of the first word lowercase, and the first letter of each subsequent word capitalized. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 39 Variables: Variables:Scope Scopeand andLife Lifespan span • The lifetime of instance variables is the same as the lifetime of the object. • • • The fields of a class are available to all methods of that class. The lifetime of a class variable begins when the class is loaded and ends when the class is unloaded. A variable declared within a method or a variable that is passed as a parameter to a method has a local scope. You can use one of these local variables within a method after you declare it. The local variable is created when the method is called and is discarded when the method exits. Variables declared within a code block, that is, between curly braces, also has a local scope; the variable can be used only within its own block. It is discarded when the block exits. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 40 Literals Literals • • • • • • • Values you enter directly into a program are called literals Numeric literals don’t code 03 as this is octal or 0X3 as this hex character literals You specify a character literal by enclosing the text character within single quotation marks. The backslash represents an escape sequence. String literals A string literal is enclosed in double quotation marks. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 41 Modifying ModifyingData DataValues: Values:Operators Operators • Assignment ♦ a = 3 + 1; as long as a is not final • Java provides seven arithmetic operators. They are for addition, • subtraction, multiplication, division, modulo (remainder), increment (add 1), decrement (subtract 1), and minus. The increment and decrement operators have prefix and postfix versions. Java provides six relational operators. They are: greater than, less than, greater than or equal, less than or equal, equal, and not equal. The first four operators take only arithmetic arguments. The last two operators take two arithmetic arguments, two boolean arguments or two reference type arguments. All of the operators return a boolean value of true or false. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 42 Control ControlFlow Flowstatements statements • • • • • • • • • If {} else do while switch for break - statement transfers control out of the enclosing for, while, do or switch statement. continue - statement stops the iteration of a while, do or for loop and causes execution to resume at the top of the nearest enclosing loop. Labels - the label name is optional, and is usually only used when you wish to return to the outermost loop in a series of nested loops. COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 43 Do/While Do/While • while • An important point to notice with a while loop is that if the test expression is false, the loop will never be executed. • do/while • The do/while loop is similar to the while loop, except that the test is performed at the end of the loop instead of at the beginning. This ensures that the loop will be executed at least once. do { statement; } while ( testExpression ); COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 44 try/catch try/catchblock block • The throw statement is used when an exception is detected and an object associated with the exception is to be referenced. The statement throw interrupts the running thread and continues execution at the associated catch statement. void runExample() throws SQLException {System.out.println( "Running the example." ); Try #sql { DELETE FROM SALES }; catch(SQLException e); { System.out.println("exception: " + e.getMessage()); e.printStackTrace(); } COT5200: DISTRIBUTED DATABASE SYSTEMS Client/Server Database Systems 45