You are on page 1of 22

ADVANCED JAVA LAB

LAB MANNUAL

Prepared by:

Ms. Shilpy Gupta (Lecturer, CSE/I !

April, "#$#

Advanced Institute of Technology & Management, Palwal Lab Planning Course: C ! emester: "th Su%&ect C'(e ) CSE-406-E Su%&ect Na*e) Advanced JAVA Lab Su%&ect +aculty ,'r la%) Ms. Shilpy Gupta
Course #b$ective: To enhance the knowledge about various implementations in the working of a compiler.

Program %o&

'escri(tion of the Program to be covered in Lab

Assign&

) * + , . / " 0 )1

WAP for Client/Server (SOCKET) PROGRAMMING WAP to implement MULTITHREADING in JAVA. WAP to implement the concept of SWINGS. Write steps to create DATA SOURCE NAME (DSN). WAP to execute DATABASE QUERIES using JDBC connecti it!. WAP to access Database using D"t"B"#eMet"$"t". WAP to access Database using Re#%&tSetMet"$"t". WAP to develop an RMI #!#te'. WAP to implement SER(LETS. WAP to implement JSP.

Practical !2ecution : 20 3ritten : 10 4iva : 20

Ms& hil(y 5u(ta (C'urse C''r(i-at'r!

PROGRAM ) *+
2

WAP ,OR CLIENT-SER(ER (SOCKET) PROGRAMMING


//Clie-t Si(e .r'/ra* i p!"t #ava.i!.$% i p!"t #ava.net.$% public class client & public static v!id ain'St"in( s)*+ & t"y & S!c,et se"ve"% St"in( st"-..% St"in( na e-ne/ St"in('+% 0ata1nputSt"ea d-ne/ 0ata1nputSt"ea 'Syste .in+% 2"intSt"ea t!se"ve"% 3u44e"ed5eade" 4"! se"ve"% se"ve"-ne/ S!c,et'.l!calh!st.676+% Syste .!ut.p"intln'.C!nnected889n:2"ess ct"l;C t! <uit:.+% 1nputSt"ea 5eade" is"-ne/ 1nputSt"ea 5eade"'se"ve".(et1nputSt"ea '++% 4"! se"ve"- ne/ 3u44e"ed5eade"'is"+% t!se"ve"-ne/ 2"intSt"ea 'se"ve".(et=utputSt"ea '++% Syste .!ut.p"int'.Ente" y!u" na e>.+% na e-d."eadLine'+% Syste .!ut.p"intln'.?elc! e6 .;na e;.8.+% st"-na e% t!se"ve".p"intln'st"+% /hile't"ue+ & Syste .!ut.p"int'na e;.>.+% st"-na e;.>.;d."eadLine'+% t!se"ve".p"intln'st"+% st"-4"! se"ve"."eadLine'+% Syste .!ut.p"intln'st"+% @ @ catch'EAcepti!n e+ & Syste .!ut.p"intln'e+% @ @ @ //Ser0er Si(e .r'/ra* B

i p!"t #ava.i!.$% i p!"t #ava.net.$% public class se"ve & public static v!id ain'St"in( s)*+ & Se"ve"S!c,et sc% S!c,et client% St"in( na e-ne/ St"in('+% 0ata1nputSt"ea d% 2"intSt"ea t!Client% 3u44e"ed5eade" 4"! Client% St"in( st"-..% t"y & d-ne/ 0ata1nputSt"ea 'Syste .in+% sc-ne/ Se"ve"S!c,et'76+% Syste .!ut.p"intln'.Se"ve"Sta"ted.+% Syste .!ut.p"intln'.2"ess ct"l;C t! <uit.+% Syste .!ut.p"int'.Ente" y!u" na e>.+% na e-d."eadLine'+% Syste .!ut.p"intln'.?elc! e6 .;na e;.89n?aitin( 4!" Client88.+% client-sc.accept'+% 1nputSt"ea 5eade" is"-ne/ 1nputSt"ea 5eade"'client.(et1nputSt"ea '++% 4"! Client-ne/ 3u44e"ed5eade"'is"+% t!Client-ne/ 2"intSt"ea 'client.(et=utputSt"ea '++% st"-4"! Client."eadLine'+% Syste .!ut.p"intln'st";. C!nnected88.+% /hile't"ue+ & st"-4"! Client."eadLine'+% Syste .!ut.p"intln'st"+% Syste .!ut.p"int'na e;.>.+% st"-na e;.>.;d."eadLine'+% t!Client.p"intln'st"+% @ @ catch'EAcepti!n e+ & Syste .!ut.p"intln'e+% @ @ @

PROGRAM ) .+
4

WAP TO IMPLEMENT MULTITHREADING IN JA(A


class Ce/Dh"ead i ple ents 5unnable & St"in( na e% Dh"ead t% Ce/Dh"ead'St"in( th"eadna e+ & na e-th"eadna e% t-ne/ Dh"ead'this6na e+% Syste .!ut.p"intln'.Ce/ th"ead>.;t+% t.sta"t'+% @ public v!id "un'+ & t"y & 4!"'int i-E%iF0%i--+ & Syste .!ut.p"intln'na e;.>.;i+% Dh"ead.sleep'G000+% @ @ catch'1nte""uptedEAcepti!n e+ & Syste .!ut.p"intln'na e;.1nte""upted.+% @ Syste .!ut.p"intln'na e;.eAitin(.+% @ @ class MultiDh"ead0e ! & public static v!id ain'St"in( a"(s)*+ & ne/ Ce/Dh"ead'.=ne.+% ne/ Ce/Dh"ead'.D/!.+% ne/ Ce/Dh"ead'.Dh"ee.+% t"y & Dh"ead.sleep'G000+% @ catch'1nte""uptedEAcepti!n e+ & Syste .!ut.p"intln'.Main Dh"ead 1nte""upted.+% @ Syste .!ut.p"intln'.Main Dh"ead EAitin(.+% E

@ @

PROGRAM ) /+
6

WAP TO IMPLEMENT THE CONCEPT O, SWINGS


i p!"t #avaA.s/in(.$% i p!"t #ava.a/t.$% i p!"t #ava.a/t.event.$% H$Iapplet c!de-.sh!/s/in(.class. hei(ht-E00 /idth-E00FIHappletF$H public class sh!/s/in( eAtends JApplet i ple ents Acti!nListene" & JDeAtJield t4% JLabel lG6l26lB6l4% J3utt!n b% JC! b!3!A cb% JList #l% J5adi!3utt!n 64% 3utt!nG"!up b(% J2anel p% public v!id init'+ & C!l!" c-ne/ C!l!"'24K62GL6GK2+% p-ne/ J2anel'+% p.set3ac,("!und'c+% lG-ne/ JLabel'.Ente" M!u" Ca e>.+% l2-ne/ JLabel'.Gende">.+% lB-ne/ JLabel'.Nuali4icati!n>.+% l4-ne/ JLabel'.O!bbies>.+% t4-ne/ JDeAtJield'20+% b-ne/ J3utt!n'.Sh!/.+% St"in( st")*-&.33A.6.3A.6.3CA.6.M3A.6.MA.6.MCA.@% St"in( st"G)*-&.DV.6.3==PS.6.M=V1ES.6.GAMES.6.1CDE5CED.@% G"id3a(Lay!ut (l-ne/ G"id3a(Lay!ut'+% G"id3a(C!nst"aints (bc-ne/ G"id3a(C!nst"aints'+% p.setLay!ut'(l+% cb-ne/ JC! b!3!A'st"+% #l-ne/ JList'st"G+% -ne/ J5adi!3utt!n'.Male.+% 4-ne/ J5adi!3utt!n'.Je ale.+% (bc.("idA-0% (bc.("idy-0% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (bc.4ill-G"id3a(C!nst"aints.O=51Q=CDAL% L

(l.setC!nst"aints'lG6(bc+% p.add'lG+% (bc.("idA-G% (bc.("idy-0% (bc.("id/idth-2% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints't46(bc+% p.add't4+% (bc.("idA-0% (bc.("idy-2% (bc.("id/idth-G% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'l26(bc+% p.add'l2+% b(-ne/ 3utt!nG"!up'+% (bc.("idA-G% (bc.("idy-2% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% b(.add' +% (l.setC!nst"aints' 6(bc+% p.add' +% (bc.("idA-2% (bc.("idy-2% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% b(.add'4+% (l.setC!nst"aints'46(bc+% p.add'4+% (bc.("idA-0% (bc.("idy-4% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'lB6(bc+% p.add'lB+% (bc.("idA-G% (bc.("idy-4% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'cb6(bc+% p.add'cb+% (bc.("idA-0% (bc.("idy-6% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% K

(l.setC!nst"aints'l46(bc+% p.add'l4+% (bc.("idA-G% (bc.("idy-6% (bc.anch!"-G"id3a(C!nst"aints.C=5DO% (l.setC!nst"aints'#l6(bc+% p.add'#l+% (bc.("idA-G% (bc.("idy-K% (bc.("id/idth-G% (bc.4ill-G"id3a(C!nst"aints.O=51Q=CDAL% (bc.anch!"-G"id3a(C!nst"aints.CECDE5% (l.setC!nst"aints'b6(bc+% p.add'b+% (etC!ntent2ane'+.add'p+% b.addActi!nListene"'this+% @ public v!id acti!n2e"4!" ed'Acti!nEvent a+ & St"in( s% s-t4.(etDeAt'+% i4' .isSelected'++ & s-s;.6 .; .Male.% @ else & s-s;.6 .;.Je ale.% @ s-s;. pu"suin( .;St"in(.value=4'cb.(etSelected1te '++% s-s;. li,es .;St"in(.value=4'#l.(etSelectedValue'++% J=pti!n2ane.sh!/Messa(e0ial!('this6s+% @ @ HHEAecute the ab!ve p"!("a li,e a si ple applet p"!("a usin( appletvie/e"

PROGRAM ) 0+ WRITE STEPS TO CREATE DATA SOURCE NAME (DSN)


7

G. =pen C!nt"!l 2anel and Clic, Ad inist"ative D!!ls6 then !pen this 0ial!( b!A 2. Clic, the Add butt!n. Dhen !pen this 0ial!( 3!A

B. Select MS Access 0"ive" and clic, !n Jinish butt!n. Dhen !pen this 0ial!( b!A

G0

4.Jill the 0SC na e and select database. Dhen clic, !, butt!n

6. Select the database na e'deep. db+ and clic, the =P butt!n

GG

L. Clic, the =, R 4inish step.

PROGRAM ) 1+

G2

WAP TO E2ECUTE DATABASE QUERIES USING JDBC CONNECTI(IT3.


//Create a pr'/ra* 1%ase.&a0a2 as ,'ll'3s) i p!"t #ava.lan(.$% i p!"t #ava.s<l.$% public class base & public static v!id ain'St"in( a"(s)*+ & t"y & St"in( st"G-.C5EADE DA3LE student . ;.'cSid 1CDEGE56.;.cSna e VA5COA5'2E++.% St"in( st"2-.1CSE5D 1CD= student 'cSid6cSna e+ VALTES':G:6:a:+.% St"in( st"B-.1CSE5D 1CD= student 'cSid6cSna e+ VALTES':2:6:b:+.% St"in( st"4-.1CSE5D 1CD= student 'cSid6cSna e+ VALTES':B:6:c:+.% St"in( st"E-.SELECD $ J5=M student .% St"in( st"6-. T20ADE student SED cSid-:E: ?OE5E cSna e-:a:.% St"in( st"L-.0ELEDE J5=M student ?OE5E cSid-:E:.% Class.4!"Ca e'.sun.#dbc.!dbc.Jdbc=dbc0"ive".+% C!nnecti!n c!n-0"ive"Mana(e".(etC!nnecti!n'.#dbc>!dbc>test.6..6..+% State ent st t-c!n.c"eateState ent'+% st t.eAecute'st"G+% Syste .!ut.p"intln'.table is c"eated.+% int c!untG-st t.eAecuteTpdate'st"2+% Syste .!ut.p"intln'.values a"e inse"ted.+% int c!unt2-st t.eAecuteTpdate'st"B+% Syste .!ut.p"intln'.values a"e inse"ted.+% int c!untB-st t.eAecuteTpdate'st"4+% Syste .!ut.p"intln'.values a"e inse"ted.+% 5esultSet "s-st t.eAecuteNue"y'st"E+% Syste .!ut.p"intln'.id9tna e.+% /hile'"s.neAt'++ & St"in( id-"s.(etSt"in('.cSid.+% St"in( na e-"s.(etSt"in('.cSna e.+% Syste .!ut.p"int'id+% Syste .!ut.p"int'na e+% @ GB

int c!unt4-st t.eAecuteTpdate'st"6+% Syste .!ut.p"intln'.table is updated.+% int c!untE-st t.eAecuteTpdate'st"L+% Syste .!ut.p"intln'.table is deleted.+% c!n.cl!se'+% @ catch'EAcepti!n eA+ & Syste .!ut.p"intln'.e""!" !ccu""ed.; eA+% @ @ @ //C'*pile the pr'/ra* 3ith 1&a0ac %ase.&a0a2 //Create DSN as *e-ti'-e( i- pre0i'us pr'/ra*. //E4ecute the pr'/ra* 3ith 1&a0a %ase2

PROGRAM ) 4+
G4

WAP TO ACCESS DATABASE USING DATABASEMETADATA


//Create a pr'/ra* 1D"t"B"#eMet"D"t".&a0a2 as ,'ll'3s) i p!"t #ava.s<l.$% i p!"t #ava.lan(.$% public class 0ata3aseMeta0ata & public static v!id ain'St"in( a"(s)*+ & t"y & Class.4!"Ca e'.sun.#dbc.!dbc.Jdbc=dbc0"ive".+% C!nnecti!n c!n-0"ive"Mana(e".(etC!nnecti!n'.#dbc>!dbc>test.6..6..+% St"in( )*bytes-&.table.@% 0atabaseMeta0ata db d-c!n.(etMeta0ata'+% 5esultSet "s - db d.(etDables'null6null6null6bytes+% Syste .!ut.p"intln'.Att"ibutes !4 database> .+% /hile'"s.neAt'++ & Syste .!ut.p"intln'"s.(etSt"in('.DableSCa e.++% @ c!n.cl!se'+% @ catch'EAcepti!n eA+ & Syste .!ut.p"intln'eA+% @ @ @ //C'*pile the pr'/ra* usi-/ 1&a0ac D"t"B"#eMet"D"t".&a0a2 //Create DSN as i- pre0i'us pr'/ra* //e4ecute the pr'/ra* usi-/ 1&a0a D"t"B"#eMet"D"t" 2

PROGRAM ) 5+
GE

WAP TO ACCESS DATABASE (TABLE DATA) USING DATABASEMETADATA


//Create a pr'/ra* 16#Met"D"t".&a0a2 as ,'ll'3s) i p!"t #ava.s<l.$% i p!"t #ava.lan(.$% i p!"t #ava.i!.$% public class "sMeta0ata & public static v!id ain'St"in( a"(s)*+ & t"y & Class.4!"Ca e'.sun.#dbc.!dbc.Jdbc=dbc0"ive".+% C!nnecti!n c!n-0"ive"Mana(e".(etC!nnecti!n'.#dbc>!dbc>test.6..6..+% St"in( st"-ne/ St"in('+% 0ata1nputSt"ea dis-ne/ 0ata1nputSt"ea 'Syste .in+% Syste .!ut.p"intln'.Ente" table na e>.+% st"-dis."eadLine'+% 2"epa"edState ent ps - c!n.p"epa"eState ent'.SELECD $ J5=M .;st"+% 5esultSet "s - ps.eAecuteNue"y'+% 5esultSetMeta0ata "s d-"s.(etMeta0ata'+% int c!ls - "s d.(etC!lu nC!unt'+% Syste .!ut.p"intln'."esultSet Meta0ata c!lu n C!unt-. ; c!ls+% Syste .!ut.p"intln'.c!lu n Meta0ata .+% 4!" 'int i - G% i I- c!ls% i;;+ & Syste .!ut.p"intln'.c!lu n nu be" . ; i+% HH (et the desi(nated c!lu n:s table na e. Syste .!ut.p"intln'"s d.(etC!lu nCa e'i++% @ c!n.cl!se'+% @ catch'EAcepti!n eA+ & G6

Syste .!ut.p"intln'eA+% @ @ @ //C'*pile the pr'/ra* usi-/ 1&a0ac 6#Met"D"t".&a0a2 //Create DSN as i- pre0i'us pr'/ra* //e4ecute the pr'/ra* usi-/ 1&a0a 6#Met"D"t" 2

PROGRAM ) 7+ 5A. 6 DEVEL6. AN 7MI S8S EM


GL

Step $) Ma9e a- I-ter,ace 1:ell'.&a0a2)


i p!"t #ava." i.$% public inte"4ace Oell! eAtends 5e !te & public St"in( sayOell!'+ th"!/s 5e !teEAcepti!n% @

Step ") I*ple*e-tati'- 1:ell'I*pl.&a0a2


i p!"t #ava." i.$% i p!"t #ava." i.se"ve".$% public class Oell!1 pl eAtends Tnicast5e !te=b#ect i ple ents Oell! & public Oell!1 pl'+ th"!/s 5e !teEAcepti!n & supe"'+% @ public St"in( sayOell!'+ th"!/s 5e !teEAcepti!n & "etu"n .Oell!8pete" s ith.% @

@
Step ;) 7e%i-( the Ser0er 1:ell'Ser0er.&a0a2
import java.rmi.*; import java.rmi.server.*; public class HelloServer { public static void main(String args[]) { try { System.setSecurityManager( ne !M"SecurityManager()); Hello #$ne Hello"mpl(); %aming.rebind(&server&'#); System.out.println(&object is registered&); System.out.println(&no servaer is aiting&); ( catc#()*ception e) { System.out.println(&error+&,e); GK

( ( (

Step <)

L''9Up the Clie-t 1:ell'Clie-t.&a0a2

import java.rmi.*; public class Hello-lient { public static void main(String args[]) { try { Hello #$(Hello)%aming.loo.up(&rmi+//012.034.5.33/server&); System.out.println(&client+ Hello6&); System.out.println(&server+& ,#.sayHello()); ( catc#()*ception e) { System.out.println(&)rror+&,e); ( ( (

Step 5: Compile the

four source files

Step 6: Generate the Stub & Skelton using: rmic HelloImpl Step 7: Start the RMI registr start rmiregistr Step !: start the ser"er #a"a HelloSer"er Step $: Start the client #a"a HelloClient

PROGRAM ) 8+ WAP TO IMPLEMENT SER(LETS.


Step $! Create a ,'l(er i- '*cat=s 3e%apps ,'l(er, say e4a*ples. G7

Step "! Create ,'l(ers as e4a*ples >? 5EB>IN+ >? classes Step ;! 5rite the ,'ll'3i-/ pr'/ra* i- a ,ile 1:ell'5'rl(.&a0a2 a-( sa0e i- classes ,'l(er i i i i p!"t #ava.i!.$% p!"t #ava.util.$% p!"t #avaA.se"vlet.$% p!"t #avaA.se"vlet.http.$%

public class Oell!?!"ld eAtends OttpSe"vlet & public Oell!?!"ld'+ & supe"'+% @ p"!tected v!id d!Get'OttpSe"vlet5e<uest "e<uest6 OttpSe"vlet5esp!nse "esp!nse+ th"!/s Se"vletEAcepti!n6 1=EAcepti!n & HH D=0= Aut!-(ene"ated eth!d stub "esp!nse.setC!ntentDype'.teAtHht l.+% 2"int?"ite" p/ - "esp!nse.(et?"ite"'+% p/.p"intln'.Iht lF.+% p/.p"intln'.IheadFItitleFOell! ?!"ldIHtitleFIHtitleF.+% p/.p"intln'.Ib!dyF.+% p/.p"intln'.IhGFOell! ?!"ldIHhGF.+% p/.p"intln'.IHb!dyFIHht lF.+% "esp!nse.addOeade"'.5e4"esh.6 .G.+% p/.p"intln'ne/ 0ate'+.t!St"in('++% @ p"!tected v!id d!2!st'OttpSe"vlet5e<uest "e<uest6 OttpSe"vlet5esp!nse "esp!nse+ th"!/s Se"vletEAcepti!n6 1=EAcepti!n & HH D=0= Aut!-(ene"ated eth!d stub @ @ Step <! 'pe- the c'**a-( pr'*pt usi-/ c*( a-( set the (ri0e l'cati'- as) C)@.r'/ra* +iles@Apache S',t3are +'u-(ati'-@ '*cat A.#@3e%apps@e4a*ples@5EB> IN+@classes Step <! C'*pile the a%'0e pr'/ra* usi-/ 1&a0ac :ell'5'rl(.&a0a2. Step B! C'py paste the 3e%.4*l ,ile ,r'* 3e%apps >? 766 >? 5EB>IN+ ' 3e%apps >? e4a*ples >? 5EB>IN+ 20

Step A! E(it the 3e%.4*l ,ile t' set the ser0let -a*e a-( ser0let>*appi-/s acc'r(i-/ t' y'ur ser0let. Step C! Start the '*cat Ser0ice 7u--er i- 1C)@.r'/ra* +iles@Apache S',t3are +'u-(ati'-@ '*cat A.#@%i-2 Step D! 6pe- the 3e% %r'3ser a-( type the ,'ll'3i-/ U7L t' ru- the ser0let ,r'* a-y clie-t syste*) http)//l'calh'st)D#D#/e4a*ples/:ell'5'rl( 'r http)// EI. a((ress ', ser0er? /e4a*ples/:ell'5'rl( a-( press e-ter.

PROGRAM ) *9+ WAP TO IMPLEMENT JSP


Step $! Create a ,'l(er i- '*cat=s 3e%apps ,'l(er, say e4a*ples.

2G

Step ;! 5rite the ,'ll'3i-/ pr'/ra* a-( sa0e li9e a- ht*l ,ile, 3ith e4te-si'- &sp, say 1ti*e.&sp2 i- the a%'0e create( ,'l(er Iht lF IheadF ItitleF Cu""ent D1 e IHtitleF IHheadF Ib!dyF Itable ali(n-:cente": F It"F ItdF IbFCu""ent 0ate and Di eIHbF IHtdF ItdF IU-ne/ #ava.util.0ate'+UF IHtdF IHt"F IHtableF IHb!dyF IHht lF Step ;! Start the '*cat Ser0ice 7u--er i- 1C)@.r'/ra* +iles@Apache S',t3are +'u-(ati'-@ '*cat A.#@%i-2 Step <! 6pe- the 3e% %r'3ser a-( type the ,'ll'3i-/ U7L t' ru- the ser0let ,r'* a-y clie-t syste*) http)//l'calh'st)D#D#/e4a*ples/ti*e.&sp 'r http)// EI. a((ress ', ser0er? /e4a*ples/ti*e.&sp a-( press e-ter.

22

You might also like