MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

Embed Size (px)

Citation preview

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    1/20

    NetBeans IDE NetBeans Platform Enterprise Plugins Docs & Support Community Search

    HOME/ Docs & Support

    Creating a Simple We !pplication "sing a MyS#$ Dataase

    Written by Troy Giunipero

    %is 'ocument 'escries o( to create a simple (e application tat connects to a MyS#$ 'ataase ser)er* It also co)ers some asic i'eas an'

    tecnologies in (e 'e)elopment+ suc as ,a)aSer)er Pages-,SP.+ ,a)aSer)er Pages Stan'ar' %ag $irary-,S%$.+ te ,a)a Dataase

    Connecti)ity-,DBC. !PI+ an' t(otier+ clientser)er arcitecture* %is tutorial is 'esigne' for eginners (o a)e a asic un'erstan'ing of (e

    'e)elopment an' are loo0ing to apply teir 0no(le'ge using a MyS#$ 'ataase*

    MyS#$is a popular open source 'ataase management system commonly use' in (e applications 'ue to its spee'+ fle1iility an' reliaility*

    MyS#$ employs S#$+ or Structured Query Language+ for accessing an' processing 'ata containe' in 'ataases*

    %is tutorial is a continuation from te Connecting to a MyS#$ Dataasetutorial an' assumes tat you a)e alrea'y create' a MyS#$ 'ataase

    name' MyNewDatabase+ (ic you a)e registere' a connection for in te NetBeans IDE* %e tale 'ata use' in tat tutorial is containe' in

    ifp(afca'*s2lan' is also re2uire' for tis tutorial* % is S#$ file creates t(o tales+ Subjectan' Counselor+ ten populates tem (it sample

    'ata* If nee'e'+ sa)e tis file to your computer+ ten open it in te NetBeans IDE an' run it on te MyS#$ 'ataase name'MyNewDatabase*

    Contents

    Planning te Structure

    Creating a Ne( Pro3ect

    4 Preparing te We Interface

    4 Preparing Communication et(een te !pplication an' Dataase

    4 !''ing Dynamic $ogic

    5unning te Complete' !pplication

    4 %roulesooting

    See !lso

    To follow this tutorial, you need the following software and resources.

    Software or Resource Version Required

    NetBeans IDE 6*7+ 6*8+ 6*9+ :*;+ ,a)a EE un'le

    ,a)a De)elopment lass?is ser)er.+ an' any content in

    S#$+ te language un'erstoo' y te 'ataase ser)er -MyS#$.*

    %e application you uil' in tis tutorial in)ol)es te creation of t(o ,SP pages* In eac of tese pages you use H%M$ an' CSS to implement a

    simple interface+ an' apply ,S%$ tecnology to perform te logic tat 'irectly 2ueries te 'ataase an' inserts te retrie)e' 'ata into te t(o

    pages* %e t(o 'ataase tales+ Subjectan' Counselor+ are containe' in te MyS#$ 'ataase+ MyNewDatabase+ (ic you create y

    completing te Connecting to a MyS#$ Dataasetutorial* Consi'er te follo(ing t(otier scenario*

    %raining

    ,a)a Programming

    $anguage

    Support

    Oracle De)elopment %ools

    Support Offering for

    NetBeans IDE

    Documentation

    >eneral ,a)a De)elopment

    E1ternal %ools an' Ser)ices

    ,a)a >"I !pplications

    ,a)a EE & ,a)a We

    De)elopment

    We Ser)ices !pplications

    NetBeans Platform -5CP.

    an' Mo'ule De)elopment

    PHP an' H%M$=

    !pplications

    C/CAA !pplications

    Moile !pplications

    Sample !pplications

    Demos an' Screencasts

    More

    ?!#s

    Contriute Documentation

    Docs for Earlier 5eleases

    Coose page language

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    ( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    2/20

    %e (elcome page -index.jsp. presents te user (it a simple H%M$ form* Wen a ro(ser re2uests index.jsp+ te ,S%$ co'e (itin te

    page initiates a 2uery on MyNewDatabase* It retrie)es 'ata from te Subject'ataase tale+ an' inserts it into to te page efore it is sent to

    te ro(ser* Wen te user sumits is or er selection in te (elcome pages H%M$ form+ te sumit initiates a re2uest for te response page

    -response.jsp.* !gain+ te ,S%$ co'e (itin te page i nitiates a 2uery on MyNewDatabase* %is t ime+ it retrie)es 'ata from ot te

    Subjectan' Counselortales an' inserts it into to te page+ allo(ing te user to )ie( 'ata ase' upon is or er selection (en te page is

    returne' to te ro(ser*

    In or'er to implement te scenario 'escrie' ao)e+ you 'e)elop a simple application for a ficti tious organiation name' I?PW!?C!D+ %e

    International ?ormer Professional Wrestlers !ssociation for Counseling an' De)elopment*

    index.jsp

    response.jsp

    Creating a Ne( Pro3ect

    Begin y creating a ne( ,a)a (e pro3ect in te IDE

    Coose ?ile F Ne( Pro3ect -CtrlSiftNG SiftN on Mac. from te main menu* Select te ,a)a We category+ ten select We !pplication*

    Clic0 Ne1t*

    *

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    ( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    3/20

    %e Ne( Pro3ect (iar' allo(s you to create an empty (e application in a stan'ar' IDE pro3ect* %e stan'ar' pro3ect uses an

    IDEgenerate' !nt uil' script to compile+ 'eploy+ an' run te application*

    In Pro3ect Name+ enter !"#$!C$%* !lso+ specify te location for te pro3ect on your computer* -By 'efault+ te IDE places pro3ects in a

    NetBeansProjectsfol'er locate' in your ome 'irectory*. Clic0 Ne1t*

    7*

    In te Ser)er an' Settings panel+ specify te >lass?is ser)er as ser)er (ic (ill e use' to run te application*

    Note. %e >lass?is ser)er 'isplays in te Ser)er 'rop'o(n fiel' if you installe' te ,a)a )ersion of te NetBeans IDE* Because te

    >lass?is ser)er is inclu'e' in te 'o(nloa'+ it is automatically registere' (it te IDE* If you (ant to use a 'ifferent ser)er for tis

    pro3ect+ clic0 te !'' utton locate' ne1t to te Ser)er 'rop'o(n fiel'+ an' register a 'ifferent ser)er (it te IDE* Ho(e)er+ (or0ing

    (it ser)ers oter tan te >lass?is ser)er is eyon' te scope of tis tutorial*

    8*

    In te ,a)a EE ersion fiel'+ select &a'a (( )*

    ,a)a EE J an' ,a)a EE 6 (e pro3ects 'o not re2uire te use of te web.xml'eployment 'escriptor+ an' te NetBeans pro3ect template

    'oes not inclu'e te web.xmlfile in ,a)a EE J an' ,a)a EE 6 pro3ects* Ho(e)er+ tis tutorial 'emonstrates o( to 'eclare a 'ata source

    in te 'eployment 'escriptor+ an' it 'oes not rely on any features specific to ,a)a EE J or ,a)a EE 6+ so you can set te pro3ect )ersion to

    ,a)a EE =*

    Note.@ou coul' e2ually set te pro3ect )ersion to ,a)a EE J or ,a)a EE 6 an' ten create a web.xml'eployment 'escriptor* -?rom

    te Ne( ?ile (iar'+ select te We category+ ten Stan'ar' Deployment Descriptor*.

    9*

    Clic0 ?inis* %e IDE creates a pro3ect template for te entire application+ an' opens an empty ,SP page -index.jsp. in te e'itor* %e

    index.jspfile ser)es as te (elcome page for te application*

    =*

    Preparing te We Interface

    Begin y preparing te (elcome -index.jsp. an' response -response.jsp. pages* %e (elcome page implements an H%M$ form tat is

    use' to capture user 'ata* Bot pages implement an H%M$ tale to 'isplay 'ata in a structure' fasion* In tis section+ you also create a styleseet

    tat enances te appearance of ot pages*

    Setting up te (elcome page

    Creating te response page

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    ( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    4/20

    Creating a styleseet

    Setting up te (elcome page

    Confirm tat index.jspis open in te e'itor* If te file is not alrea'y open+ 'ouleclic0 index.jspun'er te We Pages no'e in te

    I?PW!?C!D pro3ect in te Pro3ects (in'o(*

    In te e'itor+ cange te te1t et(een te tags to IFP!FC!D #omepa$e**

    Cange te te1t et(een te tags to elcome to IFP!FC!D' t%e International Former Pro(essional

    restlers) !ssociation (or Counselin$ and De*elopment+*

    7*

    Open te IDEs Palette y coosing Win'o( F Palette -CtrlSift:G Sift: on Mac. from te main menu* Ho)er your pointer o)er te

    %ale icon from te H%M$ category an' note tat te 'efault co'e snippet for te item 'isplays*

    @ou can configure te Palette to your li0ing rigtclic0 in te Palette an' coose So( Big Icons an' Hi'e Item Names to a)e it

    'isplay as in te image ao)e*

    8*

    Place your cursor at a point 3ust after te tags* -%is is (ere you (ant to implement te ne( H%M$ tale*. % en+ in te Palette+

    'ouleclic0 te %ale icon*

    9*

    In te Insert %ale 'ialog tat 'isplays+ specify te follo(ing )alues ten clic0 O

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    5/20

    %ype in te follo(ing content et(een te tags -ne( content so(n in -old.

    $strong%&elect a su!"ect#$'strong%

    :*

    Press Enter to a'' an empty line after te content you 3ust a''e' an' ten 'ouleclic0 Drop'o(n $ist in te Palette to open te Insert

    Drop'o(n 'ialog o1*

    K*

    %ype subject0idfor te Name te1t fiel' in te Insert Drop'o(n 'ialog an' clic0 O

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    6/20

    %o )ie( tis page in a ro(ser+ rigtclic0 in te e'itor an' coose 5un ?ile -Sift?JG ?nSift?J on Mac.* Wen you 'o tis+ te ,SP

    page is automatically compile' an' 'eploye' to your ser)er* %e IDE opens your 'efault ro(ser to 'isplay te page from its 'eploye'

    location*

    Creating te response page

    In or'er to prepare te interface for response.jspyou must first create te file in your pro3ect* Note tat most of te content tat 'isplays in

    tis page is generate' 'ynamically using ,SP tecnology* %erefore+ in te follo(ing steps you a''placeholders(ic you (ill later sustitute

    for te ,SP co'e*

    5igtclic0 te I?PW!?C!D pro3ect no'e in te Pro3ects (in'o( an' coose Ne( F ,SP* %e Ne( ,SP ?ile 'ialog opens**

    In te ,SP ?ile Name fiel'+ enter response* Note tat We Pages is currently selecte' for te $ocation fiel'+ meaning tat te file (ill

    e create' in te pro3ects web'irectory* %is is te same location as (ere te index.jsp(elcome page resi'es*

    7*

    !ccept any oter 'efault settings an' clic0 ?inis* ! template for te ne(response.jsppage is generate' an' opens in te e'itor* !

    ne( ,SP no'e also 'isplays un'er We Pages in te Pro3ects (in'o(*

    8*

    In te e'itor+ cange te title to IFP!FC!D 4 5place%older6*9*

    5emo)e te #ello orld+line et(een te tags+ ten copy an' paste te follo(ing H%M$ tale into te

    o'y of te page

    5place%older6

    Description3

    5place%older6

    Counselor3

    5place%older6


    member since3 5place%older6

    Contact Details3

    =*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    ( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    7/20

    email3

    5place%older6


    p%one3 5place%older6

    %o )ie( tis page in a ro(ser+ rigtclic0 in te e'itor an' coose 5un ?ile -Sift?JG ?nSift?J on Mac.* %e page compiles+ is

    'eploye' to te >lass?is ser)er+ an' opens in your 'efault ro(ser*

    Creating a styleseet

    Create a simple styleseet tat enances te 'isplay of te (e interface* %is tutorial assumes tat you un'erstan' o( style rules function+

    an' o( tey affect correspon'ing H%M$ elements foun' in index.jspan' response.jsp*

    Open te Ne( ?ile (iar' y pressing te Ne( ?ile - . utton in te IDEs main toolar* Select te We category+ ten select

    Casca'ing Style Seet an' clic0 Ne1t*

    *

    %ype stylefor CSS ?ile Name an' clic0 ?inis* % e IDE creates an empty CSS file an' places it in te same pro3ect location as

    index.jspan' response.jsp* Note tat a no'e for style.cssno( 'isplays (itin te pro3ect in te Pro3ects (in'o(+ an' te

    file opens in te e'itor*

    7*

    In te e'itor+ a'' te follo(ing content to te style.cssfile

    body 5

    (ont4(amily3 9erdana' !rial' sans4seri(8 (ont4si7e3 smaller8

    paddin$3 :px8

    color3 ;:::8

    6

    %& 5

    text4ali$n3 le(t8

    letter4spacin$3 px8

    (ont4si7e3 &.=em8

    color3 ;be=1?8

    (ont4wei$%t3 normal8

    widt%3 =:px8

    6

    table 5

    widt%3 :@px8

    paddin$3 &px8

    bacA$round4color3 ;c:ee8

    6

    t% 5

    text4ali$n3 le(t8

    border4bottom3 &px solid8

    6

    td 5

    paddin$3 &px8

    6

    8*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    ( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    8/20

    Important: From this point forward, you need you ensure that you have a ySQL database instance named MyNewDatabase

    set up that contains sample data provided in ifpwafcad!s"l! This SQL file creates two tables, Subjectand Counselor, then

    populates them with sample data! #f you have not already done this, or if you need help with this tas$, see %onnecting to a ySQL

    &atabasebefore proceeding further!

    'lso, your database needs to be password-protected to create a data source and wor$ with the GlassFish server in thi s tutorial! # f you

    are using the default ySQL rootaccount with an empty password, you can set the password from a command-line prompt!

    This tutorial uses nbuseras an e(ample password! To set your password to nbuser, navigate to your ySQL installation)s bin

    directory in the command-line prompt and enter the following*

    shell> mysql -u root

    mysql> UPDATE mysqluser SET Passwor! " PASS#$%D&'nbuser'(

    -> #)E%E User " 'root'*

    mysql> +,US) P%.,E/ES*

    For more information, see the official ySQL +eference anual* Securing the #nitial ySQL 'ccounts!

    a3linA 5

    color3 ;be=1?8

    (ont4wei$%t3 normal8

    text4decoration3 none8

    6

    a3linA3%o*er 5

    color3 ;be=1?8

    (ont4wei$%t3 normal8

    text4decoration3 underline8

    6

    $in0 te styleseet to index.jspan' response.jsp* In ot pages+ a'' te follo(ing line et(een te tags

    %o 2uic0ly na)igate et(een files tat are open in te e'itor+ press Ctrl%a+ ten select te file you are (anting*

    9*

    Preparing Communication et(een te !pplication an' Dataase

    %e most efficient (ay to implement communication et(een te ser)er an' 'ataase is to set up a 'ataase connection pool* Creating a ne(

    connection for eac client re2uest can e )ery timeconsuming+ especially for applications tat continuously recei)e a large numer of re2uests* %o

    reme'y tis+ numerous connections are create' an' maintaine' in a connection pool* !ny incoming re2uests tat re2uire access to te applications

    'ata layer use an alrea'ycreate' connection from te pool* $i0e(ise+ (en a re2uest is complete'+ te connection is not close' 'o(n+ ut returne'

    to te pool*

    !fter preparing te 'ata source an' connection pool for te ser)er+ you ten nee' to instruct te application to use te 'ata source* %is is typically

    'one y creating an entry in te applications web.xml'eployment 'escriptor* ?inally+ you nee' to ensure tat te 'ataase 'ri)er -MyS#$

    Connector/, ,DBC Dri)er. is accessile to te ser)er*

    Setting up a ,DBC 'ata source an' connection pool*

    5eferencing te 'ata source from te application7*

    !''ing te 'ataase 'ri)ers ,!5 file to te ser)er8*

    Setting up a ,DBC 'ata source an' connection pool

    %e >lass?is Ser)er Open Source E'ition contains Dataase Connection Pooling -DBCP. liraries tat pro)i'e connection pooling

    functionality in a (ay tat is transparent to you as a 'e)eloper* %o ta0e a')antage of tis+ you nee' to configure a ,DBC-,a)a DataaseConnecti)ity. data sourcefor te ser)er (ic your application can use for connection pooling*

    ?or more information on ,DBC tecnology+ see %e ,a)a %utorials ,DBC Basics*

    @ou coul' configure te 'ata source 'irectly (itin te >lass?is ser)er !'min Console+ or+ as 'escrie' elo(+ you can 'eclare te resources

    tat your application nee's in a $lass(is%4resources.xmlfile* Wen te application is 'eploye'+ te ser)er rea's in te resource

    'eclarations+ an' creates te necessary resources*

    %e follo(ing steps 'emonstrate o( to 'eclare a connection pool+ an' a 'ata source tat relies on te connection pool* % e NetBeans ,DBC

    5esource (iar' allo(s you to perform ot actions*

    Open te Ne( ?ile (iar' y pressing te Ne( ?ile - . utton in te IDEs main toolar* Select te >lass?is ser)er category+ ten

    select ,DBC 5esource an' clic0 Ne1t*

    *

    In step 7+ >eneral !ttriutes+ coose te Create Ne( ,DBC Connection Pool option+ ten in te ,NDI Name te1t fiel'+ type in7*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    ( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    9/20

    jd-c!"#$!C$%*

    %e ,DBC 'ata source relies on ,NDI+ te ,a)a Naming an' Directory Interface* %e ,NDI !PI pro)i'es a uniform (ay for

    applications to fin' an' access 'ata sources* ?or more information+ see %e ,NDI %utorial*

    Optionally+ a'' a 'escription for te 'ata source* ?or e1ample+ type in !ccesses t%e database t%at pro*ides data

    (or t%e IFP!FC!D application*

    8*

    Clic0 Ne1t+ ten clic0 Ne1t again to s0ip step 8+ !''itional Properties*9*

    In Step 9+ type in fpwafcad"oolfor ,DBC Connection Pool Name* Ma0e sure te E1tract from E1isting Connection option is selecte'+

    an' coose jdbc3mysl3//local%ost3/MyNewDatabasefrom te 'rop'o(n list* Clic0 Ne1t*

    Note: %e (iar' 'etects any 'ataase connections tat a)e een set up in te IDE* %erefore+ you nee' to a)e alrea'y

    create' a connection to te MyNewDatabase'ataase at tis point* @ou can )erify (at connections a)e een create' y opening

    te Ser)ices (in'o( -Ctrl=G = on Mac. an' loo0ing for connection no'es - . un'er te Dataases category*

    =*

    In Step =+ select ja*ax.sl.ConnectionPoolDataSourcein te 5esource %ype 'rop'o(n list*

    Note tat te IDE e1tracts information from te 'ataase connection you specifie' in te pre)ious step+ an' sets name)alue properties

    for te ne( connection pool*

    J*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    ( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    10/20

    Clic0 ?inis* %e (iar' generates a $lass(is%4resources.xmlfile tat contains entries for te 'ata source an' connection pool

    you specifie'*

    6*

    In te Pro3ects (in'o(+ you can open te $lass(is%4resources.xmlfile tat (as create' un'er te Ser)er 5esources no'e an' note

    tat+ (itin te tags+ a 'ata source an' connection pool a)e een 'eclare' containing te )alues you pre)iously specifie'*

    %o confirm tat a ne( 'ata source an' connection pool are in'ee' registere' (it te >lass?is ser)er+ you can 'eploy te pro3ect to te

    ser)er+ ten locate te resources i n te IDEs Ser)ices (in'o(

    In te Pro3ects (in'o(+ rigtclic0 te I?PW!?C!D pro3ect no'e an' coose Deploy* %e ser)er starts up if not alrea'y running+ an' te

    pro3ect is compile' an' 'eploye' to it*

    *

    Open te Ser)ices (in'o( -Ctrl=G = on Mac. an' e1pan' te Ser)ers F >lass?is F 5esources F ,DBC F ,DBC 5esources an'

    Connection Pools no'es* Note tat te ne( 'ata source an' connection pool are no( 'isplaye'

    7*

    5eferencing te 'ata source from te application

    @ou nee' to reference te ,DBC resource you 3ust configure' from te (e application* %o 'o so+ you can create an entry in te applications

    web.xml'eployment 'escriptor*

    Deployment 'escriptors are LM$ase' te1t files tat contain information 'escriing o( an application is to e 'eploye' to a specific

    en)ironment* ?or e1ample+ tey are normally use' to specify application conte1t parameters an' ea)ioral patterns+ security settings+ as (ell

    as mappings for ser)lets+ filters an' listeners*

    Note.If you specifie' ,a)a EE J or ,a)a EE 6 as te ,a)a )ersion (en you create' te pro3ect+ you nee' to create te 'eployment

    'escriptor file y coosing We F Stan'ar' Deployment Descriptor in te Ne( ?ile (iar'*

    Perform te follo(ing steps to reference te 'ata source in te applications 'eployment 'escriptor*

    In te Pro3ects (in'o(+ e1pan' te Configuration ?iles fol'er an' 'ouleclic0 web.xmlto open te file in te e'itor**

    Clic0 te 5eferences ta locate' along te top of te e'itor*7*

    E1pan' te 5esource 5eferences ea'ing an' clic0 !'' to open te !'' 5esource 5eference 'ialog*8*

    ?or 5esource Name+ enter te resource name tat you ga)e (en configuring te 'ata source for te ser)er ao)e9*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    '( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    11/20

    -jdbc/IFP!FC!D.*

    %ype "avax.s(l.ConnectionPoolData&ourcein te 5esource %ype fiel'* Clic0 Olass?is ser)er in te left pane**

    In te main pane+ select te Enale ,DBC Dri)er Deployment option*7*

    Before you close te Ser)ers manager+ ma0e a note of te pat in'icate' in te Domains fol'er te1t fiel'* Wen you connect to te

    >lass?is ser)er in te IDE+ you are actually connecting to an instanceof te application ser)er* Eac instance runs applications in a

    8*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    '( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    12/20

    uni2ue 'omain+ an' te Domain Name fiel' in'icates te name of te 'omain your ser)er is using* !s so(n in te image ao)e+ te

    'ri)er ,!5 file soul' e locate' (itin domain&+ (ic is te 'efault 'omain create' upon installing te >lass?is ser)er*

    Clic0 Close to e1it te Ser)ers manager*9*

    On your computer+ na)igate to te >lass?is ser)er installation 'irectory an' 'rill into te domainsF domain&F libsufol'er*

    Because you soul' a)e alrea'y 'eploye' te I?PW!?C!D pro3ect to te ser)er+ you soul' see te mysl4connector4

    ja*a4:.&.4bin.jarfile* If you 'o not see te 'ri)er ,!5 file+ perform te follo(ing step*

    =*

    Deploy your pro3ect to te ser)er* In te IDEs Pro3ects (in'o(+ coose Deploy from te rigtclic0 menu of te pro3ect no'e* @ou can

    )ie( progress in te IDEs Output (in'o( -Ctrl9G 9 on Mac.* %e output in'icates tat te MyS#$ 'ri)er is 'eploye' to a location in

    te >lass?is ser)er*

    No(+ if you return to te domain&/libsufol'er on your computer+ you can see tat te mysl4connector4ja*a4:.&.4

    bin.jarfile as een automatically a''e'*

    J*

    !''ing Dynamic $ogic

    5eturning to te index.jspan' response.jsp placeol'ers tat you create' earlier in te tutorial+ you can no( implement te ,S%$ co'e

    tat enales pages to generate content dynamically+ i*e*+ ase' on user input* %o 'o so+ perform te follo(ing tree tas0s*

    !'' te ,S%$ lirary to te pro3ects classpat*

    Implement ,S%$ co'e7*

    !''ing te ,S%$ lirary to te pro3ects classpat

    @ou can apply te ,a)aSer)er Pages Stan'ar' %ag $irary-,S%$. to access an' 'isplay 'ata ta0en from te 'ataase* %e >lass?is ser)er

    inclu'es te ,S%$ lirary y 'efault* @ou can )erify tis y e1pan'ing te >lass?is Ser)er no'e un'er te $iraries no'e in te Pro3ects

    (in'o(+ an' searcing for te ja*ax.ser*let.jsp.jstl.jarlirary* -Ol'er )ersions of te >lass?is ser)er use te

    jstl4impl.jarlirary*. Because te >lass?is ser)er liraries are y 'efault a''e' to your pro3ects classpat+ you 'o not a)e to perform

    any steps for tis tas0*

    ,S%$ pro)i'es te follo(ing four asic areas of functionality*

    core common+ structural tas0s suc as iterators an' con'itionals for an'ling flo( control

    (mt internationaliation an' localiation message formatting

    sl simple 'ataase access

    xml an'ling of LM$ content

    %is tutorial focuses on usage of te corean' sltag liraries*

    Implementing ,S%$ co'e

    No( you can implement te co'e tat 'ynamically retrie)es an' 'isplays 'ata for eac page* Bot pages re2uire tat you implement an S#$

    2uery tat utilies te 'ata source create' earlier in te tutorial*

    %e IDE pro)i'es se)eral 'ataasespecific ,S%$ snippets (ic you can select from te Palette -CtrlSift:G Sift: on Mac.*

    index.jsp

    In or'er to 'ynamically 'isplay te contents of te form in index.jsp+ you nee' to access all names from te Subject'ataase tale*

    Ho)er your mouse o)er te DB 5eport item in te Palette**

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    '( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    13/20

    %e DB 5eport item uses te tag to create an S#$ 2uery+ ten it uses te tag to loop troug te

    2uerys resultsetan' output te retrie)e' 'ata*

    Place your cursor ao)e te 'eclaration -line 6.+ ten 'ouleclic0 te DB 5eport item in te Palette* In te 'ialog

    tat 'isplays+ enter te follo(ing 'etails

    Varia-le Na*e:subjects

    Scope:pa$e

    %ata Source:jdbc/IFP!FC!D

    /uery State*ent:SGC2 subject0id' name FHM Subject

    7*

    Clic0 O

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    14/20

    $tr%

    $c#for-ach var+,column6ame, items+,78su!"ects.column6ames9,%

    $th%$c#out value+,78column6ame9,'%$'th%

    $'c#for-ach%

    $'tr%

    $455 column data 55%

    $c#for-ach var+,row, items+,78su!"ects.rows:yIndex9,%

    $tr%

    $c#for-ach var+,column, items+,78row9,%

    $td%$c#out value+,78column9,'%$'td%

    $'c#for-ach%

    $'tr%$'c#for-ach%

    $'ta!le%

    Note tat te IDE automatically a''e' ta$lib'irecti)es nee'e' for te ,S%$ tags use' in te generate' content -

    an' .* ! ta$lib'irecti)e 'eclares tat te ,SP page uses custom -i*e*+ ,S%$. tags+ names te tag lirary tat 'efines

    tem+ an' specifies teir tag prefi1*

    5un te pro3ect to see o( it 'isplays in a ro(ser* 5igtclic0 te pro3ect no'e in te Pro3ects (in'o( an' coose 5un*

    Wen you coose 5un+ te IDE 'eploys te pro3ect to te >lass?is ser)er+ te in'e1 page is compile' into a ser)let+ an' te (elcome

    page opens in your 'efault ro(ser* %e co'e generate' from te DB 5eport item creates te follo(ing tale in te (elcome page*

    !s you can see+ te DB 5eport item enales you to 2uic0ly test your 'ataase connection+ an' enales you to )ie( tale 'ata from te

    'ataase in your ro(ser* %is can e particularly useful (en prototyping*

    %e follo(ing steps 'emonstrate o( to integrate te generate' co'e into te H%M$ 'rop'o(n list you create' earlier in te tutorial*

    9*

    E1amine te column 'ata in te generate' co'e* %(o tags are use'G one is neste' insi'e te oter* %is causes te

    ,SP container -i*e*+ te >lass?is ser)er. to perform a loop on all t ale ro(s+ an' for eac ro(+ it loops troug all columns* In tis

    manner+ 'ata for te entire tale is 'isplaye'*

    =*

    Integrate te tags into te H%M$ form as follo(s* %e )alue of eac item ecomes te subject0id + an' te output

    te1t ecomes te name+ as recor'e' in te 'ataase* -Canges are 'isplaye' in -old.*

    Select a subject3

    $c#for-ach var+,row, items+,78su!"ects.rows:yIndex9,%

    $c#for-ach var+,column, items+,78row9,%

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    15/20

    !n alternati)e+ simpler (ay to integrate te tags into te H% M$ form (oul' e as follo(s*

    Select a subject3

    $c#for-ach var+,row, items+,78su!"ects.rows9,%

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    16/20

    Subject.counselor0id(A !ND Subject.subject0id ,

    Clic0 O

    A6D &u!"ect.su!"ect/id + ? $s(l#param value+,78param.su!"ect/id9,'%

    $'s(l#(uery%

    Note tat te IDE automatically a''e' te ta$lib'irecti)e nee'e' for te tag* !lso+ note tat you use' an

    tag 'irectly (itin te 2uery* Because tis 2uery relies on te subject0id)alue tat (as sumitte' from

    index.jsp+ you can e1tract te )alue using an E$ -E1pression $anguage. statement in te form of O5param.subject0id6+ an'

    ten pass it to te tag so tat it can e use' in place of te S#$ 2uestion mar0 -. 'uring runtime*

    8*

    "se a tag to set a )ariale tat correspon's to te first recor' -i*e*+ ro(. of te resultsetreturne' from te 2uery* -Ne(

    content so(n in -old*.

    SGC2 FHM Subject' Counselor

    #H Counselor.counselor0id , Subject.counselor0id(A !ND Subject.subject0id ,

    $c#set var+,counselorDetails, value+,78counselor;uery.rows@B9,'%

    !ltoug teresultsetreturne' from te 2uery soul' only contain a single recor'+ tis is a necessary step ecause te page nee's

    to access )alues from te recor' using E$ -E1pression $anguage. statements* 5ecall tat i n index.jsp+ you (ere ale to access

    )alues from te resultsetsimply y using a tag* Ho(e)er+ te tag operates y setting a )ariale

    for te ro(s containe' in te 2uery+ tus enaling you to e1tract )alues y inclu'ing te ro( )ariale in E$ statements*

    9*

    !'' te ta$lib'irecti)e for te ,S%$ corelirary to te top of te file+ so tat te tag is un'erstoo'* -Ne( content so(n

    in -old*.

    $)*tagli! prefix+,c, uri+,http#''"ava.sun.com'"sp'"stl'core,)%

    =*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    '( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    17/20

    In te H%M$ mar0up+ replace all placeol'ers (it E$ statements co'e tat 'isplay te 'ata el' in te counselorDetails)ariale*

    -Canges elo( so(n in -old.

    78counselorDetails.name9

    78counselorDetails.name9

    Description3

    78counselorDetails.description9

    Counselor3

    78counselorDetails.first/name9

    78counselorDetails.nic>/name9 78counselorDetails.last/name9


    member since3 78counselorDetails.mem!er/since9

    Contact Details3

    email3

    78counselorDetails.email9


    p%one3 78counselorDetails.telephone9

    J*

    5unning te Complete' !pplication

    @ou)e no( complete' te application* %ry running it again to see o( it 'isplays in a ro(ser* Note tat ecause of NetBeans Compile on Sa)e

    feature+ you 'o not nee' to (orry aout compiling or re'eploying te application* Wen you run a pro3ect+ you can e sure te 'eployment contains

    your latest canges*

    Clic0 te 5un Pro3ect - . utton in te main toolar* %e index.jsppage opens in te IDEs 'efault ro(ser*

    Wen index.jsp'isplays in te ro(ser+ select a su3ect from te 'rop'o(n list an' clic0 submit* @ou soul' no( e for(ar'e' to te

    response.jsppage+ so(ing 'etails correspon'ing to your selection*

    %is conclu'es te Creating a Simple We !pplication "sing a MyS#$ Dataase tutorial* %is 'ocument 'emonstrate' o( to create a simple (e

    application tat connects to a MyS#$ 'ataase* It also 'emonstrate' o( to construct an application using a asic t(otier arcitecture+ an' utilie'

    numerous tecnologies inclu'ing ,SP+ ,S%$+ ,DBC+ an' ,NDI as a means of accessing an' 'isplaying 'ata 'ynamically*

    %roulesooting

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    '( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    18/20

    Most of te prolems tat occur (it te tutorial application are 'ue to communication 'ifficulties et(een te >lass?is Ser)er Open Source

    E'ition an' te MyS#$ 'ataase ser)er* If your application 'oes not 'isplay correctly+ or if you are recei)ing a ser)er error+ te follo(ing

    e1aminations may e useful*

    Do 'ataase resources e1ist

    Do te connection pool an' 'ata source e1ist on te ser)er

    Is te MyS#$ Connector/, 'ri)er accessile to te >lass?is ser)er

    Is te 'ataase pass(or'protecte'

    !re te connection pool properties correctly set

    Do 'ataase resources e1ist

    "se te IDEs Ser)ices (in'o( -Ctrl=G = on Mac. to ensure tat te MyS#$ ser)er is running+ an' tat MyNewDatabaseis accessile an'

    contains appropriate tale 'ata*

    %o connect to te MyS#$ 'ataase ser)er+ rigtclic0 te MyS#$ Ser)er no'e an' coose Connect*

    If a connection no'e - . for MyNewDatabase'oes not 'isplay in te Ser)ices (in'o(+ you can create a connection y rigtclic0ing

    te MyS#$ 'ri)er no'e - . an' coosing Connect "sing* Enter te re2uire' 'etails in te 'ialog tat 'isplays*

    %e fiel's pro)i'e' in te Ne( Dataase Connection 'ialog mirror te "5$ string entere' in te So( ,DBC "5$ option*

    %erefore+ if you 0no( te "5$ -e*g*+ jdbc3mysl3//local%ost3/MyNewDatabase. you can paste it into te So(

    ,DBC "5$ fiel'+ an' te remaining 'ialog fiel's ecome automatically populate'*

    %o ensure tat te Subjectan' Counselortales e1ist an' tat tey contain sample 'ata+ e1pan' te MyNewDatabase

    connection no'e - . an' locate te MyNewDatabasecatalog no'e - .* E1pan' te catalog no'e to )ie( e1isting tales* @ou can

    )ie( tale 'ata y rigtclic0ing a tale no'e an' coosing ie( Data*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    '( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    19/20

    Do te connection pool an' 'ata source e1ist on te ser)er

    !fter 'eploying te application to te >lass?is ser)er+ te $lass(is%4resources.xmlcontaine' in te pro3ect soul' instruct te ser)er

    to create a ,DBC resource an' connection pool* @ou can 'etermine (eter tese e1ist from te Ser)ers no'e in te Ser)ices (in'o(*

    E1pan' te Ser)ers F te >lass?is Ser)er F 5esources no'e* E1pan' ,DBC 5esources to )ie( te jdbc/IFP!FC!D'ata source

    tat (as create' from $lass(is%4resources.xml* E1pan' te Connection Pools no'e to )ie( te I(pwa(cadPool

    connection pool tat (as create' from $lass(is%4resources.xml* -%is is 'emonstrate' ao)e*.

    Is te MyS#$ Connector/, 'ri)er accessile to te >lass?is ser)er

    Ma0e sure tat te MyS#$ Connector/, 'ri)er as een 'eploye' to te >lass?is ser)er* -%is is 'iscusse' in !''ing te 'ataase 'ri)ers

    ,!5 file to te ser)er*.

    $ocate te >lass?is ser)er installation fol'er on your computer an' 'rill 'o(n into te lassFis% domains/domain&/lib

    sufol'er* Here you soul' fin' te mysl4connector4ja*a4:.&.4bin.jarfile*

    Is te 'ataase pass(or'protecte'

    %e 'ataase nee's to e pass(or'protecte' to enale te >lass?is ser)er 'ata source to (or0 properly in tis tutorial* If you are using te

    'efault MyS#$ rootaccount (it an empty pass(or'+ you can set te pass(or' from a comman'line prompt*

    %o set your pass(or' to nbuser+ na)igate to your MyS#$ installations bin'irectory in te comman'line prompt an' enter te

    follo(ing

    s%ell> mysl 4u root

    mysl> JPD!2 mysl.user S2 Password , P!SSHDQ)nbuser)R

    4> #H Jser , )root)8

    mysl> FGJS# PHI9IGS8

    ?or more information+ see te official MyS#$ 5eference Manual Securing te Initial MyS#$ !ccounts*

    !re te connection pool properties correctly set

    Ensure tat te connection pool is (or0ing correctly for te ser)er*

    Open te Ser)ices (in'o( -Ctrl=G = on Mac. an' e1pan' te Ser)ers no'e**

    5igtclic0 te >lass?is ser)er no'e an' coose ie( !'min Console*7*

    Enter te username an' pass(or' if you are prompte'* @ou can )ie( te username an' pass(or' in te Ser)ers manager*8*

    In te tree on te left si'e of te console+ e1pan' te 5esources F ,DBC F ,DBC Connection Pools F I(pwa(cadPoolno'e* Details

    for te I(pwa(cadPoolconnection pool 'isplay in te main (in'o(*

    9*

    Clic0 te Ping utton* If te connection pool is set up correctly+ you (ill see a Pin$ Succeeded message*=*

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e

    '( %(%)'(%*

  • 7/25/2019 MySQL-NetBeans_Creating a Simple Web Application_Tutorial.pdf

    20/20

    Send s our Feedbac$

    SiteMap !out "s Contact $egal & $icences

    If te ping fails+ clic0 te !''itional Properties ta an' ensure tat te liste' property )alues are correctly set*J*

    See !lso

    ?or more information aout ,a)a (e 'e)elopment+ see te follo(ing resources*

    4 Net+eans $rticles and Tutorials

    4 &a'a %ata-ase Connecti'ity 2&%+C3

    4 &a'aSer'er "ages Standard Tag 4i-rary 2&ST43

    4 &a'a Na*ing and %irectory nterface 2&N%3

    By use of tis (esite+ you agree to te NetBeans Policies an' %erms of "se* 7;=+ Oracle Corporation an'/or its affiliates* Sponsore' y

    ing a Simple Web Application Using a MySQL Database - NetBeans... https:netbeans.org!b"ocs#ebmys$l-#e