2 jdbc notes_tri

Upload: kumar

Post on 17-Feb-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/23/2019 2 JDBC Notes_tri

    1/94

    JDBC is an API released by sun micro system.

    JDBC means for java data base connectivity.

    We can store the data into multiple places they are

    1. iles!. Database server

    "hey are so many disadvanta#es to store the data into $le

    system. "hey are

    1. ile system does not provide security.!. "here is limit on the si%e of the $les.&. If 'e store the data into $le the redundant data 'ill be store.(. We can resolve this entire problem by usin# data base

    servers only.). JDBC API is used if a java application 'ant interact 'ith

    communication 'ith database server.*. "here are so many database servers are available in the

    mar+et. ,ome of them all -racle /0,2 ,AP IB/DB!

    P-I3" database server and etc.4. When ever 'e purchase any database server 'e #et t'o

    di5erent soft'are6s they areDatabase ,erver Pro#ram7,8W9Database Client Pro#ram7,8W9

    "he database server s8' 'ill be installed in server computer.

    It is the responsibility of database or net'or+ administrator toinstall server soft'are in server computer.

    :very server computer is connected to a net'or+. We can ;uic+ly

    identify a server computer by usin# the IP Address of the

    computer. If 'e 'ant to develop any pro#ram to communicate

    'ith they are server 'ith database three information6s. "hey are

  • 7/23/2019 2 JDBC Notes_tri

    2/94

    1. IP Address!. ,ervice 3ame&. Port 3umber

    Database service name

    Default Port number is 1)!1

    ,erver Computer

    "he database client soft'are installed 'ith all the developer

    computers.

    Computer 1

    Computer !

    IP< 1=*.&>.&(.1>

    ,:?@IC: 3A/:>>.

    import java.s;l.F

    public class Insert"able

    public static void main7,trin# ar#sMN9thro's ,2:Gception

    Driver/ana#er.re#isterDriver7ne'oracle.jdbc.driver.-racleDriver799F

    Connection con O

    Driver/ana#er.#etConnection7Ujdbc

  • 7/23/2019 2 JDBC Notes_tri

    17/94

    ,tatement stmt O con.create,tatement79F

    stmt.eGecutepdate7Uinsert into emp values71>*V?aviV1K>>>9U9F

    E

    E

    :Gecute update method returns an inte#er value. "his inte#er

    value indicates the number of records a5ected by the ;uery in the

    database server. "he memory of e5ected is because of ,2 ;uery

    ho' many number of records are update delete and inserted the

    data.Ex< int no O stmt.eGecutepdate7 delete from emp 'here eno

    O 1>1Q9F

    ,ystem.out.println7no9F

    Requirement>>

    ! naveen

    !>>>

    & sumanth

    1 ?aju 1>>>

    ! naveen

    !>>>

    & sumanth

    1

    !

    &

    1 ?aju 1>>>

    ! naveen

    !>>>

    & sumanth

    *>>>

    ( Praveen

  • 7/23/2019 2 JDBC Notes_tri

    19/94

    et ro' method is used to $nd 'here the ?esult,et pointer is

    pontin# to.

    Syn>>9F

    end /yProcF

    8

    "o eGecute the Procedure from the client 'e use a command

    eGec /yProcL "he follo'in# procedure 'hich ta+es input

    parameter.

    1 create or replace procedure /yProc7veno I3 number

  • 7/23/2019 2 JDBC Notes_tri

    38/94

    ! vname I3 varchar! vsalary I3 number9

    & as

    ( be#in

    ) insert into emp values7veno vname vsalary9F

    * end /yProcF

    4 8

    "o run the above procedure 'e use the follo'in# command eGec

    /yProc716abc6!&()9F

    create or replace procedure addition7no1 in number

    no! in number result out number9

    as

    be#in

    result !>

  • 7/23/2019 2 JDBC Notes_tri

    39/94

    create or replace procedure addition7no1 in number

    no! in number result out number9

    as

    be#in

    result

  • 7/23/2019 2 JDBC Notes_tri

    40/94

    7jdbc

  • 7/23/2019 2 JDBC Notes_tri

    41/94

    Driver/ana#er.re#isterDriver7ne'

    oracle.jdbc.driver.-racleDriver799F

    Connection con O Driver/ana#er.#etConnection

    7jdbc

  • 7/23/2019 2 JDBC Notes_tri

    42/94

    import java.s;l.F

    public class Callable,tatement

    public static void main7,trin#MN ar#s9thro's ,2:Gception

    Driver/ana#er.re#isterDriver7ne'

    oracle

  • 7/23/2019 2 JDBC Notes_tri

    43/94

    !. ser de$ned functions

    In oracle 'e have the follo'in# a##re#ate functions they are

    1. count79!. min79&. maG79(. sum79). av#79 and etc.

    "o call function in oracle 'e use ueries for eGample

    ,elect sum7sal9 from empF,elect maG7sal9 from empF

    ,elect count79 from empF

    Develop a java application to $nd the number of records

    available a emp tableL

    import java.s;l.F

    public class ind?ecords

    public static void main7,trin#MN main9thro's ,2:Gception

    Driver/ana#er.re#isterDriver7ne'

    oracle.jdbc.driver.-ralceDriver799F

    Connection con O Driver/ana#er.#etConnection

    7jdbc

  • 7/23/2019 2 JDBC Notes_tri

    44/94

    ?esutl,et rs O stmt.eGecuteuery7select count79 count from

    empQ9F

    if7rs.neGt799

    ,ystem.out.println7rs.#etInt7countQ99F

    E

    E

    ,elect from user[sourceF

    ,ho' errorsF

    create or replace function addition7@no1 in number @no! in

    number9

    return number

    as

    result numberF

    be#in

    result !>9 from dualF

  • 7/23/2019 2 JDBC Notes_tri

    45/94

    Appr"ac#'< We can call a function by usin# callable statementF

    S%&X select addition71>!>9 sum from dualF

    Ex>9F

    pstmt.addBatch79F

    pstmt.setInt711((9F

    pstmt.set,trin#7!UsudheerU9F

    pstmt.setDouble7&=K4*9Fpstmt.addBatch79F

    pstmt.eGecuteBatch79F

    E

    E

    import java.s;l.F

    public class PreparedBsnlBill

    public static void main7,trin#MN ar#s9thro's ,2:Gception

    Driver/ana#er.re#isterDriver7ne'

    oracle.jdbc.driver.-racleDriver799F

    Connection con O Driver/ana#er.#etConnection

    7Ujdbc

  • 7/23/2019 2 JDBC Notes_tri

    58/94

    ?esult,et rs O stmt.eGecuteuery7Uselect from

    B,32C,"-/:?U9F

    Prepared,tatement pstmt O con.prepare,tatement

    7Uupdate bsnlcustomer set bamountO L 'here phoneno O LU9F

    'hile7rs.neGt799

    int noofc O rs.#etInt7UnoofcU9F

    int phoneno O rs.#etInt7UphonenoU9F

    double bamount O noofc >.1>F

    pstmt.setDouble71bamount9F

    pstmt.setInt7!phoneno9F

    pstmt.addBatch79F

    E

    pstmt.eGecuteBatch79F

    E

    E

    Without JDBC driver 'e cannot develop java application driver

    hence interact 'ith JDBC driver server sun micro6s there are

    summary.

    /ajorly there are three 'ay6s are available to re#ister the driverthey are

    1. Driver/ana#er.re#isterDriver!. By usin# class.for3ame

    Ex

  • 7/23/2019 2 JDBC Notes_tri

    59/94

    public class ?etrive?ecords

    public static void main7,trin#MN ar#s9

    class.for3ame7oracle.jdbc.driver.-racleDriverQ9F

    Connection con O Driver/ana#er.#etConnection

    7jdbc and JDBC (.>

  • 7/23/2019 2 JDBC Notes_tri

    60/94

    When ever a speci$cation is released it is the responsibility of

    JDBC Driver vendors to provide the implementation #enerally

    bet'een the version the chan#ed the names of the java $les.Ex speci$cation oracle 'ays are choose

    ojdbc1(.jar in case of JDBC (.> implementation they choose

    ojdbc*.jar.

    When the ne' versions of speci$cations are released then all ne'interfaces and ne' methods in the eGistin# interfaces.

    As part of JDBC(.> speci$cation sun micro system has added a

    feature to re#ister the driver automatically

    7Driver/ana#er.#etConnection9.

    We can $nd the speci$cation implementation version as part of

    /:"A[I3 folders.

    When 'e 'or+ 'ith JDBC(.> to re#ister JDBC driver automatically'e need to ma+e sure that 'e set the C2A,,PA" to ojdbc*.jar.

    "heoretically spea+s a driver can communicate 'ith any DB

    server 'hen 'e consider oracle JDBC Driver. "hey have provided

    the code to communicate only 'ith oracle DB server.

    We cannot use oracle JDBC Driver to communicate 'ith /ys;l

    Database ,erver.

    ,3 JDBC API

  • 7/23/2019 2 JDBC Notes_tri

    61/94

    create databasenameF

    sho' databasesF

    use databasenameF

    uses the enterprise editionF

    ,:?3A/:< root

    PA,,W-?D< i netsolv

    /ys;lX sho' databasesF

    /ys;lX create database mydatabaseF

    /ys;lX sho' databasesF

    /ys;lX use mydatabaseF

    /ys;lX create database7eno int7)9 ename varchar7!>9 salary

    double9F

    procedure to 'or+ 'ith mys;l database server.

    i net DBC/0, 2 DBC-racle DBC

    DB/ DB!,0BA,:-?AC2 /0, 2

  • 7/23/2019 2 JDBC Notes_tri

    62/94

    ,tep 1< 2o#in to mys;l DB, by usin# mys;l command line client.

    ,tep !< "o 'or+ 'ith mys;l DB, 'e have to create DB,6s. "he

    follo'in# is a command 'hich is used to create database.

    Ex