Java Programming (MDE117A) - Lab Manual
Java Programming (MDE117A) - Lab Manual
Program 1 : Setup Java Programming development environment using: Command prompt.(Classpath and path setup)
Any IDE (Eclipse, Netbeans, VScode, Jcreatoretc.).
Soln. :
(b) Launch
2. Choose an appropriate directory for your workspace, i.e., where you would like to save your files
(e.g., c:\myproject\eclipse for Windows).
3. If the "Welcome" screen shows up, close it by clicking the "cross" button next to the "Welcome" title.
1. Select "File" menu ⇒ "New" ⇒ "Java project" (or "File" ⇒ "New" ⇒ "Project" ⇒ "Java project").
5. In "JRE", select "Use default JRE (currently 'JDK10.0.x')". But make sure that your JDK is 1.8 and above.
6. In "Project Layout", check "Use project folder as root for sources and class files".
Click "Next" button.
8. The source file "[Link]" opens on the editor panel (the center pane). Enter the following codes :
public class HelloWorld {
public static void main(String ar[ ]) {
[Link]("Hello, world!");
}
}
3. The output "Hello, world!" appears on the Console panel (the bottom pane).
Java Programming L-3 Lab Manual
Soln. :
import [Link];
class ExpressionEvaluation
{
public static void main(String ar[ ])
{
int n1, n2, ad, sub, mult;
int leftShift, rightShift, bitwiseAnd, bitwiseOr;
float div;
Scanner sc = new Scanner([Link]);
[Link]("Enter 2 values");
n1 = [Link]( );
n2 = [Link]( );
ad = n1 + n2;
sub = n1 - n2;
mult = n1 * n2;
div = n1 / (float)n2;
[Link]("Addition is : " + ad);
Java Programming L-4 Lab Manual
The if statement
import [Link];
class ifdemo
{
public static void main(String ar[ ])
{
int age;
Java Programming L-5 Lab Manual
Output
Output
Java Programming L-6 Lab Manual
else if Ladder
import [Link].*;
class Exbuff
{
public static void main(String ar[ ]) throws IOException
{
int Java,Sys,IP,total,avg;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter marks of Java: ");
Java = [Link]([Link]( ));
Marks
[Link]("Enter marks of Sys: ");
are
Sys = [Link]([Link]( )); accepted
and
[Link]("Enter marks of IP: ");
converte
IP = [Link]([Link]( )); d in int
total = Java + Sys + IP;
avg = total / 3;
[Link]("Total marks : "+total);
[Link]("Average : "+avg);
if(Java>=40 && Sys>=40 && IP>=40)
{
Student should
if(avg>=80)
pass in all subjects
[Link]("Grade : A");
else if(avg>=60)
[Link]("Grade : B");
else if(avg>=40)
[Link]("Grade : C");
}
else
[Link]("Fail...");
}
}
Output
Java Programming L-7 Lab Manual
import [Link].*;
class Exbuff
{
public static void main(String ar[ ]) throws IOException
{
int n1=0,n2=0,r,ch;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("1 : Addition");
[Link]("2 : Subtraction"); Displaying
[Link]("3 : Multiplcation"); menu
[Link]("4 : Division");
[Link]("Select your choice : ");
ch = [Link]([Link]( ));
if(ch>=1 && ch<=4) Accept numbers only
{ for valid choice
if(n2!=0)
Division by zero is
{ not allowed
r = n1 / n2;
[Link]("Division is "+r);
}
else
[Link]("Cannot divide by zero");
break;
default :
[Link]("Invalid choice");
}
}
}
Output
class testpr
{
public static void main(String ar[ ])
{
int i,sum;
sum = 0;
for(i=101;i<200;i++)
{
if(i%7==0)
{
sum = sum + i;
}
}
Java Programming L-9 Lab Manual
[Link]("Sum is "+sum);
}
}
Output
Do -While
class testpr
{
public static void main(String ar[ ])
{
int a,b,c,i;
a = 1;
b = 2;
i = 1;
[Link]("1 1 2");
do
{
c = a + b;
[Link](" "+c);
a = b;
b = c;
i = i + 1;
}while(i<9);
}
}
Output
Java Programming L-10 Lab Manual
While loop
import [Link].*;
class sumofdig
{
public static void main(String ar[ ])
{
Scanner sc = new Scanner([Link]);
int n,rem,sum;
sum = 0;
[Link]("Enter a number :");
n = [Link]( );
while(n>0)
{
rem = n % 10;
sum = sum + rem ;
n = n / 10;
}
[Link]("Sum of digits : "+sum);
}
}
Output
String class
class exTest
{
public static void main(String ar[ ])
Java Programming L-11 Lab Manual
{
String str = "Kunal";
String str1 = "Phoenix InfoTech";
[Link](str);
[Link](str1);
[Link]([Link](2));
[Link]([Link]( ));
[Link]([Link](1));
[Link]([Link](1,3));
[Link]([Link]("Ku"));
[Link]([Link]("abc"));
[Link]([Link]("KUNAL"));
[Link]([Link]("KUNAL"));
}
}
Output
StringBuffer class
class exTest2
{
public static void main(String ar[ ])
{
String str = "Kunal";
String str1 = "Phoenix InfoTech";
[Link]([Link]('e'));
[Link]([Link]('e'));
[Link]("Hello ".concat(str));
[Link]([Link](str1));
[Link]([Link]( ));
Java Programming L-12 Lab Manual
[Link]([Link]( ));
[Link]([Link]('l','m'));
[Link]([Link]("Info"));
[Link](" Hello ".trim( ));
}
}
Output
Use of Array.
Use of Vectors
Soln. :
Use of Array
class Exarray
{
public static void main (String ar[ ])
{
int[] myArray; Declares an Array of
integers
Output
class Exarray
{
public static void main (String ar[ ])
{
int myarr[][]={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
[Link](myarr[i][j]+" ");
}
[Link]( );
}
}
}
Output
Java Programming L-14 Lab Manual
Use of Vectors :
import [Link].*;
class coll7
{
public static void main(String ar[ ])
{
Scanner sc = new Scanner([Link]);
Vector v = new Vector(5);
[Link]("Current size "+[Link]( ));
[Link](new Integer(10));
[Link](new Integer(20));
[Link](new Float(4.5));
[Link](new Float(80.90));
[Link]("ABC");
[Link]("XYZ");
Output
Java Programming L-15 Lab Manual
int x=15;
Converting int to
Integer y = [Link](x);
integer
Output
Output
class ExCons
{
int n1,n2,sum;
ExCons( )
{
n1 = 10; Definition of
default constructor
n2 = 5;
}
void cal( )
{
sum = n1 + n2;
[Link]("Summation is "+sum);
}
}
class ExCons1
{
[Link]( );
Default constructor get called
}
}
Java Programming L-17 Lab Manual
Output
class ExCons
{
int n1,n2,sum;
ExCons( )
{
n1 = 0; Definition of default
n2 = 0; constructor
}
ExCons(int x, int y)
{
[Link]("This is parameterized constructor");
n1 = x;
Definition of
n2 = y; parameterized
} constructor
void cal( )
{
sum = n1 + n2;
[Link]("Summation is "+sum);
}
}
class ExCons1
{
public static void main(String ar[ ])
{
new ExCons( ); Default constructor get called
ExCons obj = new ExCons(10,20);
[Link]( ); Parameterized
} constructor get
called
}
Java Programming L-18 Lab Manual
Output
Single Inheritance
class A
{
void displayBase( )
{
[Link]("Base Class Method");
}
}
Extends keyword is
class B extends A used to inherit a
{ class
void displayDerived( )
{
[Link]("Derived Class Method");
}
}
class inh
{
public static void main(String ar[ ])
{
Base class method
B obj = new B( ); can be accessed
[Link]( ); using derived class
object
[Link]( );
}
}
Java Programming L-19 Lab Manual
Output
Multilevel Inheritance
class A
{
void display1( )
{
[Link]("Class A Method");
}
}
class B extends A
{
void display2( )
{
[Link]("Class B Method");
}
}
class C extends B
{
void display3( )
{
[Link]("Class C Method");
}
}
class inh
{
public static void main(String ar[ ])
{
C obj = new C( );
obj.display1( ); Object of class C can
access members of
obj.display2( ); both class A and B
obj.display3( );
}
}
Java Programming L-20 Lab Manual
Output
Soln. :
interface Animal
{
public void sound( );
public void foundin( );
}
class Dog implements Animal
{
public void sound( )
{
[Link]("Dog Barks");
}
class InterfaceTest
{
public static void main(String ar[ ])
{
Dog d1 = new Dog( );
[Link]( );
[Link]( );
[Link]( );
Monkey m1 = new Monkey( );
[Link]( );
[Link]( );
}
}
The output of above program will be :
Built in packages
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Java Programming L-22 Lab Manual
class DemoBuiltinPackages
{
public static void main(String ar[ ]) throws UnknownHostException, IOException
{
// Accepting 3 Strings using class Scanner
Scanner sc = new Scanner([Link]);
[Link]("Enter 3 Names");
String s1 = [Link]( );
String s2 = [Link]( );
String s3 = [Link]( );
}
}
Java Programming L-23 Lab Manual
File 1 : [Link]
package mypack1;
public class ArithmeticOperations
{
public int getAddition(int x, int y)
{
return(x+y);
}
public int getSubtraction(int x, int y)
{
return(x-y);
}
}
File 2 : [Link]
package mypack1;
public class RelationalOperations
{
public int getLargest(int x, int y)
{
return x>y ? x : y;
}
Java Programming L-24 Lab Manual
import [Link];
import [Link];
class DemoUserDefinedPackages
{
public static void main(String ar[ ])
{
[Link] op1 = new [Link]( );
[Link] op2 = new [Link]( );
Program 11 : Write programs for implementation of try, catch and finally block.
Soln. :
class Exception2
int a=0,b=0,res;
try
a=[Link](args[0]);
b=[Link](args[1]);
res = a/b;
[Link]("Division is "+res);
catch(ArithmeticException e)
catch(ArrayIndexOutOfBoundsException e)
[Link]("Insufficient arguments");
finally
res = a + b;
[Link]("Addition is "+res);
} Executes even if
exception is not
} handled
}
Java Programming L-26 Lab Manual
Output
Soln. :
import [Link].*;
class Exbuff
{
public static void main(String ar[ ]) throws IOException
{
String nm;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter your name : ");
nm = [Link]( );
Creating object for
[Link]("Hello : "+nm); BufferedReader
}
}
Output
class test
{
static void divide( )
{
int x,y,z;
try
Java Programming L-27 Lab Manual
{
x=5;
y=0;
z = x/y ;
[Link](x + "/"+ y +" = " + z);
}
catch(ArithmeticException e)
{
[Link]("Exception Caught in Divide( )");
[Link]("Cannot Divide by Zero in Integer Division");
throw e Re-throwing the exception
}
}
}
public class RethrowingExceptions
{
public static void main(String ar[ ])
{
[Link]("Start of main( )");
try
{
[Link]( );
}
catch(ArithmeticException e)
{
[Link]("Caught in Main");
[Link](e);
}
}
}
Output
Java Programming L-28 Lab Manual
Soln. :
class thread1
{
public static void main(String ar[ ])
{
Thread t = [Link]( );
[Link]("Current Thread : "+t);
t prints : name of
[Link]("My Thread");
thread(default-main), priority
(default-5), group of
thread(default-main)
Output
Program 14 : Write program to design any type of form using AWT components.
Soln. :
import [Link].*;
class GUIForm extends Frame
{
public GUIForm( )
{
setLayout(null);
Font f1 = new Font("Arial", [Link], 21);
Font f2 = new Font("Times New Roman", [Link], 18);
Java Programming L-30 Lab Manual
Program 15 : Write program to create a menu bar with variousmenu items and sub menu items.
Soln. :
import [Link].*;
class MenuDemo extends Frame
{
public MenuDemo( )
{
MenuBar mb = new MenuBar( );
Menu m1 = new Menu("File");
Menu m2 = new Menu("Edit");
Menu m3 = new Menu("Format");
Menu m4 = new Menu("Open");
MenuItem mi1 = new MenuItem("New");
MenuItem mi2 = new MenuItem("Save");
MenuItem mi3 = new MenuItem("Save As");
MenuItem mi4 = new MenuItem("Cut");
MenuItem mi5 = new MenuItem("Copy");
MenuItem mi6 = new MenuItem("Paste");
MenuItem mi7 = new MenuItem("Font");
MenuItem mi8 = new MenuItem("File 1");
MenuItem mi9 = new MenuItem("File 2");
CheckboxMenuItem cmi1 = new CheckboxMenuItem("Word Wrap",true);
[Link](m1);
[Link](m2);
[Link](m3);
[Link](mi1);
[Link](mi2);
[Link](mi3);
[Link](mi4);
[Link](mi5);
[Link](m4); // nesting of menu
[Link](mi6);
[Link](mi7);
[Link](cmi1);
[Link](mi8);
[Link](mi9);
setMenuBar(mb);
}
Java Programming L-33 Lab Manual
Program 16 : Write program to demonstrate the use of border layout. The layout shows four buttons at four sides with
captions “left”, “right”, “top” and “bottom” using Swing Components.
Soln. :
import [Link].*;
import [Link];
import [Link];
public class BorderLayoutDemo extends JFrame
{
public BorderLayoutDemo( )
{
setLayout(new BorderLayout( ));
JButton bt1 = new JButton("TOP HEADER - NORTH");
add(bt1, [Link]);
add(btn3, [Link]);
String s = "This program shows another\n" + " way of creating and\n" + "adding components.\n";
JTextArea ta = new JTextArea(s);
add(ta, [Link]);
}
public static void main(String ar[ ])
{
BorderLayoutDemo fr = new BorderLayoutDemo( );
[Link](400, 400);
[Link]("Demonstrating BorderLayout");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
The output of above program will be :
Program 17 : Write program to design a calculator to demonstrate the use of grid layout using swingcomponents.
Soln. :
import [Link].*;
import [Link].*;
public class GridLayoutDemo extends JFrame
Java Programming L-35 Lab Manual
{
public GridLayoutDemo( )
{
Container ct = getContentPane( );
GridLayout gl = new GridLayout(4,3);
[Link](gl);
for(int i = 1;i <=9; i++)
{
[Link](new JButton("" + i));
}
JButton bt1 = new JButton("*");
JButton bt2 = new JButton("0");
JButton bt3 = new JButton("#");
[Link](bt1);
[Link](bt2);
[Link](bt3);
}
public static void main(String ar[ ])
{
GridLayoutDemo fr = new GridLayoutDemo( );
[Link](400, 400);
[Link]("Demonstrating GridLayout");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
The output of above program will be :
Java Programming L-36 Lab Manual
Soln. :
import [Link].*;
import [Link].*;
//aligning components
[Link](30,50,100,30);
[Link](150,50,100,30);
}
public static void main(String ar[ ])
{
JComboBoxDemo fr1 = new JComboBoxDemo( );
[Link]("Demonstrating combo box");
[Link](300,400);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
Java Programming L-37 Lab Manual
Soln. :
import [Link].*;
import [Link].*;
import [Link].*;
// Createsubtree of "A"
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
[Link](a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
[Link](a1);
Java Programming L-38 Lab Manual
// Createsubtree of "B"
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
[Link](b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
[Link](b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
[Link](b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
[Link](b3);
// Create tree
JTree tree = new JTree(root);
}
Java Programming L-39 Lab Manual
[Link].*;
[Link].*;
// Initialize data
final Object[ ][ ] data = {
{ "1", "Amit", "Ahmedabad", "559847" },
{ "2", "Ketan", "Rajkot", "756655" },
{ "3", "Vivek", "Junagadh", "563458" },
{ "4", "Mahendra", "Rajkot", "734592" },
{ "5", "Ankita", "Surat", "123733" },
{ "6", "Jayesh", "Baroda", "565614" },
Java Programming L-40 Lab Manual
Soln. :
import [Link].*;
import [Link].*;
class KeyEventDemo extends Frame implements KeyListener
{
TextField tf1, tf2;
public KeyEventDemo( )
{
setLayout(new FlowLayout( ));
add(tf1); add(tf2);
[Link](false);
[Link](this);
}
[Link](current_text);
}
public void keyTyped(KeyEvent ke)
{}
public void keyPressed(KeyEvent ke)
{}
}
The output of above program will be :
(Performing Addition when on mouse-enter event and clear TextFields on mouse-exit event)
import [Link].*;
import [Link].*;
class MouseEventDemo extends Frame implements MouseListener
{
TextField tf1, tf2, tf3;
Button btn;
public MouseEventDemo( )
{
setLayout(new FlowLayout( ));
Font f = new Font("Arial", [Link], 25);
[Link](f);
tf3 = new TextField(25);
[Link](f);
[Link](false);
btn = new Button("ADD");
[Link](f);
[Link]([Link]);
[Link](this);
}
public static void main(String ar[ ])
{
MouseEventDemo fr = new MouseEventDemo( );
[Link](400, 400);
[Link]("Demonstrating MouseEvent");
[Link](true);
}
public void mouseEntered(MouseEvent me)
{
[Link]([Link]);
int a = [Link]([Link]( ) );
int b = [Link]([Link]( ) );
int c = a + b;
[Link]("" + c);
}
public void mouseExited(MouseEvent me)
{
[Link]([Link]);
[Link](null);
[Link](null);
[Link](null);
}
public void mouseClicked(MouseEvent me)
{}
Java Programming L-44 Lab Manual
Soln. :
import [Link].*;
import [Link].*;
import [Link].*;
class ActionEventDemo extends JFrame
{
JTextField tf1, tf2, tf3;
JButton adbtn, subbtn, larbtn, smbtn;
public ActionEventDemo( )
{
setLayout(new FlowLayout( ));
Font f = new Font("Arial", [Link], 25);
Font f2 = new Font("Comic Sans MS", [Link], 20);
[Link](new Inner1( ) );
[Link](new Inner2( ) );
[Link](new Inner3( ) );
[Link](new Inner4( ) );
}
class Inner1 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int a = [Link]([Link]( ) );
int b = [Link]([Link]( ) );
int c = a + b;
[Link]("Addition is : " + c);
}
}
Java Programming L-46 Lab Manual
Soln. :
(TextEvent is not applicable on Swing’s JTextField and JTextArea. Instead we have to use DocumentListener)
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
class SwingTextEvent extends JFrame implements DocumentListener
{
JTextField tf1;
public SwingTextEvent( )
{
setLayout(new FlowLayout( ) );
tf1 = new JTextField(15);
[Link](new Font("Arial", [Link], 22) );
add(tf1);
[Link]( ).addDocumentListener(this);
}
}
The output of above program will be :
Java Programming L-49 Lab Manual
Soln. :
import [Link].*;
class InetAddressFactoryMethods
{
public static void main(String ar[ ]) throws UnknownHostException
{
InetAddress addr1 = [Link]( );
[Link]("Inet of LocalHost is : " + addr1);
import [Link].*;
import [Link].*;
Java Programming L-50 Lab Manual
class URLDemo
{
public static void main (String ar[ ]) throws IOException
{
URL url = new URL ("[Link]
[Link] ("Authority = " + [Link]( ));
[Link] ("File = " +[Link]( ));
[Link] ("Host = " +[Link]( ));
[Link] ("Path = " +[Link]( ));
[Link] ("Port = " +[Link]( ));
[Link] ("Protocol = " +[Link]( ));
[Link] ("Query = " +[Link]( ));
[Link] ("Ref = " +[Link]( ));
[Link] ("User Info = " +[Link]( ));
}
}
The output of above program will be :
import [Link].*;
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Java Programming L-51 Lab Manual
/*
Following code will print complete source code of ---> [Link]
{
[Link](i);
}
*/
}
catch (Exception e)
{
[Link](e);
}
}
}
The output of above program will be :
Soln. :
File 1 : [Link]
import [Link].*;
import [Link].*;
class ClientApplication
{
public static void main(String ar[ ]) throws IOException, UnknownHostException
{
[Link]("Client application Starts");
InetAddress i = [Link]( );
Socket client = new Socket(i, 100);
Java Programming L-53 Lab Manual
String s2 = [Link]( );
[Link]("Server Responded : " + s2);
[Link]( );
File 2 : [Link]
import [Link].*;
import [Link].*;
class ServerApplication
{
public static void main(String ar[ ]) throws IOException
{
[Link]("Server application Starts");
String s = [Link]( );
int num = [Link](s);
long f = 1;
for(int i=1; i<=num; i++)
{
f = f * i;
}
Java Programming L-54 Lab Manual
[Link]("" + f);
[Link]( );
[Link]( );
Program 26 : Write program to demonstrate sending and receiving data through datagram.
Soln. :
File 1 : [Link]
import [Link].*;
public class DataSender
Java Programming L-55 Lab Manual
{
public static void main(String ar[ ]) throws Exception
{
DatagramSocket ds = new DatagramSocket( );
String str = "Welcome java";
InetAddress ip = [Link]( );
DatagramPacket dp = new DatagramPacket([Link]( ), [Link]( ), ip, 3000);
[Link](dp);
[Link]( );
[Link]("Message Sent..");
}
}
File 2 : [Link]
import [Link].*;
public class DataReceiver
{
public static void main(String ar[ ]) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte buf[ ] = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
[Link](dp);
String str = new String([Link]( ), 0, [Link]( ));
[Link](str);
[Link]( );
}
}
The output of above program will be : (Run Sender application first, then run receiver application)
Java Programming L-56 Lab Manual
import [Link].*;
class DatabaseDemo
{
public static void main(String ar[ ])
{
try
{
// String query ="Write any insert / update / deleteQuery here”;
[Link]("[Link]");
//loading JDBC driver for Derby Database
Statement st = [Link]( );
// using interface Statement
// [Link](query);
// uncomment above statement to fire query
[Link]( );
//closing connection
[Link]("Connection Successful..!");
}
Java Programming L-57 Lab Manual
catch (Exception e)
{
[Link]("Got an exception! ");
[Link]([Link]( ));
}
}
}
Insert record.
Update record.
Delete record.
Soln. :
import [Link].*;
import [Link].*;
import [Link].*;
class DatabaseOperations extends Frame
{
TextField tf1, tf2, tf3, tf4;
Label lb5;
public DatabaseOperations( )
{
setLayout(null);
Font f1 = new Font("Arial", [Link], 21);
Font f2 = new Font("Times New Roman", [Link], 18);
Font f3 = new Font("Times New Roman", [Link], 25);
[Link](new Inner1( ) );
[Link](new Inner2( ) );
[Link](new Inner3( ) );
}
Java Programming L-59 Lab Manual
String s3 = [Link]( );
String s4 = [Link]( );
String q = "update emp_info set
emp_name='" + s1 + "', emp_post='" +
s2 + "', emp_dept='" + s4 + "' where emp_id=" + s1;
fireQuery(q, "Employee Record Updated..");
}
}
class Inner3 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
String s1 = [Link]( );
String q = "delete from emp_info where emp_id=" + s1;
fireQuery(q, "Employee Record Deleted");
}
}
public static void main(String ar[ ])
{
DatabaseOperations fr = new DatabaseOperations( );
[Link](730, 500);
[Link]("Demonstrating Database Operations");
[Link](true);
}
}
The output of above program will be :
Java Programming L-61 Lab Manual
Soln. :
import [Link].*;
import [Link].*;
import [Link].*;
class DatabaseOperations extends Frame
{
TextField tf1, tf2, tf3, tf4;
Label lb5;
public DatabaseOperations( )
{
setLayout(null);
Font f1 = new Font("Arial", [Link], 21);
Font f2 = new Font("Times New Roman", [Link], 18);
Font f3 = new Font("Times New Roman", [Link], 25);
[Link](new Inner1( ) );
[Link](new Inner2( ) );
[Link](new Inner3( ) );
}
String s3 = [Link]( );
String s4 = [Link]( );
try
{
String query = "insert into emp_info values(?, ?, ?, ?)";
[Link]("[Link]");
Connection con = [Link]
("jdbc:derby://localhost:1527/sample", "app", "app");
PreparedStatement psmt = [Link](query);
[Link](1,s1);
[Link](2,s2);
[Link](3,s3);
[Link](4,s4);
[Link]( );
[Link]( );
[Link]("Employee Record Inserted..");
}
catch (Exception e)
{
[Link]([Link]( ));
}
}
}
class Inner2 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int s1 = [Link]([Link]( ));
String s2 = [Link]( );
String s3 = [Link]( );
String s4 = [Link]( );
try
{
String query = "update emp_info set emp_name=?,
emp_post=?, emp_dept=? where emp_id=?";
Java Programming L-64 Lab Manual
[Link]("[Link]");
Connection con = [Link]
("jdbc:derby://localhost:1527/sample", "app", "app");
PreparedStatement psmt = [Link](query);
[Link](1,s2);
[Link](2,s3);
[Link](3,s4);
[Link](4,s1);
[Link]( );
[Link]( );
[Link]("Employee Record Updated..");
}
catch (Exception e)
{
[Link]([Link]( ));
}
}
}
class Inner3 implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
int s1 = [Link]([Link]( ));
try
{
String query = "delete from emp_info where emp_id=?";
[Link]("[Link]");
Connection con = [Link]
("jdbc:derby://localhost:1527/sample", "app", "app");
PreparedStatement psmt = [Link](query);
[Link](1,s1);
[Link]( );
[Link]( );
Java Programming L-65 Lab Manual
Program 30 : Write program to retrieve data from table using ResultSet interface.(Use various methods of navigation
methods).
Soln. :
Program 30.1 : Program to demonstrate SELECT query to fetch every record from Employee Table and
showing it in console.
import [Link].*;
public class SelectQueryDemo1
Java Programming L-66 Lab Manual
{
public static void main(String ar[])
{
String id, nm, pst, dep;
try
{
Statement st = [Link]( );
ResultSet rs = [Link](query);
while([Link]( ))
{
id = [Link]("emp_id");
nm = [Link]("emp_name");
pst = [Link]("emp_post");
dep = [Link]("emp_dept");
[Link]( );
catch(ClassNotFoundException cnfe)
catch(SQLException sqe)
[Link]([Link]( ));
}
}
Java Programming L-67 Lab Manual
Program 30.2 : Demonstrating SELECT query to fetch record of specific employee and filling it in TextField
objects.
import [Link].*;
import [Link].*;
import [Link].*;
public class SelectQueryDemo2 extends Frame implements ActionListener
{
TextField tf1, tf2, tf3, tf4;
Label lb5;
public SelectQueryDemo2( )
{
setLayout(null);
Font f1 = new Font("Arial", [Link], 21);
Font f2 = new Font("Times New Roman", [Link], 18);
Font f3 = new Font("Times New Roman", [Link], 25);
[Link](false);
[Link]([Link]("emp_post"));
[Link]([Link]("emp_dept"));
}
else
{
[Link]("Invalid Employee ID");
}
}
catch(ClassNotFoundException cnfe)
{
[Link]([Link]( ));
}
catch(SQLException sqe)
{
[Link]([Link]( ));
}
}
public static void main(String ar[ ])
{
SelectQueryDemo2 fr = new SelectQueryDemo2( );
[Link](730, 500);
[Link]("Demonstrating use of Select Query");
[Link](true);
}
}
The output of above program will be :