Lab Work Java Ass
Lab Work Java Ass
Lab Work -1
Topic: Chapter-1(Fundamentals of Java)
1 Write a program to check whether a given number is armstrong or
not.
import java.io.*;
public class Program1
{
static int power(int n, int r)
{
int c, p = 1;
for (c = 1; c <= r; c++)
p = p*n;
return p;
}
public static void main(String args[])throws
Exception
{
int n, sum=0, temp, remainder, count=0;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.print("\n\n\t\t\t Enter number :- ");
n = Integer.parseInt(br.readLine());
temp = n;
while(temp!=0)
{
count++;
temp = temp/10;
}
temp = n;
while(temp!=0)
{
remainder = temp%10;
sum = sum + power(remainder,count);
temp = temp/10;
}
if( n == sum )
System.out.print("\n\n\t\t\t
Number
is
Armstrong...!\n\n");
else
System.out.print("\n\n\t\t\t Number is not
Armstrong...!\n\n");
}
}
import java.io.*;
public class Program3
{
public static void main(String args[])throws Exception
{
int febCount = 15;
int[] feb = new int[febCount];
feb[0] = 0;
feb[1] = 1;
for(int i=2; i<febCount ; i++)
{
feb[i] = feb[i-1] + feb[i-2];
}
System.out.print("\n\n\t\t\t first 15 fibonecci series :");
for(int i=2; i<febCount ; i++)
{
System.out.print("\n\n\t\t\t\t"+feb[i]);
}
System.out.print("\n\n");
}
}
****
***
***
**
**
**
**
***
***
*********
****
import java.io.*;
public class Program4
{
public static void main(String args[])throws Exception
{
int n;
BufferedReader
br
=
new
BufferedReader(new
InputStreamReader(System.in));
System.out.print("\n\n\n\t\t\t Enter length of the pattern :- ");
n = Integer.parseInt(br.readLine());
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < (n - i); j++)
System.out.print("*");
for (int j = 1; j <= i; j++)
System.out.print(" ");
for (int k = 1; k < i; k++)
System.out.print(" ");
System.out.println();
}
for (int i = n - 1; i >= 1; i--)
{
for (int j = 0; j < (n - i); j++)
System.out.print("*");
for (int j = 1; j <= i; j++)
System.out.print(" ");
for (int k = 1; k < i; k++)
System.out.print(" ");
System.out.println();
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
for (int i = 1 ; i<=n; i++)
{
for (int j = 0; j < (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
}
}
Lab Work -2
Topic: Chapter-1(Fundamentals of Java)
::-
new
Program1(empID,
name,
designation, sal);
p.display();
}
catch(Exception e)
{}
}
}
int medianPos;
try
{
BufferedReader
InputStreamReader(System.in));
br
new
BufferedReader(new
arr[i] = sc.nextInt();
if(i==4)
{
medianPos = 5/2;
System.out.print("\n\n\n\t\t\t
"+arr[medianPos]);
}
}
}
catch(Exception e)
{}
}
}
Median
:-
import java.io.*;
import java.util.*;
public class Program3
{
public static void main(String args[])
{
int m,n,p,q;
int sum = 0,c,d,k;
int firstM[][] = new int[3][3];
int secondM[][] = new int[3][3];
int mult[][] = new int[3][3];
try
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
System.out.print("\n\n\n\t\t Enter elements in First
matrix :- \n\n");
for( c = 0 ; c < 3 ; c++ )
{
for( d = 0; d<3; d++ )
firstM[c][d] = sc.nextInt();
}
System.out.print("\n\n\n\t\t Enter elements
Second matrix :- \n\n");
for( c = 0 ; c < 3 ; c++ )
{
for( d = 0; d<3; d++ )
secondM[c][d] = sc.nextInt();
}
in
System.out.print("\n\n\n\t\t\t
Multiplication
of
matrices :- \n\n");
for( c = 0 ; c < 3 ; c++ )
{
for( d = 0; d<3; d++ )
System.out.print("\t "+mult[c][d]);
System.out.print("\n");
}
}
catch(Exception e)
{}
}
}
Lab Work -3
Topic: Chapter-1(Inheritance Polymorphism )
1
Write a Java program can contain two classes: Computer and Laptop. Both
classes have their own constructors and a method. In main method create object
of two classes and call their methods.
import java.io.*;
class Computer
{
Computer()
{
}
public void display()
{
System.out.print("\n\n\t\t This is Computer...!");
}
}
class Laptop
{
Laptop()
{
}
public void display()
{
System.out.print("\n\n\t\t This is Laptop...!\n\n");
}
}
c.display();
l.display();
}
}
Create person class with data members as person_id & name. Derive two classes
Student & faculty from it. The fields of Student are course name & fees paid.
The fields of faculty are subject name & number of years experience. Use proper
method to accept values & override display method. (Using parameterized
constructor)
import java.io.*;
class Person
{
int personID;
String name;
BufferedReader(new
BufferedReader(new
String subjectName;
int experience;
BufferedReader(new
3.Your program will consist of the following classes: Shape, Circle, Square, Cube,
Sphere, Cylinder, and Glome and two interfaces Area and Volume (Area.java
and Volume.java are given below).
Your classes may only have the class variable specified in the table below and the
methods defined in the two interfaces Area and Volume. You will implement the
methods specified in the Area and Volume interfaces and have them return the
appropriate value for each shape. Class Shape will have a single public method
called getName that returns a string.
import java.io.*;
interface Area
{
public void getArea();
}
interface Volume
{
public void getVolume();
}
class Shape
{
String name;
Shape(String n)
{
this.name = n;
}
public void getName()
{
}
}
class Circle extends Shape implements Area
{
double radius;
Circle(double r, String n)
{
super(n);
this.radius = r;
}
public void getArea()
{
}
}
class Square extends Shape implements Area
{
double side;
Square(double s, String n)
{
super(n);
this.side = s;
}
public void getArea()
{
}
}
class Cylinder extends Circle implements Volume
{
double heigth;
Cylinder(double h, String n)
{
super(h,n);
}
public void getVolume()
{
}
}
class Sphere extends Circle implements Volume
{
Sphere(double r, String n)
{
super(r,n);
}
public void getVolume()
{
}
}
class Cube extends Square implements Volume
{
Cube(double s, String n)
{
super(s,n);
}
public void getVolume()
{
}
}
class Glome extends Sphere implements Volume
{
Glome(double r, String n)
{
super(r,n);
}
public void getVolume()
{
}
}
Lab Work - 4
Topic: Chapter-1(Polymorphism)
1
Create 3 classes Circle, Rectangle & Triangle. Use area() method to display
area of respective shape.( Use method overloading)
import java.io.*;
class Circle
{
public void area(double r)
{
double area = 3.14*r*r;
System.out.print("\n\n\t\t\t Area of Circle :- "+area);
}
}
class Rectangle
{
public void area(double length, double width)
{
double area = length * width;
System.out.print("\n\n\t\t\t Area of Rectangle :- "+area);
}
}
class Triangle
{
public void area(double h, double base)
{
double area = (h*base)/2;
System.out.print("\n\n\t\t\t Area of Circle :- "+area);
}
}
c.area(10);
r.area(15,15);
t.area(20,20);
2.Create a class mobile containing company name, mobile number & cost and write
necessary member functions for the following:
a. Search the mobile number with given name.
b. Search the name with given telephone number. (Use method overloading)
import java.io.*;
class Mobile
{
String[] name;
int[] mobNo;
double[] cost;
int n;
public void getData()throws Exception
{
BufferedReader
br
=
new
InputStreamReader(System.in));
BufferedReader(new
Record
not
found...!");
}
}
}
public void search(int mno)
{
for(int i=0 ; i<n ; i++)
{
if(mobNo[i]==mno)
{
System.out.print("\n\n\t\t Record found...!");
System.out.print("\n\n\t\t\t
Name
:-
"+name[i]);
System.out.print("\n\n\t\t\t Mobile number :"+mobNo[i]);
System.out.print("\n\n\t\t\t cost :- "+cost[i]);
}
else
{
System.out.print("\n\n\t\t
Record
not
found...!");
}
}
}
}
public class Program2
{
public static void main(String args[])throws Exception
{
Mobile m = new Mobile();
m.getData();
BufferedReader
br
InputStreamReader(System.in));
new
BufferedReader(new
Lab Work -5
Topic: Chapter-1 (Interfaces and Package)
Write a program to Design a Shape as an interface and then Design class for
Rectangle, Triangle and Hexagon which implements the interface and override
method drawShape().
import java.io.*;
interface Shape
{
{
Rectangle r = new Rectangle();
Triangle t = new Triangle();
Hexagon h = new Hexagon();
r.drawShape();
t.drawShape();
h.drawShape();
}
}
Lab Work -6
Topic: Chapter-1(Exception Handling).
1 .Accept two numbers a & b from command line argument & print
output as a/b and handle all possible system defined exceptions.
import java.io.*;
import java.lang.*;
public class Program1
{
public static void main(String args[])
{
try
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int div = a/b;
System.out.print("\n\n\t\t\t"+a+" / "+b+" :- "+div+"
\n\n");
}
catch(ArrayIndexOutOfBoundsException ai)
{
}
catch(ArithmeticException ae)
{
}
}
}
2.Write a class Driver with attributes vehicle no, name & age. Initialize values
through parameterized constructor. If age of driver is less than 18 then generate
user-defined exception Age Not Within the Range.
import java.lang.*;
import java.io.*;
}
}
class Driver
{
String name;
int age;
String vehicleNo;
new
BufferedReader(new
if(age<18)
{
throw new AgeLimitException(age);
}
{
System.out.print("\n\n\t\t\t
"+a.getAge()+"\n\n");
ERROR
:-
invalid
}
}
}
age...!
import java.io.*;
import java.lang.*;
InsufficientAmountException(double wAmt)
{
this.withdrawAmount = wAmt;
}
public double getAmount()
{
return withdrawAmount;
}
}
class CheckingAmount
{
int accNo;
String name;
double amount,withdrawAmount;
new
BufferedReader(new
try
{
System.out.print("\n\n\t\t Enter Account details :");
System.out.print("\n\n\t\t\t account number :- ");
accNo = Integer.parseInt(br.readLine());
System.out.print("\n\n\t\t\t Name :- ");
name = br.readLine();
System.out.print("\n\n\t\t\t Amount :- ");
amount = Double.parseDouble(br.readLine());
System.out.print("\n\n\t\t\t Withdraw Amount :- ");
withdrawAmount
Double.parseDouble(br.readLine());
new
}
}
catch(InsufficientAmountException ie)
{
System.out.print("\n\n\t\t\t ERROR :- Insufficient
account balance...!"+ie.getAmount()+"\n\n");
}
}
}
{
public static void main(String args[])throws Exception
{
CheckingAmount c = new CheckingAmount();
c.withdraw();
}
}
4.Define an exception called No equal Exception that is thrown when a float value is
not equal to 3.14. Write a program that uses the above user defined exception.
import java.io.*;
NoEqualException(double val)
{
this.value = val;
}
public double getValue()
{
return value;
}
}
new
BufferedReader(new
ERROR
:-
not
equal
to
Lab Work -7
Topic: Chapter-7(Multithreading)
import java.io.*;
import java.io.*;
Extending
Thread...!
"+i);
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
}
catch(Exception e)
{}
}
}
}
t1.start();
t2.run();
}
}
import java.io.*;
class ExtendThread extends Thread implements Runnable
{
public void run()
{
for( int i = 0 ; i<10 ; i++)
{
try
{
System.out.print("\n\n\t\t
Thread...! "+i);
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
}
class ImplementThread implements Runnable
{
public void run()
{
for( int i = 0 ; i<10 ; i++)
{
try
{
System.out.print("\n\n\t\t
Thread...! "+i);
Thread.sleep(100);
}
catch(Exception e)
{}
}
}
Extending
Implementing
}
public class Program3
{
public static void main(String args[])
{
ExtendThread t1 = new ExtendThread();
ImplementThread t2 = new ImplementThread();
t1.setPriority(1);
t1.start();
t2.run();
Lab Work -8
Topic: Chapter-7 (Multithreading)
Write a program to print name, priority of a thread and change name of current
to java thread and display the details of current thread.
import java.io.*;
Lab Work -9
Topic: Chapter- (Applets)
/*
<applet code="Program1.class" height=550 width=550>
</applet>
*/
import java.applet.*;
import java.awt.*;
public class Program1 extends Applet
{
public void init()
{}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(100,100,100,100);
g.setColor(Color.yellow);
g.fillRect(200,200,100,100);
g.setColor(Color.orange);
g.fillRect(300,100,100,100);
}
}
/*
<applet code="Program2.class" height=550 width=500>
</applet>
*/
import java.applet.*;
import java.awt.*;
public class Program2 extends Applet
{
public void paint(Graphics g)
{
Color c;
c=Color.black;
g.setColor(c);
g.fillRect(237,114,10,500);
c=Color.red;
g.setColor(c);
g.fillRect(248,125,200,25);
c=Color.white;
g.setColor(c);
g.fillRect(248,150,200,25);
c=Color.green;
g.setColor(c);
g.fillRect(248,175,200,25);
c=Color.blue;
g.setColor(c);
g.drawOval(342,149,25,25);
int l=0;
int x=355,y=162;
double x1,y1;
double d;
c=Color.black;
g.setColor(c);
for(int i=1;i<=24;i++)
{
d=(double)l*3.14/180.0;
x1=x+(double)10*Math.cos(d);
y1=y+(double)10*Math.sin(d);
g.drawLine(x,y,(int)x1,(int)y1);
l=l+(360/24);
}
}
}
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Timer;
public class Ball
{
// execute application
public static void main( String args[] )
{
JFrame frame = new JFrame( "Bouncing Ball" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
BallPanel bp = new BallPanel();
frame.add( bp );
frame.setSize( 300, 300 ); // set frame size
frame.setVisible( true ); // display frame
} // end main
}
// class BallPanel
class BallPanel extends JPanel implements ActionListener
{
private int delay = 10;
protected Timer timer;
private int x = 0;
private int y = 0;
private int radius = 15;
// x position
// y position
// ball radius
private int dx = 2;
private int dy = 2;
public BallPanel()
{
timer = new Timer(delay, this);
timer.start();
// start the timer
}
public void actionPerformed(ActionEvent e)
// will run when the timer fires
{
repaint();
}
// draw rectangles and arcs
public void paintComponent( Graphics g )
{
dx
dx
dy
dy
=
=
=
=
Math.abs(dx);
-Math.abs(dx);
Math.abs(dy);
-Math.abs(dy);
/* <applet code=signal.class
width=1200></applet>
*/
import java.applet.*;
import java.awt.*;
public class signal extends Applet
height=1200
{
int i;
public void init()
{
i=1;
}
public void paint(Graphics g)
{
if(i==1)
{
g.setColor(Color.red);
g.fillOval(30,30,50,50);
g.setColor(Color.black);
g.fillOval(30,130,50,50);
g.fillOval(30,230,50,50);
}
if(i==2)
{
g.setColor(Color.black);
g.fillOval(30,30,50,50);
g.setColor(Color.yellow);
g.fillOval(30,130,50,50);
g.setColor(Color.black);
g.fillOval(30,230,50,50);
}
if(i==3)
{
g.setColor(Color.black);
g.fillOval(30,30,50,50);
g.fillOval(30,130,50,50);
g.setColor(Color.green);
g.fillOval(30,230,50,50);
}
try
{
Thread.sleep(500);
}
catch(Exception ex){} i++;
if(i==4)
i=1;
repaint();
}
}
Last Name
ABC
XYZ
ABC XYZ
Display
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public
class
Program1
ActionListener
{
JTextField t1, t2, t3;
JLabel l1,l2;
JButton b1;
extends
Program1()
{
t1 = new JTextField(20);
t2 = new JTextField(20);
t3 = new JTextField(20);
l1 = new JLabel("First Name");
l2 = new JLabel("Last Name");
b1 = new JButton("Display");
Panel p1 = new Panel();
p1.setLayout(new GridLayout(3,2));
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p1.add(b1);
p1.add(t3);
setLayout(new FlowLayout());
add(p1);
setVisible(true);
setLocation(200,300);
JFrame
implements
setSize(550,550);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
JButton b = (JButton) ae.getSource();
if(b==b1)
{
t3.setText(t1.getText().toString()+"
"+t2.getText().toString());
}
}
public static void main(String args[])
{
new Program1();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
Program2()
{
t1 = new JTextField(20);
b1 = new JButton("Red");
b2 = new JButton("Yellow");
b3 = new JButton("Orange");
setLayout(new FlowLayout());
add(p1);
setVisible(true);
setLocation(200,300);
setSize(550,550);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
JButton b = (JButton) ae.getSource();
if(b==b1)
{
t1.setBackground(Color.red);
}
if(b==b2)
{
t1.setBackground(Color.yellow);
}
if(b==b3)
{
t1.setBackground(Color.orange);
}
}
=
=
=
=
new
new
new
new
Checkbox("Mango");
Checkbox("Grapes");
Checkbox("Watermelon");
Checkbox("Apple");
items .
Red, Green, Blue, Black in color. Depending on which menu item
was selected the background color of the frame window is set to
that color.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Program1 extends JFrame implements ItemListener
{
JMenuBar mainMenuBar;
JMenu mainMenu;
JMenuItem redItem, greenItem, blueItem, blackItem;
JFrame mainFrame;
Program1()
{
mainFrame = new JFrame("Color Menu");
mainMenuBar = new JMenuBar();
mainMenu = new JMenu("Color");
redItem = new JMenuItem("Red");
greenItem = new JMenuItem("Green");
blueItem = new JMenuItem("Blue");
blackItem = new JMenuItem("Black");
mainMenu.add(redItem);
mainMenu.add(greenItem);
mainMenu.add(blueItem);
mainMenu.add(blackItem);
mainMenuBar.add(mainMenu);
mainFrame.setJMenuBar(mainMenuBar);
mainFrame.setLocation(350,150);
mainFrame.setSize(350,350);
mainFrame.setVisible(true);
mainFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
redItem.addItemListener(this);
greenItem.addItemListener(this);
blueItem.addItemListener(this);
blackItem.addItemListener(this);
}
/*
if(menuItem.equals("Red"))
{
mainFrame.setBackground(Color.red);
}
if(menuItem.equals("Green"))
{
mainFrame.setBackground(Color.green);
}
if(menuItem.equals("Blue"))
{
mainFrame.setBackground(Color.blue);
}
if(menuItem.equals("Black"))
{
mainFrame.setBackground(Color.black);
}
}
*/
public void itemStateChanged(ItemEvent ie)
{
String menuItem = (String)ie.getItem();
if(menuItem.equals("Red"))
{
mainFrame.setBackground(Color.red);
}
if(menuItem.equals("Green"))
{
mainFrame.setBackground(Color.green);
}
if(menuItem.equals("Blue"))
{
mainFrame.setBackground(Color.blue);
}
if(menuItem.equals("Black"))
{
mainFrame.setBackground(Color.black);
}
}
public static void main(String args[])
{
new Program1();
}
}
import java.awt.*;
import javax.swing.*;
public class Program2 extends JFrame
{
JMenuBar mainMenuBar;
JMenu fileMenu, editMenu;
{
}
l.setText("x="+e.getX()+" "+"y="+e.getY());
new
BufferedReader(new
Record
saved
System.out.print("\n\n\t\t\t
Hidden...!\n\n");
}
else
{
System.out.print("\n\n\t\t\t
Hidden...!\n\n");
}
}
}
File
File
is
is
not
import java.io.*;
import java.net.*;
class Client
{
static DataInputStream din, dis;
static DataOutputStream dos;
new
Socket(InetAddress.getLocalHost(),
8080);
while(true)
{
dos.writeUTF(dis.readLine());
System.out.println("SERVER : " + din.readUTF());
}
}
catch(Exception e)
{
System.out.println("EXCEPTION : " + e);
}
}
}
import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[])
{
try
{
Socket s;
ServerSocket ss = new ServerSocket(8080);
while(true)
{
s = ss.accept();
(new MyThread(s)).start();
}
}
catch(Exception e)
{
System.out.println("EXCEPTION : " + e);
}
}
}
try
{
while(true)
{
}
}
catch(Exception e)
{
System.out.println("EXECPTION IN SERVER :" + e);
}
}
}
2.Write a java code in which client will send file name to server
and server will send content of that file to client.
import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[])
{
try
{
Socket s;
ServerSocket ss = new ServerSocket(8080);
while(true)
{
s = ss.accept();
(new MyThread(s)).start();
}
}
catch(Exception e)
{
System.out.println("EXCEPTION : " + e);
}
}
}
try
{
while(true)
{
}
}
catch(Exception e)
{
System.out.println("EXECPTION IN SERVER :" + e);
}
}
}
import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss = new ServerSocket(4000);
Socket s;
while(true)
{
s = ss.accept();
(new MyThread(s)).start();
}
}
}
DataInputStream dis;
DataOutputStream dos;
FileInputStream fin;
String fname;
String data="";
byte ch = (byte)fin.read();
while (ch!=-1)
{
data = data + (char)ch;
ch = (byte)fin.read();
}
dos.writeUTF(data);
}
catch(Exception e)
{
}
}
}
3.Write a program for client and server to send and receive message using
connectionless networking.
import java.io.*;
import java.net.*;
class Client
{
public static void main(String[ ] args)
{
try{
InetAddress
ia
InetAddress.getByName("172.16.64.221");
int port = Integer.parseInt("2000");
DatagramSocket ds = new DatagramSocket();
String str1 = "Hello.......";
byte buffer[ ] = new byte[1024];
buffer = str1.getBytes();
DatagramPacket
dp
=
DatagramPacket(buffer,buffer.length,ia,2000);
new
ds.send(dp);
}
catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
}
import java.net.*;
import java.io.*;
class Server
{
//public static DatagramSocket ds;
public static void main(String args[])
{
try
{
DatagramSocket ds = new DatagramSocket(2000);
byte buffer[] = new byte[20];
while( true )
{
DatagramPacket
dp
=
new
DatagramPacket(buffer,buffer.length);
ds.receive(dp);
String str = new String (dp.getData( ));
System.out.println("Data from Client is " + str);
}
}
catch(Exception e)
{
//System.out.println(e);
e.printStackTrace();
}
}
}
Write a program for JDBC to insert, update and delete a record on click of buttons
from Book table having fields Bookno ,Bookname and BookPrice
import java.io.*;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
JButton save,update;
JLabel bnoLabel, nameLabel, priceLabel;
JTextField bnoTextField, nameTextField, priceTextField;
Panel p1;
JFrame f1;
Program1()
{
f1 = new JFrame("Book details");
p1 = new Panel();
save = new JButton("save");
update = new JButton("update");
f1.setLayout(new FlowLayout());
f1.add(p1);
f1.setSize(500,500);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
save.addActionListener(this);
update.addActionListener(this);
}
if(b==save)
{
int bno = Integer.parseInt(bnoTextField.getText());
price
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:Book");
st = con.createStatement();
st.executeUpdate("insert
values(bno,'name',price)");
System.out.print("\n\n\n\t\t\t
into
Record
Book
saved
successfully...!");
}
catch(Exception e)
{}
}
if(b==update)
{
int bno = Integer.parseInt(bnoTextField.getText());
String name = nameTextField.getText();
double
Double.parseDouble(bnoTextField.getText());
price
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:Book");
st = con.createStatement();
st.executeUpdate("update
Book
SET
price="+price);
System.out.print("\n\n\n\t\t\t
Record
successfully...!");
}
catch(Exception e)
{}
}
}
public static void main(String args[])throws Exception
{
new Program1();
}
}
import java.io.*;
saved
import java.sql.*;
con=DriverManager.getConnection("jdbc:odbc:Book");
Write a program for JDBC to insert, update and delete a record in a table using
methods of ResultsetMetadta with buttons and frames.
import java.io.*;
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
Program1()
{
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.setLayout(new GridLayout(3,2));
p1.add(bnoLabel);
p1.add(bno);
p1.add(nameLabel);
p1.add(name);
p1.add(priceLabel);
p1.add(price);
p2.setLayout(new GridLayout(1,3));
p2.add(insert);
p2.add(update);
p2.add(delete);
setLayout(new FlowLayout());
add(p1);
add(p2);
setVisible(true);
setLocation(350,250);
setSize(550,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
insert.addActionListener(this);
}
Class.forName("com.mysql.jdbc.Driver");
con
DriverManager.getConnection("jdbc:mysql://localhost/Book","root","");
st = con.createStatement();
if(b == insert)
{
st.executeUpdate("insert
values(bNo,'name',price)");
System.out.print("\n\n\n\t\t\t
into
Record
Book
saved
successfully...!");
}
if(b == delete)
{
st.executeUpdate("insert
values(bNo,'name',price)");
System.out.print("\n\n\n\t\t\t
successfully...!");
}
}
catch(Exception e)
{}
}
into
Record
Book
saved
}
}
2.Write a program for JDBC to insert, update and delete a record in a table using
prepared and callable statements.
import
import
import
import
import
java.io.*;
java.sql.*;
java.awt.*;
javax.swing.*;
java.awt.event.*;
Program3()
{
f1 = new JFrame("Book details");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
bno = new JTextField(20);
name = new JTextField(20);
price = new JTextField(20);
bnoLabel = new JLabel("Book number");
nameLabel = new JLabel("Name");
priceLabel = new JLabel("price");
insert = new JButton("Insert");
update = new JButton("Update");
delete = new JButton("Delete");
p1.setLayout(new GridLayout(3,2));
p1.add(bnoLabel);
p1.add(bno);
p1.add(nameLabel);
p1.add(name);
p1.add(priceLabel);
p1.add(price);
p2.setLayout(new GridLayout(1,3));
p2.add(insert);
p2.add(update);
p2.add(delete);
f1.setLayout(new FlowLayout());
f1.add(p1);
f1.add(p2);
f1.setVisible(true);
f1.setLocation(350,250);
f1.setSize(550,350);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
insert.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
int bNo = Integer.parseInt(bno.getText());
String nm = name.getText();
double cost = Double.parseDouble(price.getText());
try
{
JButton b = (JButton) ae.getSource();
Connection con;
Statement st;
PreparedStatement ps;
Class.forName("com.mysql.jdbc.Driver");
con
=
DriverManager.getConnection("jdbc:mysql://localhost/Book","root","");
st = con.createStatement();
if(b == insert)
{
ps = con.prepareStatement("insert into Book
values(?,'?',?)");
ps.setInt(1,bNo);
ps.setString(2,nm);
ps.setDouble(3,cost);
ps.executeUpdate();
JOptionPane.showMessageDialog(f1,"Record
saved...!");
}
if(b == delete)
{
st.executeUpdate("insert
into
Book
values(bNo,'name',price)");
System.out.print("\n\n\n\t\t\t
Record
saved
successfully...!");
}
}
catch(Exception e)
{}
}
public static void main(String args[])throws Exception
{
new Program3();
}
}
Write a RMI application to check whether the given number is Prime or not . Write
all interfaces and required classes.
ClientPrime
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
{
BufferedReader
br
InputStreamReader(System.in));
new
BufferedReader(new
remotePrime rp = (remotePrime)Naming.lookup("prime");
rp.prime(n);
}
}
RemotePrime
import java.rmi.*;
ServerPrime
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class
remotePrime
serverPrime
extends
UnicastRemoteObject
implements
{
public serverPrime()throws Exception
{
super();
}
public void prime(int number) throws Exception
{
int n = number;
int result=1;
int temp;
for(int i=2;i<=n/2;i++)
{
temp=n%i;
if(temp==0)
{
result = 0;
break;
}
else
{
result = 1;
}
}
if(result == 1)
{
System.out.print("\n\n\t\t\t Number is prime...!");
}
else
{
System.out.print("\n\n\t\t\t Number is not prime...!");
}
}
Write a RMI application to reverse the given string . Write all interfaces and
required classes.
ClientReverse
import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
public class clientReverse
{
public static void main(String args[])throws Exception
{
BufferedReader
br
=
new
BufferedReader(new
InputStreamReader(System.in));
remoteReverse
r
=
(remoteReverse)
Naming.lookup("reverse");
System.out.print("\n\n\t\t\t Enter String :- ");
String str = br.readLine();
r.result(str);
}
}
RemoteReverse
import java.rmi.*;
ServerReverse
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
Naming.bind("reverse",sr);
}
}