j2ee_v1 1

Upload: chetanjec

Post on 05-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 J2EE_v1 1

    1/88

    Version 1.1 1

    Module 2: JavaScript

    The lessons covered in this module include:

    DHTML

    Introduction to JavaScript

    Elements of JavaScript Program

    JavaScript Statements

    Functions

    Objects

    Defining Objects

    Arrays

    Events

    Time Outs

    Integrating JavaScript with Java

    Creating Windows

    Summary

    Examples and Exercises

  • 7/31/2019 J2EE_v1 1

    2/88

    Version 1.1 2

    Objectives

    At the end of this module you will be able to:

    1. Write JavaScript code using all the basic elements of JavaScriptPrograms

    2. Create Windows & Dialog Boxes

    3. Use the JavaScripts in-built objects in a web page

    4. Write code that does Event Handling in HTML pages5. Manipulate HTML Forms through JavaScript dynamically

    6. Integrate Java and JavaScript through Applets

  • 7/31/2019 J2EE_v1 1

    3/88

    Version 1.1 3

    DHTML

    DHTML stands for Dynamic Hyper Text Markup Language which

    helps to add Dynamic content to static HTML pages.

  • 7/31/2019 J2EE_v1 1

    4/88

    Version 1.1 4

    Introduction to JavaScript

    JavaScript is:

    1. An easy to use object scripting language

    2. Designed for creating live online applications

    3. Code is included as part of a HTML document

    4. Scripts are run by the Web browser

  • 7/31/2019 J2EE_v1 1

    5/88

    Version 1.1 5

    Introduction to JavaScript: JavaScript Versus Java

    1. JavaScript can be combined directly with HTML

    2. The JavaScript language structure is simpler than that of Java

    3. JavaScript is a purely interpreted language

    4. The JavaScript interpreter is built into a Web browser

  • 7/31/2019 J2EE_v1 1

    6/88

    Version 1.1 6

    Introduction to JavaScript: Using the SCRIPT Tag

    Simple JavaScript Example

    HTML Text goes here.

    document.write("Here is my output.")

  • 7/31/2019 J2EE_v1 1

    7/88Version 1.1 7

    Elements of JavaScript Program

    Elements of JavaScript Program can be divided into five categories, as

    follows:

    1. Variables2. Expressions3. Control Structures

    4. Functions5. Objects and Arrays

  • 7/31/2019 J2EE_v1 1

    8/88

  • 7/31/2019 J2EE_v1 1

    9/88Version 1.1 9

  • 7/31/2019 J2EE_v1 1

    10/88Version 1.1 10

    JavaScript Statements

    Conditional statements: if...else and switch

    Loop Statements: for, while, dowhile, break and continue

    Object Manipulation Statements and Operators: for...in, new, this

    and with

    Comments: single-line (//) and multi-line (/*...*/)

  • 7/31/2019 J2EE_v1 1

    11/88Version 1.1 11

    JavaScript Statements: switch Statement

    A switch statement allows a program to evaluate an expression and

    attempt to match the expression's value to a case label. If a match isfound, the program executes the associated statement. A switchstatement looks as follows:

    switch (expression){

    case label :

    statement;

    break;

    default : statement;

    }

  • 7/31/2019 J2EE_v1 1

    12/88

    Version 1.1 12

    JavaScript Statements: for Statement

    A for loop repeats until a specified condition evaluates to false. A

    for statement looks as follows:

    for ([initial-expression]; [condition]; [increment-expression]) {

    statements

    }

  • 7/31/2019 J2EE_v1 1

    13/88

    Version 1.1 13

    JavaScript Statements: dowhile Statement

    The do...while statement repeats until a specified condition

    evaluates to false. A do...while statement looks as follows:

    do {

    statement

    } while (condition)

  • 7/31/2019 J2EE_v1 1

    14/88

  • 7/31/2019 J2EE_v1 1

    15/88

    Version 1.1 15

    Functions

    Functions are one of the fundamental building blocks in JavaScript.

    A function is a JavaScript procedure: a set of statements thatperforms a specific task. To use a function, you must first define it,then your script can call it. A function definition looks as follows:

    function gcd(m,n){

    return n > 0 ? gcd(n,m%n) : m ;

    }

  • 7/31/2019 J2EE_v1 1

    16/88

    Version 1.1 16

    Objects

    An object is a self-contained unit of code having the following

    characteristics: Properties

    Methods

    Identity

  • 7/31/2019 J2EE_v1 1

    17/88

    Version 1.1 17

    Objects: The Window Object

    At the top of the browser hierarchy is the window object, which

    represents a browser window

    The properties/methods of this object are:

    status

    alert()

    confirm()

    prompt()

  • 7/31/2019 J2EE_v1 1

    18/88

    Version 1.1 18

    Objects: The Document Object

    Represents characteristics of the current HTML page.

    Some of its properties are:

    title - lastModified

    fgColor - bgColor

    Some of its methods are: write()

    writeln()

    In the browser object hierarchy, the document object is contained ina window object (for a page without frames)

  • 7/31/2019 J2EE_v1 1

    19/88

    Version 1.1 19

    Objects: The Form Object

    Represents an HTML Form.

    Has the same name as the NAME attribute in the FORM tag

    In the browser object hierarchy, the form object is contained in the

    document object

  • 7/31/2019 J2EE_v1 1

    20/88

    Version 1.1 20

    Objects: Frame Objects

    Each Frame in a frame set is represented as a frame object

    A frame set contains an array of frame objects representing all theframes in it

    You can refer to a particular frame : By name - if it has one

    By reference to the parent and the array index of that frame

  • 7/31/2019 J2EE_v1 1

    21/88

    Version 1.1 21

    Objects: The Math Object

    The Math object cant be created, since it exists automatically in all

    Java Script Programs Its properties represent mathematical constants

    Its methods are mathematical functions

  • 7/31/2019 J2EE_v1 1

    22/88

    Version 1.1 22

    Objects: The String Object

    Any String variable in JavaScript is a String object. It has a property

    Length and

    Many Methods

  • 7/31/2019 J2EE_v1 1

    23/88

    Version 1.1 23

    Objects: The Date Object

    Is built-in JavaScript object

    Create using new keyword

  • 7/31/2019 J2EE_v1 1

    24/88

    Version 1.1 24

  • 7/31/2019 J2EE_v1 1

    25/88

    Version 1.1 25

    Arrays

    JavaScript doesnt support array variables

    Arrays need to be created using array object

  • 7/31/2019 J2EE_v1 1

    26/88

    Version 1.1 26

    Events

    Are things that happen to the browser

    Used to trigger portions of program

    Pertain to the web page containing the script

  • 7/31/2019 J2EE_v1 1

    27/88

    Version 1.1 27

    Events: Event Handlers

    Embedded in HTML tags as part of anchor and links or any of the

    form element tags.

  • 7/31/2019 J2EE_v1 1

    28/88

    Version 1.1 28

    Time Outs

    Statements that will be executed after a certain amount of time

    elapses

    Handy for periodically updating a Web Page or for delaying thedisplay of a message or execution of a function

  • 7/31/2019 J2EE_v1 1

    29/88

    Version 1.1 29

  • 7/31/2019 J2EE_v1 1

    30/88

    Version 1.1 30

    Summary

    In this module you have learnt to:

    Write JavaScript code using all the basic elements of JavaScriptPrograms

    Create Windows & Dialog Boxes

    Use the JavaScripts in-built objects in a web page

    Write code that does Event Handling in HTML pages Manipulate HTML Forms through JavaScript dynamically

    Integrate Java and JavaScript through Applets

  • 7/31/2019 J2EE_v1 1

    31/88

    Version 1.1 31

  • 7/31/2019 J2EE_v1 1

    32/88

    Version 1.1 32

  • 7/31/2019 J2EE_v1 1

    33/88

    Version 1.1 33

  • 7/31/2019 J2EE_v1 1

    34/88

    Version 1.1 34

  • 7/31/2019 J2EE_v1 1

    35/88

    Version 1.1 35

  • 7/31/2019 J2EE_v1 1

    36/88

    Version 1.1 36

  • 7/31/2019 J2EE_v1 1

    37/88

    Version 1.1 37

  • 7/31/2019 J2EE_v1 1

    38/88

    Version 1.1 38

  • 7/31/2019 J2EE_v1 1

    39/88

    Version 1.1 39

  • 7/31/2019 J2EE_v1 1

    40/88

    Version 1.1 40

  • 7/31/2019 J2EE_v1 1

    41/88

    Version 1.1 41

  • 7/31/2019 J2EE_v1 1

    42/88

    Version 1.1 42

  • 7/31/2019 J2EE_v1 1

    43/88

    Version 1.1 43

  • 7/31/2019 J2EE_v1 1

    44/88

    Version 1.1 44

    M d l 3 JDBC

  • 7/31/2019 J2EE_v1 1

    45/88

    Version 1.1 45

    Module 3: JDBC

    The lessons covered in this module include:

    Introduction to JDBC Architecture & Querying with JDBC

    Stage 1: Connect

    Stage 2: Query

    Stage 3: Process the Results

    Stage 4: Close

    The DatabaseMetaData Object

    The ResultSetMetaData Object

    Mapping Database Types to Java Types

    The PreparedStatement Object

    The CallableStatement Object

    Using Transactions Summary of JDBC Classes

    Summary

    Examples

    Obj ti

  • 7/31/2019 J2EE_v1 1

    46/88

    Version 1.1 46

    Objectives

    At the end of this module you will be able to:

    Connect to a database using Java Database Connectivity (JDBC) Create and execute a query using JDBC

    Invoke prepared statements

    Commit and roll back transactions

    Use the Metadata objects to retrieve more information about thedatabase or the resultset

    I t d ti t JDBC

  • 7/31/2019 J2EE_v1 1

    47/88

    Version 1.1 47

    Introduction to JDBC

    JDBC is a standard interface for connecting to relational databases

    from Java

    The JDBC classes and interfaces are in the java.sql package

    A hit t & Q i ith JDBC

  • 7/31/2019 J2EE_v1 1

    48/88

    Version 1.1 48

    Query

    Close

    Connect

    Process

    results

    Architecture & Querying with JDBC

    Stage 1 Connect

  • 7/31/2019 J2EE_v1 1

    49/88

    Version 1.1 49

    Query

    Close

    Connect

    Process

    results

    Register the driver

    Connect to the database

    Stage 1: Connect

    Connect: A JDBC Driver

  • 7/31/2019 J2EE_v1 1

    50/88

    Version 1.1 50

    Connect: A JDBC Driver

    Is an interpreter that translates JDBC method calls to vendor-

    specific database commands

    Implements interfaces injava.sql

    Can also provide a vendors extensions to the JDBC standard

    DriverJDBC callsJDBC calls

    DatabaseDatabase

    commandscommandsDatabase

    JDBC Driver (Contd )

  • 7/31/2019 J2EE_v1 1

    51/88

    Version 1.1 51

    JDBC-ODBC Bridge Driver

    (Type I Driver)

    JDBC Driver (Contd.).

    JDBC Driver (Contd )

  • 7/31/2019 J2EE_v1 1

    52/88

    Version 1.1 52

    JDBC Driver (Contd.).

    Native JDBC Driver (Type II Driver)

    JDBC Drivers (Contd )

  • 7/31/2019 J2EE_v1 1

    53/88

    Version 1.1 53

    JDBC Drivers (Contd.).

    All Java JDBC Net Drivers (Type III Driver)

  • 7/31/2019 J2EE_v1 1

    54/88

    Connect: About JDBC URL

  • 7/31/2019 J2EE_v1 1

    55/88

    Version 1.1 55

    JDBC uses a URL to identify the database connection.

    jdbc::

    ProtocolProtocolDatabaseDatabase

    identifieridentifier

    jdbc:oracle::@

    SubprotocolSubprotocol

    Connect: About JDBC URL

    JDBC URLs: Examples

  • 7/31/2019 J2EE_v1 1

    56/88

    Version 1.1 56

    JDBC-ODBC driver

    OCI driver

    jdbc:odbc:jdbcoodbcDriverDsn

    jdbc:oracle:oci8:@

    JDBC URLs: Examples

    How to make the Connection?

  • 7/31/2019 J2EE_v1 1

    57/88

    Version 1.1 57

    1. Register the driverClass c = Class.forName("oracle.jdbc.driver.OracleDriver");

    Connection conn =

    DriverManager.getConnection(URL,userid,password);

    Connection conn = DriverManager.getConnection

    ("jdbc:oracle:thin:@myhost:1521:orcl",

    "scott", "tiger");

    2. Connect to the database

    Class c = Class.forName(

    sun.jdbc.odbc.JdbcOdbcDriver");

    How to make the Connection?

    Stage 2: Query

  • 7/31/2019 J2EE_v1 1

    58/88

    Version 1.1 58

    Close

    Connect

    Query Create a statement

    Process

    results

    Query the database

    Stage 2: Query

    Query: The Statement Object

  • 7/31/2019 J2EE_v1 1

    59/88

    Version 1.1 59

    Query: The Statement Object

    A Statement object sends your SQL statement to the database

    You need an active connection to create a JDBC statement

    Statement has three methods to execute a SQL statement:

    executeQuery() for QUERY statements

    executeUpdate()for INSERT, UPDATE, DELETE, or DDL statements

    execute() for either type of statement

    How to Query the Database?

  • 7/31/2019 J2EE_v1 1

    60/88

    Version 1.1 60

    1. Create an empty statement object

    2. Execute the statement

    Statement stmt = conn.createStatement();

    ResultSet rset = stmt.executeQuery(statement);

    int count = stmt.executeUpdate(statement);

    boolean isquery= stmt.execute(statement);

    How to Query the Database?

    Querying the Database: Examples

  • 7/31/2019 J2EE_v1 1

    61/88

    Version 1.1 61

    Execute a select statement

    Statement stmt = conn.createStatement();

    ResultSet rset = stmt.executeQuery

    ("select NAME, VERTICAL from STUDENT");

    Execute a delete statement

    Statement stmt = conn.createStatement();

    int rowcount = stmt.executeUpdate("delete from STUDENT

    where ID = 1000");

    Querying the Database: Examples

    Stage 3: Process the Results

  • 7/31/2019 J2EE_v1 1

    62/88

    Version 1.1 62

    Close

    Query

    Step through the results

    Process

    results

    Assign results to Java

    variables

    Connect

    Stage 3: Process the Results

    Process the Results: The ResultSet Object

  • 7/31/2019 J2EE_v1 1

    63/88

    Version 1.1 63

    Process the Results: The ResultSet Object

    JDBC returns the results of a query in a ResultSet object

    AResultSetmaintains a cursor pointing to its current row of data

    Use next() to step through the result set row by row

    getString(), getInt(), and so on assign each value to a Javavariable

    How to Process the Result?

  • 7/31/2019 J2EE_v1 1

    64/88

    Version 1.1 64

    1. Step through the result set

    2. Use getXXX() to get each column value

    while (rset.next()) { }

    String val =

    rset.getString(colname);

    while (rset.next()) {

    String name = rset.getString(NAME");

    String supervisor = rset.getString(SUPERVISOR"); // Process or display the data

    }

    String val =

    rset.getString(colIndex);

    How to Process the Result?

    How to handle SQL Null values?

  • 7/31/2019 J2EE_v1 1

    65/88

    Version 1.1 65

    How to handle SQL Null values?

    Java primitive types cannot have null values

    Do not use a primitive type when your query might return a SQLnull

    Use ResultSet.wasNull() to determine whether a column has a

    null value

    while (rset.next()) {String year = rset.getString("YEAR");

    if (rset.wasNull() { // Handle null value}}

    Stage 4: Close

  • 7/31/2019 J2EE_v1 1

    66/88

    Version 1.1 66

    Connect

    Query

    Process

    results

    Close

    Close the result set

    Close the statement

    Close the connection

    Stage 4: Close

    How to Close the Connection?

  • 7/31/2019 J2EE_v1 1

    67/88

    Version 1.1 67

    1. Close the ResultSet object

    2. Close the Statement object

    3. Close the connection (not necessary for server-side driver)

    rset.close();

    stmt.close();

    conn.close();

    How to Close the Connection?

    The DatabaseMetaData Object

  • 7/31/2019 J2EE_v1 1

    68/88

    Version 1.1 68

    The DatabaseMetaData Object

    The Connection object can be used to get a DatabaseMetaData

    object

    This object provides more than 100 methods to obtain informationabout the database

    How to obtain Database Metadata?

  • 7/31/2019 J2EE_v1 1

    69/88

    Version 1.1 69

    DatabaseMetaData dbmd = conn.getMetaData();String s1 = dbmd getURL();String s2 = dbmd.getSQLKeywords();boolean b1 = dbmd.supportsTransactions();

    boolean b2 = dbmd.supportsSelectForUpdate();

    1. Get the DatabaseMetaData object

    2. Use the objects methods to get the metadata

    DatabaseMetaData dbmd= conn.getMetaData();

    How to obtain Database Metadata?

    The ResultSetMetaData Object

  • 7/31/2019 J2EE_v1 1

    70/88

    Version 1.1 70

    The ResultSetMetaData Object

    The ResultSet object can be used to get a ResultSetMetaData

    object

    ResultSetMetaData object provides metadata, including:

    Number of columns in the result set

    Column type

    Column name

    How to obtain ResultSetMetadata?

  • 7/31/2019 J2EE_v1 1

    71/88

    Version 1.1 71

    1. Get the ResultSetMetaData object

    2. Use the objects methods to get the metadata

    ResultSetMetaData rsmd = rset.getMetaData();for (int i = 1; i

  • 7/31/2019 J2EE_v1 1

    72/88

    Version 1.1 72

    ResultSetmaps database types to Java types.

    ResultSet rset = stmt.executeQuery("select ID, DATE_OF_JOIN, SUPERVISORfrom STUDENT");

    int id = rset.getInt(1);Date rentaldate = rset.getDate(2);String status = rset.getString(3);

    Col Name

    ID

    DATE_OF_JOIN

    SUPERVISOR

    Type

    NUMBER

    DATE

    VARCHAR2

    app g atabase ypes to Ja a ypes

  • 7/31/2019 J2EE_v1 1

    73/88

    Version 1.1 73

    The PreparedStatement Object

  • 7/31/2019 J2EE_v1 1

    74/88

    Version 1.1 74

    p j

    APreparedStatement object holds precompiled SQL statements

    Use this object for statements you want to execute more than once

    A prepared statement can contain variables that you supply eachtime you execute the statement

    How to Create a PreparedStatement?

  • 7/31/2019 J2EE_v1 1

    75/88

    Version 1.1 75

    1. Register the driver and create the database connection

    2. Create the prepared statement, identifying variables with aquestion mark (?)

    PreparedStatement pstmt =conn.prepareStatement("update STUDENTset SUPERVISOR = ? where ID = ?");

    PreparedStatement pstmt =conn.prepareStatement("select SUPERVISOR fromSTUDENT where ID = ?");

    p

    How to execute PreparedStatement?

  • 7/31/2019 J2EE_v1 1

    76/88

    Version 1.1 76

    1. Supply values for the variables

    2. Execute the statement

    pstmt.setXXX(index, value);

    pstmt.executeQuery();

    pstmt.executeUpdate();

    PreparedStatement pstmt =conn.prepareStatement("update STUDENT

    set SUPERVISOR = ? Where ID = ?");pstmt.setString(1, "OUT");pstmt.setInt(2, id);pstmt.executeUpdate();

    p

    The CallableStatement Object

  • 7/31/2019 J2EE_v1 1

    77/88

    Version 1.1 77

    j

    A CallableStatement object holds parameters for calling stored

    procedures

    A callable statement can contain variables that you supply each timeyou execute the call

    When the stored procedure returns, computed values (if any) areretrieved through the CallableStatement object

  • 7/31/2019 J2EE_v1 1

    78/88

    How to execute a CallableStatement?

  • 7/31/2019 J2EE_v1 1

    79/88

    Version 1.1 79

    1. Set the input parameters

    2. Execute the statement

    3. Get the output parameters

    cstmt.setXXX(index, value);

    cstmt.execute(statement);

    var = cstmt.getXXX(index);

    Using Transactions

  • 7/31/2019 J2EE_v1 1

    80/88

    Version 1.1 80

    g

    The server-side driver does not support autocommit mode

    With other drivers:

    New connections are in autocommit mode

    Use conn.setAutoCommit(false) to turn autocommit off

    To control transactions when you are not in autocommit mode:

    conn.commit(): Commit a transaction

    conn.rollback(): Roll back a transaction

    Summary of JDBC Classes

  • 7/31/2019 J2EE_v1 1

    81/88

    Version 1.1 81

    Connection

    DriverManager

    DatabaseMetaData

    Statement

    ResultSetMetaDataResultSet

    Summary

  • 7/31/2019 J2EE_v1 1

    82/88

    Version 1.1 82

    In this module you have learnt to:

    Connect to a database using Java Database Connectivity (JDBC) Create and execute a query using JDBC

    Invoke prepared statements

    Commit and roll back transactions

    Use the Metadata objects to retrieve more information about the database

    or the resultset

  • 7/31/2019 J2EE_v1 1

    83/88

    Version 1.1 83

  • 7/31/2019 J2EE_v1 1

    84/88

    Version 1.1 84

  • 7/31/2019 J2EE_v1 1

    85/88

    Version 1.1 85

  • 7/31/2019 J2EE_v1 1

    86/88

    Version 1.1 86

  • 7/31/2019 J2EE_v1 1

    87/88

    Version 1.1 87

  • 7/31/2019 J2EE_v1 1

    88/88