You are on page 1of 44

# SONA COLLEGE OF TECHNLOLOGY

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

JAVA PROGRAMMING LAB MANUAL

ABSTRACT CLASSES
PROGRAM
import java.io.*; abstract class AreaMethods { abstract void rect(int l,int b); abstract void sqr(int a); abstract void tri(int ba,int h); } class Area extends AreaMethods { void rect(int l,int b) { System.out.println("Area of the Rectangle : "+(l*b)); } void sqr(int a) { System.out.println("Area of the Square : "+(a*a)); } void tri(int ba,int h) { System.out.println("Area of the Triangle : "+(0.5*ba*h)); } } class AbstractDemo { public static void main(String args[]) throws Exception { int l,b,a,ba,h; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the length of the Rectangle:"); l=Integer.parseInt(br.readLine()); System.out.println("Enter the breadth of the Rectangle:"); b=Integer.parseInt(br.readLine()); System.out.println("Enter the side of the Square:");

a=Integer.parseInt(br.readLine()); System.out.println("Enter the base of the Triangle:"); ba=Integer.parseInt(br.readLine()); System.out.println("Enter the height of the Triangle:"); h=Integer.parseInt(br.readLine()); Area obj=new Area(); obj.rect(l,b); obj.sqr(a); obj.tri(ba,h); } }

OUTPUT
Z:\cs2k944> javac AbstractDemo.java Z:\cs2k944> java AbstractDemo Enter the length of the Rectangle: 4 Enter the breadth of the Rectangle: 3 Enter the side of the Square: 4 Enter the base of the Triangle: 5 Enter the height of the Triangle: 6 Area of the Rectangle : 12 Area of the Square : 16 Area of the Triangle : 15.0

INHERITANCE
PROGRAM
abstract class Worker { String name; double rate; Worker(String n, double r) { name = n; rate = r; } double getRate() { return rate; } abstract double computePay(int hours); } class HourlyWorker extends Worker { double pay; HourlyWorker(String n, double r) { super(n, r); } double computePay(int hours) { if(hours<40) pay = getRate() * hours; if (hours > 40) pay = (getRate()*40) + (getRate() * (hours - 40) / 2); return pay; } } class SalariedWorker extends Worker { SalariedWorker(String n, double r) {

println(s.super(n.computePay(30)).out.println(h.0 1800. HourlyWorker h = new HourlyWorker("Harry".0 1600. 40).computePay(30)). } } OUTPUT Z:\cs2k940> javac WorkerTester.0 . System.out. System. System.out.computePay(50)).java Z:\cs2k940> java WorkerTester 1600. } double computePay(int hours) { return getRate() * 40.computePay(50)). System.println(s.0 1200.println(h. } } class WorkerTester { public static void main(String[] args) { SalariedWorker s = new SalariedWorker("Sally". 40).out. r).

parseInt(br.int b). int getLCM(int a.println("Enter the second number:"). System. } class GLClass implements GLInterface { public int getGCD(int a. else return getGCD(b. } } class GL { public static void main(String args[]) throws Exception { int a. import java.println("Enter the first number:"). System.a%b). interface GLInterface { int getGCD(int a.in)). Write a java program using interface to find the GCD & LCM of two numbers.out.INTERFACES PROGRAM 1. a=Integer. } public int getLCM(int a.*.out.readLine()).int b) { return((a*b)/getGCD(a. BufferedReader br=new BufferedReader(new InputStreamReader(System. . GLClass obj=new GLClass().b)).int b).b.int b) { if(b==0) return a.io.

getLCM(a.java Z:\cs2k944> java GL Enter the first number: 24 Enter the second number: 12 GCD = 12 LCM = 24 2. System.println("LCM = "+obj.getGCD(a.String binary2). Write the java program using interface to do the following a) Binary to Decimal b) Decimal to Binary c) Two’s Complement of a Binary number d) Addition of two Binary numbers import java. } .out. String toBinary(int decimal).*. String twosComplement(String binary).b=Integer.b)). String binaryAdd(String binary1.println("GCD = "+obj.io. } } OUTPUT Z:\cs2k944> javac GL.parseInt(br.b)). interface BinaryInterface { int toDecimal(String binary).readLine()). System.out.

toBinary (Integer.parseInt(binary2. System.out. System.2))). } public String binaryAdd(String binary1. System.in)).println("Enter the binary number:").println("Decimal-->Binary = " + bc. return(Integer.toBinaryString(Integer.out. System.parseInt(binary. System. int j=~i+1.twosComplement(br.class BinaryClass implements BinaryInterface { public int toDecimal(String binary) { return(Integer.out.out. System.println("DECIMAL TO BINARY"). } public String toBinary(int decimal) { return(Integer. System.parseInt(binary1.println("Enter the binary number:").2)+Integer.readLine())).out.2).println("Enter the decimal number:"). BinaryClass bc=new BinaryClass().readLine()))).println("BINARY TO DECIMAL").out. System.println("Two's Complement = "+bc.parseInt(binary.toDecimal(br.readLine())). } } class Binary { public static void main(String args[]) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.out.println("ADDITION OF TWO BINARY NUMBERS").println("TWO'S COMPLEMENT"). System.println("Binary-->Decimal = "+bc.out. System.2)).out. } public String twosComplement(String binary) { int i=Integer.toBinaryString(j)).parseInt (br.out.toBinaryString(decimal)).String binary2) { return(Integer.println("Enter two binary numbers:").out. . System.

io. br.*. } } OUTPUT Z:\cs2k944> javac Binary. .println("Addition Result = " + bc. Write a java program using interface to implement Queue import java.readLine())). interface QueueInterface { void insert(int item).out.java Z:\cs2k944> java Binary BINARY TO DECIMAL Enter the binary number: 0101 Binary-->Decimal = 5 DECIMAL TO BINARY Enter the decimal number: 15 Decimal-->Binary = 1111 TWO'S COMPLEMENT Enter the binary number: 0001 Two's Complement = 11111111111111111111111111111111 ADDITION OF TWO BINARY NUMBERS Enter two binary numbers: 0101 0010 Addition Result = 111 3.readLine() .System.binaryAdd (br. void delete().

System. } } public void delete() { if(front==rear) System. rear=0.println("QUEUE IS FULL").println("QUEUE IS EMPTY").out.front. else { que[rear]=x. QueueClass() { que=new int[size].out. int que[].out.println("Element Deleted"). } class QueueClass implements QueueInterface { int size=5.out.void display(). int rear. } } public void display() { if(front==rear) System.out.println("QUEUE IS EMPTY"). System. else { { . rear++. front=0. } public void insert(int x) if(rear>=size) System.println("Element Inserted"). else { front++.

println(). do { System.parseInt(br.readLine()). BufferedReader br=new BufferedReader(new InputStreamReader(System. System. case 3: q.choice. System. q. Delete from the Queue").delete().println("3. Display Queue"). case 2: q.out.parseInt(br. x=Integer.in)). Insert into the Queue").out. QueueClass q=new QueueClass(). for(int i=front.i++) System. break.out.insert(x). break. System. System.out.println("Enter the element to be inserted:").System.println("2.print("\t"+que[i]).out.out. Exit"). System.out.readLine()).display().println("Elements in the Queue are"). case 4: .out.println("Enter the choice:"). break. System.println("\t"+"MENU"). } } } class Queue { public static void main(String args[])throws Exception { int x.println("1.println("4.out.out. choice=Integer.i<rear. switch(choice) { case 1: System.

exit(0). Exit Enter the choice: 1 Enter the element to be inserted: 4 Element Inserted MENU .println("Wrong Choice!!!").out. } }while(true). Delete from the Queue 3. } } OUTPUT Z:\cs2k944> javac Queue. Exit Enter the choice: 1 Enter the element to be inserted: 5 Element Inserted MENU 1. default: System. Display Queue 4. Insert into the Queue 2. Insert into the Queue 2.java Z:\cs2k944> java Queue MENU 1. Delete from the Queue 3. Display Queue 4.System.

Delete from the Queue 3. Delete from the Queue 3.1. Display Queue 4. Insert into the Queue 2. Insert into the Queue 2. Exit Enter the choice: 2 Element Deleted MENU 1. Insert into the Queue 2. Insert into the Queue 2. Exit Enter the choice: 3 Elements in the Queue are 5 4 MENU 1. Display Queue 4. Delete from the Queue 3. Delete from the Queue 3. Display Queue 4. Exit Enter the choice: 2 QUEUE IS EMPTY . Exit Enter the choice: 2 Element Deleted MENU 1. Display Queue 4.

Exit Enter the choice: 4 .MENU 1. Delete from the Queue 3. Display Queue 4. Insert into the Queue 2.

BOLD. import java. /*<applet code="MouseEventDemo" width=400 height=300> </applet>*/ public class MouseEventDemo extends Applet implements MouseListener.awt. .*.*. import java.Font. import java.EVENT HANDLING USING APPLETS PROGRAM 1. addMouseMotionListener(this).*. } public void mouseExited(MouseEvent e) { msg="Mouse Exited". repaint(). repaint().18)). } public void mouseClicked(MouseEvent e) { msg="Mouse Clicked". repaint(). } public void mouseEntered(MouseEvent e) { msg="Mouse Entered".awt.applet.MouseMotionListener { String msg="".event. addMouseListener(this). Design an Applet program to perform MouseEvent. public void init() { setFont(new Font("Arial".

120. } } OUTPUT Z:\cs2k944> javac MouseEventDemo. repaint().} public void mousePressed(MouseEvent e) { msg="Mouse Pressed". } public void paint(Graphics g) { g. } public void mouseMoved(MouseEvent e) { msg="Mouse Moved".150). } public void mouseDragged(MouseEvent e) { msg="Mouse Dragged". repaint().java . repaint().java Z:\cs2k944> AppletViewer MouseEventDemo.drawString(msg. } public void mouseReleased(MouseEvent e) { msg="Mouse Released". repaint().

int res1. l1=new Label("Enter 1st Operand: "). l2=new Label("Enter 2nd Operand: ").awt. mul=new Button("Multiplication"). Button add.*. t2=new TextField(25). add(l2). /*<applet code="Calculation" width=565 height=160> </applet>*/ public class Calculation extends Applet implements ActionListener { TextField t1. l4=new Label(" "). import java.l2. import java. Label l1. add(l1).l4. t1=new TextField(25).mod.Font.event.div. add(add).applet. add(t2). l3=new Label("Result: ").awt. . div=new Button("Division").l3. public void init() { setFont(new Font("Arial". add=new Button("Addition"). double res.mul. import java.sub.*.20)). Design an Applet program to perform Arithmetic Calculations. add(t1).t2. sub=new Button("Subtraction").*.2. mod=new Button("Modulus").BOLD.

*. Design an Applet program to find Fibonacci Series and Factorial of a number.getText())).event. . import java.java Z:\cs2k944> AppletViewer Calculation.*.*.if(x.setText(""+res1). } } } OUTPUT Z:\cs2k944> javac Calculation.applet.getText()))%(Integer.awt. import java.awt.equals("Modulus")) { res1=(Integer.parseInt(t2. import java.java 3. l4.parseInt(t1. /*<applet code="Fibo_Fact" width=400 height=250> </applet>*/ public class Fibo_Fact extends Applet implements ActionListener { Label l1.

. fibo=new Button("Fibonacci"). l1=new Label("Enter the number:").fibon.TextField t1. add(l1).i++) { fac=fac*i.getSource(). int n=Integer. fac=1.addActionListener(this).fibo2. fibo. public void init() { setFont(new Font("Arial". add(fibo). add(fact). fact=new Button("Factorial"). } if(x==fibo) { msg="".Font.i<=n.20)). Button fact.BOLD. if(x==fact) { msg="".parseInt(t1.fibo. String msg. fact. for(int i=1. msg="". } msg=""+fac. } public void actionPerformed(ActionEvent ae) { Object x=ae. int fac. add(t1).getText()).fibo1. t1=new TextField(10).parseInt(t1. int n=Integer.n.addActionListener(this).getText()).

fibon=fibo1+fibo2. fibo1=fibo2.fibo1=0.drawString(msg. for(int i=0. fibo2=1.java Z:\cs2k944> AppletViewer Fibo_Fact. } } OUTPUT Z:\cs2k944> javac Fibo_Fact. } public void paint(Graphics g) { g. } } repaint(). fibo2=fibon.i++) { msg=msg+fibo1+" ".i<n.java .20.150).

Thread t. System. while((tok=input.io. } catch(IOException e) {} } public void run() { wordCount(). class FileThread implements Runnable { String Filename.TT_EOF) if(tok == StreamTokenizer.TT_WORD) count++.THREADS ( SINGLE AND MULTIPLE ) PROGRAM 1. Start a new thread for each file.nextToken())!= StreamTokenizer.*.start().out.name). import java. t=new Thread(this. t. } public void wordCount() { try { StreamTokenizer input=new StreamTokenizer(new FileReader(Filename)). int count=0. String name.println(Filename+": "+count). int tok=0. name=tname. FileThread(String tname) { Filename=tname. } } class FileWordCounter { public static void main(String args[]) { . Write a program WordCount that counts the words in one or more files.

length ==0) { System. System.i++) new FileThread(args[i]).println("Give the File Name in the Command Line.").txt City.exit(0).txt: 11 Project. } } } OUTPUT City.length.txt Project Management System Text Editor Banking System Payroll System Voting System Z:\cs2k944> javac FileWordCounter.i<args.txt Project.java Z:\cs2k944> java FileWordCounter City.. } else { for(int i=0.out.if(args.txt Subject..txt Java Programming Computer Networks Software Engineering Formal Language and Automata Theory Project.txt: 11 .txt Salem Erode Chennai Subject.txt: 3 Subject.

start().OUTPUT Z:\cs2k944> javac MultiThreadDisplay. Write a java program that correctly implements producer consumer problem using the concept of inter thread communication.c=c. t=new Thread(this."Consumer Thread"). t. Thread t.java Z:\cs2k944> java MultiThreadDisplay Good Morning Hello Welcome Good Morning Hello Good Morning Welcome Good Morning Hello Good Morning Hello Welcome Hello Welcome Welcome 3. } public void run() { while(true) { . Consumer(Stock c) { this. class Consumer implements Runnable { Stock c.

Thread t.start().s=s.random()*20)).getStock((int) (Math. } } void stop() { t. }catch(InterruptedException e) {} s.try { t. }catch(InterruptedException e) {} c. } public void run() { while(true) { try { t.stop(). } } class Producer implements Runnable { Stock s. t=new Thread(this.random()*20)).sleep(750). } } ."Producer Thread"). t. Producer(Stock s) { this. } } void stop() { t.sleep(750).addStock((int)(Math.stop().

out. break.out. } public synchronized int getStock(int j) { while(true) { if(goods>=j) { goods-=j.println("Waiting For Stock To Come.out.println("\nStock cannot be added since it exceeds the limits"). } else { System. System. } wait().out.. notify(). System.println("STOCK TAKEN AWAY :"+j)..println("Present Stock :"+goods)..out."). System. } else System. public synchronized void addStock(int i) { if((goods+i)<30) { goods+=i.println("Present Stock :"+goods).. System.out.out.out.println("\nSTOCK ADDED :"+i).println("Stock Not Enough. System. }catch(InterruptedException e) {} .public class Stock { int goods=0.println("\nRequested Stock -> "+j).println("\nRequested Stock -> "+j).out."). System. System. try { } } return goods.

java Z:\cs2k944> java Stock STOCK ADDED :17 Present Stock :17 Requested Stock -> 4 STOCK TAKEN AWAY :4 Present Stock :13 STOCK ADDED :7 Present Stock :20 Requested Stock -> 13 . Consumer c=new Consumer(j). c.stop().out.exit(0).stop().sleep(5000). }catch(InterruptedException e) {} System. p.println("THREAD STOPPED"). } } OUTPUT Z:\cs2k944> javac Stock. Producer p=new Producer(j).join(). p. System.public static void main(String args[]) { Stock j=new Stock().t.t. try { Thread. c.join().

STOCK TAKEN AWAY :13 Present Stock :7 STOCK ADDED :12 Present Stock :19 Requested Stock -> 18 STOCK TAKEN AWAY :18 Present Stock :1 STOCK ADDED :14 Present Stock :15 Requested Stock -> 15 STOCK TAKEN AWAY :15 Present Stock :0 STOCK ADDED :18 Present Stock :18 Requested Stock -> 7 STOCK TAKEN AWAY :7 Present Stock :11 Stock cannot be added since it exceeds the limits Requested Stock -> 7 STOCK TAKEN AWAY :7 Present Stock :4 THREAD STOPPED .

10. g2d.10. class LetterH extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2d=(Graphics2D)g. g2d.27. LetterE.10).40.50).setRenderingHint(RenderingHints.50).10. import java.27).27.drawLine(10.drawLine(10.setStroke(new BasicStroke(8)).drawLine(10.setRenderingHint(RenderingHints.RenderingHints.swing. g2d. g2d. using only lines and circles.KEY_ANTIALIASING.40. }} class LetterE extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2d=(Graphics2D)g. Make classes LetterH.drawLine(40. g2d.drawLine(10. VALUE_ANTIALIAS_ON).*.27).awt. g2d. and do not use System.10.drawLine(10. g2d. g2d.50).10. and LetterO.RenderingHints.drawLine(10.10.40. Write a program to plot the string “HELLO”. import javax.*.out.40. g2d.50). } } .setStroke(new BasicStroke(8)). g2d. g2d.50.40. VALUE_ANTIALIAS_ON). LetterL.SWINGS PROGRAM 1.KEY_ANTIALIASING. Do not call drawstring.

f.setVisible(true). f. g2d.class LetterL extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2d=(Graphics2D)g.RenderingHints.setLayout(new GridLayout(1. f.setStroke(new BasicStroke(8)).KEY_ANTIALIASING.10.RenderingHints.setSize(300.setRenderingHint(RenderingHints.100).add(new LetterE()).drawLine(10.40.100). g2d.add(new LetterL()). f.50).EXIT_ON_CLOSE).add(new LetterH()).10. f.drawLine(10.KEY_ANTIALIASING. f.drawOval(10. f. VALUE_ANTIALIAS_ON). }} .add(new LetterL()).setLocation(100.40). } } class Hello { public static void main(String args[]) { JFrame f=new JFrame("Hello Frame").setStroke(new BasicStroke(8)). g2d.setRenderingHint(RenderingHints.setDefaultCloseOperation(JFrame.50.50). g2d.5)). g2d. VALUE_ANTIALIAS_ON).add(new LetterO()). f. } } class LetterO extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2d=(Graphics2D)g. f. f. g2d.10.40. g2d.

140.250. g2d.100.drawOval(316. g2d.setColor(Color. Write a program that displays the Olympic rings. g2d.drawOval(150.RenderingHints.blue).red).setColor(Color. g2d.200.swing.drawOval(208. import java.awt.Font. g2d.100). import javax. g2d.100). Color the rings in the Olympic colors.BOLD.100.setRenderingHint(RenderingHints. class OlympicRings extends JPanel { public void paintComponent(Graphics g) { Graphics2D g2d=(Graphics2D)g.*.java Z:\cs2k944> java Hello 2.100.KEY_ANTIALIASING.drawOval(100.100.setStroke(new BasicStroke(8)).drawString("OLYMPIC RINGS". .setColor(Color.*. g2d. g2d.setFont(new Font("Arial". VALUE_ANTIALIAS_ON).OUTPUT Z:\cs2k944> javac Hello.setColor(Color. g2d.100).200. g2d. Provide a class OlympicRing and a class OlympicRingsTest.30)).setColor(Color. g2d.black). g2d.green).yellow). g2d.200.100).100).

setLocation(100.g2d.drawOval(258.EXIT_ON_CLOSE). f.java Z:\cs2k944> java OlympicRingsTest .setDefaultCloseOperation(JFrame.setVisible(true).550).100). } } class OlympicRingsTest { public static void main(String args[]) { JFrame f=new JFrame("Olympic Rings"). f. } } OUTPUT Z:\cs2k944> javac OlympicRingsTest. f.100).250.100.add(new OlympicRings()).setSize(550. f. f.

System. while ((strLine = inFile.in)). } } } Content.readLine().filename.out.println("File Contents:"). BufferedReader br=new BufferedReader(new InputStreamReader (System. import java.println("Enter the filename:").readLine()) != null) { System.println("Error: " + e. BufferedReader inFile = new BufferedReader(new InputStreamReader(fstream)). } } catch(Exception e) { System.*. System.out.getMessage()). filename=br. FileInputStream fstream = new FileInputStream(filename). Write a program to read the content of a file.txt Java Programming C Programming C++ Programming .err.io.FILE HANDLING AND I/O HANDLING PROGRAM 1. class FileRead { public static void main(String args[]) { try{ String strLine.out.println (strLine).

filename. File outFile = new File(filename).io. Write a program to write a content to a file.println("Enter the Filename:"). filename=br.println("Content to a file is wrote successfully"). while(true) { if((k=br.equalsIgnoreCase("stop")) { System. System.readLine()).out.txt File Contents: Java Programming C Programming C++ Programming 2.in)).java Z:\cs2k944> java FileRead Enter the filename: Content. BufferedReader br=new BufferedReader(new InputStreamReader (System. import java. } .*.readLine().OUTPUT Z:\cs2k944> javac FileRead. PrintStream out=new PrintStream(outFile).out. System.println("Enter the content of the file <Type stop in new line to Exit>:"). public class FileWrite { public static void main(String[] args) { try { String k.out. break.

import java. } } catch (IOException e){} } } OUTPUT Z:\cs2k944> javac FileWrite. char searchChar.println(k).else out. Write a Program to search a number of occurrences of a particular character in a file.*.io.txt Enter the content of the file <Type stop in new line to Exit>: Software Engineering Computer Networks Theory of Computation Stop Content to a file is wrote successfully Contents.java Z:\cs2k944> java FileWrite Enter the Filename: Contents. .txt Software Engineering Computer Networks Theory Of Computation 3. class CharSearch { public static void main(String[] args) { String filename.

while ((fileChar = in. } catch(Exception e) {} } } Content.fileChar.println("Enter the Character to be searched:").read()) != -1) { if((char)fileChar==searchChar) count++. FileInputStream in = new FileInputStream(filename).txt Java Programming C Programming C++ Programming OUTPUT Z:\cs2k944> javac CharSearch.out.out. searchChar=(char)System. BufferedReader br=new BufferedReader(new InputStreamReader(System.out.out.int count=0.println("Enter the filename:"). filename=br.readLine().println(searchChar+" exists "+count+" times in the file"). System. try{ System.in.in)).read().java Z:\cs2k944> java CharSearch Enter the filename: Content.println("The searched character is not present in the file").txt Enter the Character to be searched: m m exists 6 times in the file . else System. } if(count==0) System.

Write a Program to search a number of occurrences of a particular word in a file. for(String str:strarr) { if(str. } } if(count==0) System. else System.word. BufferedReader br=new BufferedReader(new InputStreamReader(System. int count=0. System. word=br.io.println("The searched word is not present in the file"). while ((strLine = inFile.equalsIgnoreCase(word)) count++.readLine().readLine()) != null) { String[] strarr = strLine. try{ System.out. FileInputStream fstream = new FileInputStream(filename).in)).4.println("Enter the word to be searched:").readLine().println("Enter the filename:"). } catch(Exception e) {} } } .println(word+" exists "+count+" times in the file").out.filename.out.out. filename=br.split(" "). class WordSearch { public static void main(String[] args) { String strLine. import java. BufferedReader inFile = new BufferedReader(new InputStreamReader(fstream)).*.

txt Enter the word to be searched: Programming Programming exists 3 times in the file .java Z:\cs2k944> java WordSearch Enter the filename: Content.Content.txt Java Programming C Programming C++ Programming OUTPUT Z:\cs2k944> javac WordSearch.

bal=balance(st). l2=new JLabel("Balance :"). import java.awt.*.50. con=DriverManager."").l3. l4=new JLabel(""+bal). int bal. private static ResultSet rs=null.getMessage() . "ERROR MESSAGE". st=con.forName("sun. private static Statement st=null.showMessageDialog(null.l4.*. b2=new JButton("Withdraw"). class BankDatabase extends JFrame implements ActionListener { JButton b1. import javax. String acc.20). import java.jdbc.setBounds(50.sql.exit(0).getConnection("jdbc:odbc:bank".swing.event. b1=new JButton("Deposit"). l1. Write a program to establish JDBC connectivity between a java program and a database. BankDatabase(String acc) { try{ Class.*. } l1=new JLabel("Account No. } catch(Exception e) { JOptionPane.b2.awt.DATABASE APPLICATIONS (JDBC ) PROGRAM 1.l2. this.e.100.i. private static Connection con=null.createStatement(). 1).20. .amt.100.acc=acc. import java.odbc.setBounds(50.JdbcOdbcDriver"). l3=new JLabel(acc).*.20)."". System. setLayout(null). :"). JLabel l1. l2.

l4.executeQuery("select balance from bank where accno='"+acc+"'").l3.100.exit(0).200). bal=Integer. b1. setVisible(true). add(b1). setDefaultCloseOperation(JFrame. add(b2).20). b2.showMessageDialog(null.setBounds(150. } . does not Exit".20).setBounds(180.addActionListener(this). System.20.getString(1)).100.50. setLocation(175. System. } public void actionPerformed(ActionEvent ae) { if(ae.getSource()==b2) { withdraw(st).exit(0). b2. setTitle("Transaction")."Account No.showMessageDialog(null.20). add(l1). 1). b1.parseInt(rs.setBounds(180. } } public int balance(Statement st) { try { rs=st. } } catch(SQLException se) { JOptionPane.150.getMessage(). 1).next()) { i++.200).150."ERROR MESSAGE".100.100.20). add(l3). add(l2).se."ERROR MESSAGE". while(rs. add(l4). } return bal.setBounds(30.EXIT_ON_CLOSE). } if(i==0) { JOptionPane. } if(ae.getSource()==b1) { deposit(st). setSize(300.addActionListener(this).

setText(""+bal).parseInt(amt). "Enter the Amount to Deposit : ".executeUpdate("update bank set balance='"+Integer. l4.toString(bal)+"' where accno='"+acc+"'"). 1). l4. } catch(SQLException se) { JOptionPane.showMessageDialog(null."ERROR MESSAGE".showMessageDialog(null.se. "Enter the Amount to withdraw : ". "Message". "Amount to withdraw". 1)."ERROR MESSAGE".showMessageDialog(null.1). st. } catch(SQLException se) { JOptionPane. "Message".showMessageDialog(null. JOptionPane.getMessage(). if(amt!=null) { bal+=Integer. "Transaction Successful!". } else JOptionPane. if(amt!=null) { bal-=Integer.1). JOptionPane.showMessageDialog(null. "You pressed cancel button".setText(""+bal). 1).getMessage(). 1). } } } .showInputDialog(null. } else JOptionPane."Transaction Successful!". "Message". 1). st.se.parseInt(amt). "Amount to Deposit". "Message". "You pressed cancel button".public void withdraw(Statement st) { try { amt = JOptionPane.toString(bal) +"' where accno='"+acc +"'").showInputDialog(null.showMessageDialog(null.executeUpdate("update bank set balance='" + Integer. } } public void deposit(Statement st) { try { amt = JOptionPane. 1).

showInputDialog(null. 1). "You pressed cancel button". if(str != null) { new BankDatabase(str). 1). "Account Number".java Z:\cs2k944> java Bank . } else JOptionPane. "Message". "Enter the Account Number : ". } } OUTPUT Z:\cs2k944> javac Bank.showMessageDialog(null.class Bank { public static void main(String args[]) { String str = JOptionPane.