myfile Advanced Java
myfile Advanced Java
SONEPAT
PRACTICAL FILE
Advanced Java Programming
1|Page
INDEX
2|Page
15. Write a program to insert record 14-04-21 34
into the database using prepared
statement.
16. Write a program to extract 21-04-21 36
information about database and
table using metadata.
17. Write a program to use built in 21-04-21 38
beans using BDK.
18. Write a program to create and load 28-04-21 46
user defined java beans in BDK.
19. Write a program to create simple 28-04-21 48
servlet using generic class.
20. Write a program to create and 05-05-21 51
handle HTTP request and response.
21. Write a program to show JSP 12-05-21 54
expression and declaration in
scriplets.
22. Write a program to print welcome 19-05-21 56
message in JSP using implicit
objects.
3|Page
PROGRAM 1
Write a program to show the concept of classes and
objects.
int a = 4;
int b = 8;
int c = a*b;
int d = 1/2*(a*b);
int e = a*a;
OUTPUT:
4|Page
PROGRAM 2
Write a program to create default and parametrized
constructor.
public class Timetable
String AI;
String AJ;
String CS;
String CD;
String DM;
Timetable()
AI = a;
AJ = b;
CS = c;
CD = d;
DM = e;
5|Page
{
System.out.println("AI: "+o.AI);
System.out.println("AJ: "+o.AJ);
System.out.println("CS: "+o.CS);
System.out.println("CD: "+o.CD);
System.out.println("DM: "+o.DM);
System.out.println("\n");
System.out.println("\n");
System.out.println("\n");
6|Page
}
OUTPUT:
7|Page
PROGRAM 3
Write a program to show the concept of inheritance
with method overriding.
class Vivo
void print()
void print()
8|Page
Vivo h1 = new VivoY30();
h.print();
h1.print();
OUTPUT:
9|Page
PROGRAM 4
Write a program to implement and create interface
(show multiple inheritance).
interface F1
System.out.println("Department: CSE");
System.out.println("College: HCE");
System.out.println("Address: Najafgarh");
interface F2
System.out.println("\n");
System.out.println("Name: Chutki");
System.out.println("Department: ECE");
System.out.println("College: HCE");
System.out.println("Address: Sonipat");
interface F3
10 | P a g e
System.out.println("\n");
System.out.println("Name: Raju");
System.out.println("Department: CSE");
System.out.println("College: HCE");
System.out.println("Address: Ganaur");
interface F4
System.out.println("\n");
System.out.println("Name: Jaggu");
System.out.println("Department: CE");
System.out.println("College: HCE");
System.out.println("Address: Delhi");
F1.super.show();
F2.super.show();
F3.super.show();
F4.super.show();
11 | P a g e
{
d.show();
OUTPUT:
12 | P a g e
PROGRAM 5
Write a program to create user-defined package in
java.
Package:
package Laptop;
System.out.println("\n");
Import of package
import Laptop.*;
class L1
l.show();
13 | P a g e
OUTPUT:
14 | P a g e
PROGRAM 6
Write a program to get the input from the users using
util or io package.
import java.util.*;
import java.io.*;
class use_Of_util {
String str;
void input() {
str = scn.nextLine();
void print() {
System.out.println(str);
class use_Of_io {
String str;
void input() {
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
15 | P a g e
}
void print() {
System.out.println(str);
obj1.input();
obj1.print();
obj2.input();
obj2.print();
OUTPUT:
16 | P a g e
PROGRAM 7
Write a program to create and implement java.
import java.awt.*; // Abstract Window Toolkit
</applet> */
userName.setText("Username");
password.setText("Password");
add(userName);
add(userNameText);
add(password);
add(passwordText);
update(g);
17 | P a g e
OUTPUT:
18 | P a g e
PROGRAM 8
Write a program to create login form using Awt
controls.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
TextField t1,t2;
Label l1,l2,l3;
Button b1;
t1 = new TextField(20);
add(l1);
add(t1);
t2 = new TextField(20);
add(l2);
add(t2);
add(b1);
b1.addActionListener(this);
add(l3);
19 | P a g e
}
l3.setText("Login Success");
else{
l3.setText("Login Fail");
OUTPUT:
20 | P a g e
PROGRAM 9
Write a program to create Label, TextField, Button,
CheckBox, and RadioButton using swings.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
JTextField t1;
JButton b1;
JLabel l;
public swingDemo() {
t1 = new JTextField(15);
l = new JLabel("Greeting");
r1 = new JRadioButton("Male");
r2 = new JRadioButton("Female");
c1 = new JCheckBox("Dancer");
c2 = new JCheckBox("Singer");
b1 = new JButton("Submit");
bg.add(r1);
21 | P a g e
bg.add(r2);
add(t1);
add(r1);
add(r2);
add(c1);
add(c2);
add(b1);
add(l);
b1.addActionListener(this);
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setText(name);
22 | P a g e
OUTPUT:
23 | P a g e
PROGRAM 10
Write a program to create table and add scrollpane in
swings.
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
JTable t1;
TableWithScroll() {
Object[][] data = {{5, "Bheem", 15}, {10, "Arjun", 21}, {22, "Nakul", 45}, {14, "Nischal", 95}, {13,
"Nikhil", 101}};
t1 = new JTable(model);
add(scroll);
24 | P a g e
setLayout(new FlowLayout());
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OUTPUT:
25 | P a g e
PROGRAM 11
Write a program to create TabbedPane in swings.
import javax.swing.*;
import java.awt.*;
TabbedPane(){
tp.add("First", panel1);
tp.add("Second", panel2);
tp.add("Third", panel3);
add(tp);
setVisible(true);
setSize(400, 400);
26 | P a g e
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OUTPUT:
27 | P a g e
PROGRAM 12
Write a program to implements trees using swings.
import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;
TreeExample(){
A.add(A1);
A.add(A2);
B.add(B1);
28 | P a g e
B.add(B2);
B.add(B3);
rootNode.add(A);
rootNode.add(B);
add(tree);
// setLayout(new FlowLayout());
setVisible(true);
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OUTPUT:
29 | P a g e
PROGRAM 13
Write a program to create a list using swings in java.
import javax.swing.*;
import java.awt.*;
import javax.swing.JList;
String[] items = { "item1", "item2", "item3", "item4", "item5", "item6", "item7", "item8", "item9",
"item10",
"item11" };
JListExample() {
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED));
add(panel);
// setLayout(new FlowLayout());
setVisible(true);
30 | P a g e
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OUTPUT:
31 | P a g e
PROGRAM 14
Write a program to extract data from database using
JDBC.
import java.sql.*;
class MySQLcon{
try{
Class.forName("com.mysql.cj.jdbc.Driver");
"jdbc:mysql://localhost:3306/worker","root","nikhil3009@");
while(rs.next())
con.close();
catch(Exception e){
System.out.println(e);
32 | P a g e
OUTPUT:
33 | P a g e
PROGRAM 15
Write a program to insert record into the database
using prepared statement.
import java.sql.*;
Class.forName("com.mysql.cj.jdbc.Driver");
PreparedStatement st = con.prepareStatement(query);
st.setString(1, "Tom");
st.setString(2, "Khanna");
st.setString(3, "CSE");
st.setInt(4, 9500);
st.close();
con.close();
34 | P a g e
OUTPUT:
35 | P a g e
PROGRAM 16
Write a program to extract information about
database and table using matadata.
import java.sql.*;
class MySQLcon1{
try{
Class.forName("com.mysql.cj.jdbc.Driver");
"jdbc:mysql://localhost:3306/worker","root","nikhil3009@");
ResultSetMetaData rm = rs.getMetaData();
System.out.println(rm.getTableName(4));
int c = rm.getColumnCount();
while(rs.next())
con.close();
catch(Exception e){
System.out.println(e);
36 | P a g e
}
OUTPUT:
37 | P a g e
PROGRAM 17
Write a program to use built in beans using BDK
Example:
The easiest way to understand how BeanBox works is to use it. You can construct simple beans
applications without writing any Java code using BeanBox. As a first example, you can build a simple
"Juggling Duke" application in which Duke will start or stop juggling depending on which of two
buttons you push.
The first thing to do is add the Juggler Bean. Starting with an empty BeanBox select the Juggler Bean
from the list of Beans in the Toolbox window.
38 | P a g e
Now, placing the cursor anywhere in the BeanBox will cause a Juggler Bean to be inserted in the
design form. The highlighted box surrounding the Juggler indicates the Juggler is the currently
selected bean.
Next, you'll need to add a start button and a stop button to control the juggler. Each of these Beans is
an instance of the OurButton Bean class.
39 | P a g e
Place an instance of the button in the form.
To change the label on the button, edit the label field in the property sheet to read "start."
The text for the button changes at the same time you type it into the property sheet editor.
The next step is to specify an event to be fired by the button. A corresponding event-handler method
is then selected for the Juggler Bean.
Use the edit menu to select an action event to be fired by the start button. Note that in order for this
to work, the start button must be selected.
40 | P a g e
Once the actionPerformed menu selection is made, BeanBox enters a state where a line emanates
from the start button and follows the mouse as you move it around the window. This indicates that
the button is the selected source for the action event, and that your next mouse press should be over
the target which defines appropriate event-handler methods, in this case the Juggler.
When you drag the line from the start button and release it over the Juggler Bean, a dialog appears
listing applicable event handlers defined by the Juggler Bean.
41 | P a g e
Select the start method as the target for the event, then press OK. You can now press the start
button; Duke should start tossing beans around in a circle over his head like a professional juggler.
You can control Duke's juggling speed by manually setting the propery value labeled animationRate
in the Juggler's property sheet editor. For the appropriate property sheet editor to appear, the
Juggler must be the currently selected Bean within the BeanBox frame.
To complete the example program, you need to add a stop button. Again, from the list of available
beans in the Toolbox menu select OurButton.
42 | P a g e
Drag the button just below the start button, and drop it in place. Make sure the new button is the
currently selected Bean. If it is, the appropriate property editor appears to the right.
Edit the label field of the property sheet to read "start." The button's label reflects your changes as
you type.
43 | P a g e
Hook up the action event from the stop button to the Juggler's stop event-handler method. Make
sure the stop button is the currently selected bean. Select the actionPerformed event from the
Edit/Events menu.
Now drag the event line from the button source to the Juggler Bean target. Press and release the
mouse button with the connection line over the Juggler. You'll now see the dialog of applicable
event-handler methods defined for the Juggler Bean.
44 | P a g e
Select the stop event.
Now you're finished. You should be able to start and stop the juggler by pressing the appropriate
button.
45 | P a g e
PROGRAM – 18
Write a program to create and load user defined java
beans in BDK
Address: C:\Beans\demo\sunw\demo
• Create a Directory(folder) with the same name that you want to keep of your javabean to
distinguish better.
• Inside this Directory create a .java file of your javabean code and compile it using cmd
prompt and jdk to create .class file within the same directory.
• Now come outside of your javaBean Directory and navigate till below address:
C:\Beans\demo
• Here you have to create a manifest(.mft) file of your javaBean code using cmd prompt.
• Now again navigate outside the demo directory and navigate to jars directory located within
Beans and create .jar file of your javaBean code using cmd prompt.
• Now you are ready to use your JavaBean using the BeanBox.
Source code:
import javax.swing.*;
public class My extends JPanel
{
private String sname="My logo";
JLabel lname;
JTextField tname;
public CSE1()
{
lname=new JLabel(sname);
tname=new JTextField(10);
add(lname);
add(tname);
}
public void setSname(String str)
{
sname=str;
lname.setText(sname);
}
46 | P a g e
public String getSname()
{
return sname;
}
}
Output:
47 | P a g e
PROGRAM – 19
Write a program to create simple servlet using generic
class.
hello.java file:
package com.abhinav;
import java.io.*;
import javax.servlet.*;
index.html file:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
web.xml file:
48 | P a g e
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xm
l/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
<servlet>
<servlet-name>MyGenericServlet</servlet-name>
<servlet-class>com.abhinav.hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyGenericServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
Output:
49 | P a g e
50 | P a g e
PROGRAM – 20
Write a program to create and handle HTTP request
and response.
AddServlet.java file:
package com.abhinav;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
int i = Integer.parseInt(req.getParameter("num1"));
int j = Integer.parseInt(req.getParameter("num2"));
int k = i + j;
index.html file:
<!DOCTYPE html>
<html>
<body>
<form action="add">
</form>
</body>
</html>
51 | P a g e
web.xml file:
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>com.abhinav.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
</web-app>
Output:
52 | P a g e
53 | P a g e
PROGRAM – 21
Write a program to show JSP exp and declaration in
scriplets
add.jsp file:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>JSP</title>
</head>
<body>
<h3>Declaration of JSP</h3>
<%! int count =10; %>
<% out.println("The Number is " +count); %>
<h3>Expression of JSP</h3>
<% out.println("The expression number is "); %>
<% int num1=10; int num2=10; int num3 = 20; %>
<%= num1*num2+num3 %>
</body>
</html>
index.html file:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
54 | P a g e
Output:
55 | P a g e
PROGRAM – 22
Write a program to print welcome message in JSP
using implicit objects.
add.jsp file:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body bgcolor="cyan">
<%
%>
</body>
</html>
index.html file:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
56 | P a g e
Output:
57 | P a g e