0% found this document useful (0 votes)
7 views29 pages

Sem II Java 20-21

The document outlines a series of assignments for an Advanced Java Semester II course, focusing on various Java programming concepts including Swing components, JDBC, servlets, and JSP applications. Each assignment includes specific tasks such as designing user interfaces, implementing database connectivity, and handling user sessions. Code snippets and expected outputs are provided for practical implementation of the concepts taught in the course.

Uploaded by

ahajang141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

Sem II Java 20-21

The document outlines a series of assignments for an Advanced Java Semester II course, focusing on various Java programming concepts including Swing components, JDBC, servlets, and JSP applications. Each assignment includes specific tasks such as designing user interfaces, implementing database connectivity, and handling user sessions. Code snippets and expected outputs are provided for practical implementation of the concepts taught in the course.

Uploaded by

ahajang141
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Advanced Java Semester II

1. Program to design simple frame using swing components like JButton, JLabel,JTextField

2. Program to design simple frame using swing components like JButton,

JLabel,JTextField, JComboBox, JCheckBox

3. Programon JDBC

4. Program to design simple Login Page application using JDBC

5. Program on servlet

6. Program to maintain session

7. Program on cookies

8. Program on create simple JSP application to check given number is prime or not

9. Program on create simple JSP application to print Fibonacci sequence for given number

10. Program of JSP Application To Display System Date


Assignment no - 1

Assignment name - Program to design simple frame using swing components like
JButton, JLabel,JTextField

Roll no-

// Button with Action Listner


import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Java.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
Out put
Assignment no-2
Assignment name- Program to design simple frame using swing components like JButton,
JLabel,JTextField, JComboBox, JCheckBox
Roll no-

// Java program to implement


// a Simple Registration Form
// using Java Swing

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyFrame
extends JFrame
implements ActionListener {

// Components of the Form


private Container c;
private JLabel title;
private JLabel name;
private JTextField tname;
private JLabel mno;
private JTextField tmno;
private JLabel gender;
private JRadioButton male;
private JRadioButton female;
private ButtonGroup gengp;
private JLabel dob;
private JComboBox date;
private JComboBox month;
private JComboBox year;
private JLabel add;
private JTextArea tadd;
private JCheckBox term;
private JButton sub;
private JButton reset;
private JTextArea tout;
private JLabel res;
private JTextArea resadd;
private String dates[]
= { "1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",
"11", "12", "13", "14", "15",
"16", "17", "18", "19", "20",
"21", "22", "23", "24", "25",
"26", "27", "28", "29", "30",
"31" };
private String months[]
= { "Jan", "feb", "Mar", "Apr",
"May", "Jun", "July", "Aug",
"Sup", "Oct", "Nov", "Dec" };
private String years[]
= { "1995", "1996", "1997", "1998",
"1999", "2000", "2001", "2002",
"2003", "2004", "2005", "2006",
"2007", "2008", "2009", "2010",
"2011", "2012", "2013", "2014",
"2015", "2016", "2017", "2018",
"2019" };

// constructor, to initialize the components


// with default values.
public MyFrame()
{
setTitle("Registration Form");
setBounds(300, 90, 900, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);

c = getContentPane();
c.setLayout(null);

title = new JLabel("Registration Form");


title.setFont(new Font("Arial", Font.PLAIN, 30));
title.setSize(300, 30);
title.setLocation(300, 30);
c.add(title);

name = new JLabel("Name");


name.setFont(new Font("Arial", Font.PLAIN, 20));
name.setSize(100, 20);
name.setLocation(100, 100);
c.add(name);

tname = new JTextField();


tname.setFont(new Font("Arial", Font.PLAIN, 15));
tname.setSize(190, 20);
tname.setLocation(200, 100);
c.add(tname);

mno = new JLabel("Mobile");


mno.setFont(new Font("Arial", Font.PLAIN, 20));
mno.setSize(100, 20);
mno.setLocation(100, 150);
c.add(mno);

tmno = new JTextField();


tmno.setFont(new Font("Arial", Font.PLAIN, 15));
tmno.setSize(150, 20);
tmno.setLocation(200, 150);
c.add(tmno);

gender = new JLabel("Gender");


gender.setFont(new Font("Arial", Font.PLAIN, 20));
gender.setSize(100, 20);
gender.setLocation(100, 200);
c.add(gender);

male = new JRadioButton("Male");


male.setFont(new Font("Arial", Font.PLAIN, 15));
male.setSelected(true);
male.setSize(75, 20);
male.setLocation(200, 200);
c.add(male);

female = new JRadioButton("Female");


female.setFont(new Font("Arial", Font.PLAIN, 15));
female.setSelected(false);
female.setSize(80, 20);
female.setLocation(275, 200);
c.add(female);
gengp = new ButtonGroup();
gengp.add(male);
gengp.add(female);

dob = new JLabel("DOB");


dob.setFont(new Font("Arial", Font.PLAIN, 20));
dob.setSize(100, 20);
dob.setLocation(100, 250);
c.add(dob);

date = new JComboBox(dates);


date.setFont(new Font("Arial", Font.PLAIN, 15));
date.setSize(50, 20);
date.setLocation(200, 250);
c.add(date);

month = new JComboBox(months);


month.setFont(new Font("Arial", Font.PLAIN, 15));
month.setSize(60, 20);
month.setLocation(250, 250);
c.add(month);

year = new JComboBox(years);


year.setFont(new Font("Arial", Font.PLAIN, 15));
year.setSize(60, 20);
year.setLocation(320, 250);
c.add(year);

add = new JLabel("Address");


add.setFont(new Font("Arial", Font.PLAIN, 20));
add.setSize(100, 20);
add.setLocation(100, 300);
c.add(add);

tadd = new JTextArea();


tadd.setFont(new Font("Arial", Font.PLAIN, 15));
tadd.setSize(200, 75);
tadd.setLocation(200, 300);
tadd.setLineWrap(true);
c.add(tadd);
term = new JCheckBox("Accept Terms And Conditions.");
term.setFont(new Font("Arial", Font.PLAIN, 15));
term.setSize(250, 20);
term.setLocation(150, 400);
c.add(term);

sub = new JButton("Submit");


sub.setFont(new Font("Arial", Font.PLAIN, 15));
sub.setSize(100, 20);
sub.setLocation(150, 450);
sub.addActionListener(this);
c.add(sub);

reset = new JButton("Reset");


reset.setFont(new Font("Arial", Font.PLAIN, 15));
reset.setSize(100, 20);
reset.setLocation(270, 450);
reset.addActionListener(this);
c.add(reset);

tout = new JTextArea();


tout.setFont(new Font("Arial", Font.PLAIN, 15));
tout.setSize(300, 400);
tout.setLocation(500, 100);
tout.setLineWrap(true);
tout.setEditable(false);
c.add(tout);

res = new JLabel("");


res.setFont(new Font("Arial", Font.PLAIN, 20));
res.setSize(500, 25);
res.setLocation(100, 500);
c.add(res);

resadd = new JTextArea();


resadd.setFont(new Font("Arial", Font.PLAIN, 15));
resadd.setSize(200, 75);
resadd.setLocation(580, 175);
resadd.setLineWrap(true);
c.add(resadd);
setVisible(true);
}

// method actionPerformed()
// to get the action performed
// by the user and act accordingly
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sub) {
if (term.isSelected()) {
String data1;
String data
= "Name : "
+ tname.getText() + "\n"
+ "Mobile : "
+ tmno.getText() + "\n";
if (male.isSelected())
data1 = "Gender : Male"
+ "\n";
else
data1 = "Gender : Female"
+ "\n";
String data2
= "DOB : "
+ (String)date.getSelectedItem()
+ "/" + (String)month.getSelectedItem()
+ "/" + (String)year.getSelectedItem()
+ "\n";

String data3 = "Address : " + tadd.getText();


tout.setText(data + data1 + data2 + data3);
tout.setEditable(false);
res.setText("Registration Successfully..");
}
else {
tout.setText("");
resadd.setText("");
res.setText("Please accept the"
+ " terms & conditions..");
}
}

else if (e.getSource() == reset) {


String def = "";
tname.setText(def);
tadd.setText(def);
tmno.setText(def);
res.setText(def);
tout.setText(def);
term.setSelected(false);
date.setSelectedIndex(0);
month.setSelectedIndex(0);
year.setSelectedIndex(0);
resadd.setText(def);
}
}
}
// Driver Code
class Registration {

public static void main(String[] args) throws Exception


{
MyFrame f = new MyFrame();
}
}
Out put
Assignment no-3

Assignment name- Program on JDBC

Roll no-

import java.sql.*;

class MysqlCon{

public static void main(String args[]){

try{

Class.forName("com.mysql.jdbc.Driver");

Connection con=DriverManager.getConnection(

"jdbc:mysql://localhost:3306/sonoo","root","");

//here sonoo is database name, root is username and password

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from emp");

while(rs.next())

System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));

con.close();

}catch(Exception e){ System.out.println(e);}

Out put

C:\jdk1.4\bin>java conn

1 ravi Phaltan

2 ram pune

C:\jdk1.4\bin>
Assignment no-4

Assignment name- . Program to design simple Login Page application using JDBC

Roll no-

Database Setup

create database swing_demo;

CREATE TABLE student

( id int NOT NULL,

name varchar(250) NOT NULL,

password varchar(250)

);

INSERT INTO student (id, name, password)

VALUES (1, 'Ramesh', 'Ramesh@1234');

Java Program

import java.awt.Color;

import java.awt.EventQueue;

import java.awt.Font;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;
import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;

import javax.swing.border.EmptyBorder;

public class UserLogin extends JFrame {

private static final long serialVersionUID = 1L;

private JTextField textField;

private JPasswordField passwordField;

private JButton btnNewButton;

private JLabel label;

private JPanel contentPane;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {
UserLogin frame = new UserLogin();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

});

/**

* Create the frame.

*/

public UserLogin() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(450, 190, 1014, 597);

setResizable(false);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(null);

JLabel lblNewLabel = new JLabel("Login");

lblNewLabel.setForeground(Color.BLACK);

lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 46));

lblNewLabel.setBounds(423, 13, 273, 93);

contentPane.add(lblNewLabel);
textField = new JTextField();

textField.setFont(new Font("Tahoma", Font.PLAIN, 32));

textField.setBounds(481, 170, 281, 68);

contentPane.add(textField);

textField.setColumns(10);

passwordField = new JPasswordField();

passwordField.setFont(new Font("Tahoma", Font.PLAIN, 32));

passwordField.setBounds(481, 286, 281, 68);

contentPane.add(passwordField);

JLabel lblUsername = new JLabel("Username");

lblUsername.setBackground(Color.BLACK);

lblUsername.setForeground(Color.BLACK);

lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 31));

lblUsername.setBounds(250, 166, 193, 52);

contentPane.add(lblUsername);

JLabel lblPassword = new JLabel("Password");

lblPassword.setForeground(Color.BLACK);

lblPassword.setBackground(Color.CYAN);

lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 31));

lblPassword.setBounds(250, 286, 193, 52);

contentPane.add(lblPassword);
btnNewButton = new JButton("Login");

btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 26));

btnNewButton.setBounds(545, 392, 162, 73);

btnNewButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String userName = textField.getText();

String password = passwordField.getText();

try {

Connection connection = (Connection)


DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo",

"root", "root");

PreparedStatement st = (PreparedStatement) connection

.prepareStatement("Select name, password from student where name=? and


password=?");

st.setString(1, userName);

st.setString(2, password);

ResultSet rs = st.executeQuery();

if (rs.next()) {

dispose();

UserHome ah = new UserHome(userName);

ah.setTitle("Welcome");

ah.setVisible(true);

JOptionPane.showMessageDialog(btnNewButton, "You have successfully


logged in");
} else {

JOptionPane.showMessageDialog(btnNewButton, "Wrong Username &


Password");

} catch (SQLException sqlException) {

sqlException.printStackTrace();

});

contentPane.add(btnNewButton);

label = new JLabel("");

label.setBounds(0, 0, 1008, 562);

contentPane.add(label);

OUT PUT
Assignment no-5

Assignment name- Program on servlet

Roll no-

import javax.servlet.http.*;

import javax.servlet.*;

import java.io.*;

public class DemoServlet extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException,IOException

res.setContentType("text/html");//setting the content type

PrintWriter pw=res.getWriter();//get the stream to write the data

//writing html in the stream

pw.println("<html><body>");

pw.println("Welcome to servlet");

pw.println("</body></html>");

pw.close();//closing the stream

}}

Output
Assignment no-6

Assignment name- Program to maintain session

Roll no-

import java.io.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.net.*;

public class Dataservlet extends HttpServlet

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)

throws ServletException, IOException {

HttpSession hs=request.getSession(true);

response.setContentType("text/html");

PrintWriter pw=response.getWriter();

pw.print("<B>");

Date dt=(Date)hs.getAttribute("date");

if(dt!=null)

{ pw.print("Last Access:"+dt+"<br>"); }

dt=new Date();

hs.setAttribute("date",dt);

pw.println("current date="+dt);

pw.close();

}
protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{ processRequest(request, response); }

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

public String getServletInfo() {

return "Short description";

Out Put
Assignment no-7

Assignment name- Program on cookies

Roll no-

index.html

<form action="servlet1" method="post">

Name:<input type="text" name="userName"/><br/>

<input type="submit" value="go"/>

</form>

FirstServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response){

try{

response.setContentType("text/html");

PrintWriter out = response.getWriter();

String n=request.getParameter("userName");

out.print("Welcome "+n);

Cookie ck=new Cookie("uname",n);//creating cookie object

response.addCookie(ck);//adding cookie in the response


//creating submit button

out.print("<form action='servlet2'>");

out.print("<input type='submit' value='go'>");

out.print("</form>");

out.close();

}catch(Exception e){System.out.println(e);}

SecondServlet.java

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class SecondServlet extends HttpServlet {

public void doPost(HttpServletRequest request, HttpServletResponse response){

try{

response.setContentType("text/html");

PrintWriter out = response.getWriter();


Cookie ck[]=request.getCookies();

out.print("Hello "+ck[0].getValue());

out.close();

}catch(Exception e){System.out.println(e);}

web.xml

<web-app>

<servlet>

<servlet-name>s1</servlet-name>

<servlet-class>FirstServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s1</servlet-name>

<url-pattern>/servlet1</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>s2</servlet-name>

<servlet-class>SecondServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>

</servlet-mapping>

</web-app>

OUTPUT
Assignment no-8

Assignment name- Program on create simple JSP application to check given number is
prime or not

Roll no-

Index.jsp

html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body><br><br><center>

<form action="primejsp.jsp ">

<h1>Enter the no :: <input type=text name=n ><br><br>

<input type=submit value="Submit"></h1>

</form></center>

</body>

</html>

Prime.jsp

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body><center><h1>The required Result is:: </h1>


<h2>

<%

int n,i,flag=0;

String ns= request.getParameter("n");

n=Integer.parseInt(ns);

if(n>1)

for(i=2;i<=n/2;i++)

if(n%i==0)

flag=1;

break;

if(flag==0)

out.println("<pre>");

out.println(n+" is a prime no.");

out.println("</pre>");

else

{
out.println("<pre>");

out.println(n+" is not a prime no.");

out.println("</pre>");

%>

</h2></center>

</body>

</html>

Out put
Assignment no-9

Assignment name- Program on create simple JSP application to print Fibonacci sequence
for given number

Roll no-

input.html

<html>
<body>
<form action="Fibonacci.jsp">
Enter a value for n: <input type="text" name="val">
<input type="submit" value="Submit">
</form>
</body>
</html>

Fibonacci.jsp

<html>
<body>
<%!
int n;
String str;

int fibo(int n) {
if(n<2)
return n;
else
return fibo(n-1) + fibo(n-2);
}
%>
<b>Fibonacci series: </b><br>
<%
str = request.getParameter("val");
n = Integer.parseInt(str);
for(int i=0; i<=n; i++) {
out.print(fibo(i) + " ");
}
%>
</body>
</html>
Output:

After clicking the “Submit” button we get the following response:


Assignment no-10

Assignment name- Program of JSP Application To Display System Date .

Roll no-

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1>Date & Time is</h1>

<%

Date date = new Date();

out.print( "<h2 align=\"center\">" +date.toString()+"</h2>");

%>

</body>

</html>

Out Put

You might also like