0% found this document useful (0 votes)
46 views94 pages

Java

The document contains code snippets for several Java programs that demonstrate different concepts: 1. A program that generates a random integer using the Random class. 2. A program that prints the Fibonacci series for a given number of terms. 3. A program that checks if a given number is a palindrome. 4. A program that reverses a given string. 5. A program that calculates the factorial of a given number.

Uploaded by

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

Java

The document contains code snippets for several Java programs that demonstrate different concepts: 1. A program that generates a random integer using the Random class. 2. A program that prints the Fibonacci series for a given number of terms. 3. A program that checks if a given number is a palindrome. 4. A program that reverses a given string. 5. A program that calculates the factorial of a given number.

Uploaded by

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

1

Page

SECTION:C2 A22205316002
Program to generate random number using random class
import java.util.Random;
public class Randoms
{
public static void main(String args[])
{ Random rand = new Random();
int rand_int1 = rand.nextInt(1000);
System.out.println("Random Integers: "+rand_int1);
}}

2
Page

SECTION:C2 A22205316002
3
Page

SECTION:C2 A22205316002
Program to print Fibonacci series for a given series.

import java.util.*;
public class Fibo
{ public static void main(String args[])
{ int n1=1,n2=1,n3;
Scanner s = new Scanner(System.in);
int n = s.nextInt();
System.out.print(n1+" "+n2);
for(int i=2;i<n;i++)
{ n3 = n1 + n2;
n1=n2;
n2=n3;
System.out.print(" "+n3);
}
}
}

4
Page

SECTION:C2 A22205316002
5
Page

SECTION:C2 A22205316002
Program to check wheather a given number is palindrome.
import java.util.*;

class recurs
{

static int rev(int n, int temp)


{
if (n == 0)
return temp;

temp = (temp * 10) + (n % 10);

return rev(n / 10, temp);


}

public static void main (String[] args)


{ Scanner s = new Scanner(System.in);
int n = s.nextInt();
int temp = rev(n, 0);

if (temp == n)
System.out.println("yes");
else
System.out.println("no" );
}
6
Page

SECTION:C2 A22205316002
7
Page

SECTION:C2 A22205316002
Program to reverse a given string.

import java.lang.*;

import java.io.*;

import java.util.*;

class Rev

public static void main(String[] args)

String input = "padmagopal";

StringBuilder input1 = new StringBuilder();

input1.append(input);

input1 = input1.reverse();

System.out.println(input1);

}
8
Page

SECTION:C2 A22205316002
9
Page

SECTION:C2 A22205316002
Program to print Factorial of a given number.

import java.util.*;
class Fact
{
static int Factorial(int n)
{if(n==0)
return 1;
else
return (n*Factorial(n-1));

}
public static void main(String args[])
{ Scanner s = new Scanner(System.in);
int n = s.nextInt();
int fact = Factorial(n);
System.out.println(fact);
}}

10
Page

SECTION:C2 A22205316002
11
Page

SECTION:C2 A22205316002
Program to print Login using awt components and events.

import java.awt.*;
import java.awt.event.*;

class Welcome extends Frame


{
Welcome(String s1)
{
Label l1 =new Label();
setSize(250,250);
l1.setBounds(120,120,70,20);
l1.setText(" Welcome "+s1);
add(l1);
}
}

class Login extends Frame


{
Label l1 ;
Label l2;
TextField un;
TextField pas;
Button b1;
Button b2;
Welcome w;
Login()
{
12

l1=new Label("username : ");


Page

l2=new Label("password : ");

SECTION:C2 A22205316002
un=new TextField();
pas=new TextField();
b1=new Button("OK");
b2=new Button("CANCEL");

pas.setEchoChar('$');
l1.setBounds(50,50,80,20);
l2.setBounds(50,90,80,20);
un.setBounds(150,50,80,20);
pas.setBounds(150,90,80,20);
b1.setBounds(70,150,70,20);
b2.setBounds(170,150,70,20);

add(l1);
add(l2);
add(b1);
add(b2);
add(un);
add(pas);
setLayout(null);
setVisible(true);
setSize(500,500);

b1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
13

{
Page

if(pas.getText().equals("123"))

SECTION:C2 A22205316002
{
setVisible(false);
w=new Welcome(un.getText());
w.setVisible(true);

}
}
}
);

b2.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
}
);

public static void main(String args[])


{

new Login();
}
14

}
Page

SECTION:C2 A22205316002
15
Page

SECTION:C2 A22205316002
Program to change the color of foreground and background
using awt components.

import java.awt.*;
import java.awt.event.*;
public class color extends Frame
{
Label l1;
Checkbox c1,c2;
//CheckboxGroup cbg;

color()
{super("color");
l1 = new Label("helloworld");
c1 = new Checkbox("background");
c2 = new Checkbox("foreground");

l1.setBounds(50,100,100,100);
c1.setBounds(50,150,100,100);
c2.setBounds(50,200,100,100);

add(l1);
add(c1);
add(c2);

c1.addItemListener(new ItemListener()
{ public void itemStateChanged( ItemEvent ae)
{
16

setBackground(Color.red);
Page

SECTION:C2 A22205316002
});

c2.addItemListener(new ItemListener()
{ public void itemStateChanged( ItemEvent ae)
{
l1.setForeground(Color.blue);
}
});

setLayout(null);
setSize(250,250);

setVisible(true);
}
public static void main(String args[])
{
new color();
}}

17
Page

SECTION:C2 A22205316002
18
Page

SECTION:C2 A22205316002
Program to change the font using awt components and events.

import java.awt.*;
import java.awt.event.*;
//import java.awt.event.WindowEvent;
//import java.awt.event.WindowListener;
class font extends Frame
{ Frame f = new Frame();
Label l1;
Checkbox c1,c2,c3,c4;
CheckboxGroup cbg;
font()
{
super("change font");
l1 = new Label("helloworld");
c1 = new Checkbox("bold",cbg,false);
c2 = new Checkbox("italic",cbg,false);
c3 = new Checkbox("fontitalic",cbg,false);
c4 = new Checkbox("normal",cbg,true);
l1.setBounds(50,100,100,100);
c1.setBounds(50,150,100,100);
c2.setBounds(150,150,100,100);
c3.setBounds(250,150,100,100);
c4.setBounds(350,150,100,100);

add(l1);
add(c1);
19

add(c2);
Page

add(c3);

SECTION:C2 A22205316002
add(c4);

c1.addItemListener(new ItemListener()
{public void itemStateChanged(ItemEvent ae)
{ c2.setState(false);
c4.setState(false);
c3.setState(false);
l1.setFont(new Font("Courier New", Font.BOLD, 12));}
});
c2.addItemListener(new ItemListener()
{public void itemStateChanged(ItemEvent ae)
{c1.setState(false);
c4.setState(false);
c3.setState(false);
l1.setFont(new Font("Courier New", Font.ITALIC, 12));}
});
c3.addItemListener(new ItemListener()
{public void itemStateChanged(ItemEvent ae)
{c1.setState(true);
c2.setState(true); }

});
c4.addItemListener(new ItemListener()
{public void itemStateChanged(ItemEvent ae)
{c1.setState(false);
c2.setState(false);
c3.setState(false); }
});
20

addWindowListener(new WindowAdapter(){
Page

public void windowClosing(WindowEvent e) {

SECTION:C2 A22205316002
dispose();
}
});
setLayout(null);
setSize(250,250);

setVisible(true);
}
public static void main(String args[])
{
new font();
}
}

21
Page

SECTION:C2 A22205316002
22
Page

SECTION:C2 A22205316002
Program to implement JLabel on jframe.

import javax.swing.*;
class Labels
{
public static void main(String args[])
{
JFrame f= new JFrame("padmagopal");
JLabel l1,l2;
l1=new JLabel("padmagopal");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Btech cse.");
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}

23
Page

SECTION:C2 A22205316002
24
Page

SECTION:C2 A22205316002
Program to show message dialog box using JOptionPane.
import javax.swing.*;
public class Op {
JFrame f;
Op(){
f=new JFrame();
JOptionPane.showMessageDialog(f,"Hello, welcome padmagoapl.");
}
public static void main(String[] args) {
new Op();
}
}

25
Page

SECTION:C2 A22205316002
26
Page

SECTION:C2 A22205316002
Program to implement JButton in Jframe
import javax.swing.*;
public class Buttons {
public static void main(String[] args) {
JFrame f=new JFrame("PADMAGOpal");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

27
Page

SECTION:C2 A22205316002
28
Page

SECTION:C2 A22205316002
Program to implement JTextField in Jframe

import javax.swing.*;
class Texts
{
public static void main(String args[])
{
JFrame f= new JFrame("PADMAGOPAL");
JTextField t1,t2;
t1=new JTextField("pADMAGOAPL.");
t1.setBounds(50,100, 200,30);
t2=new JTextField("CSE");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

29
Page

SECTION:C2 A22205316002
30
Page

SECTION:C2 A22205316002
Program to implement JTextArea in Jframe
import javax.swing.*;
public class TextAreas{
TextAreas(){
JFrame f= new JFrame();
JTextArea area=new JTextArea("GOPAL");
area.setBounds(10,30, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreas();
}}

31
Page

SECTION:C2 A22205316002
32
Page

SECTION:C2 A22205316002
Program to implement JTextArea in Jframe
import javax.swing.*;
public class Passwords {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(100,100,100,30);
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}

33
Page

SECTION:C2 A22205316002
34
Page

SECTION:C2 A22205316002
Program to implement JCheckBox in Jframe
import javax.swing.*;
public class Checks {
Checks(){
JFrame f= new JFrame("PADMAGOPAL");
JCheckBox checkBox1 = new JCheckBox("TN");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("KK", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new Checks();
}}

35
Page

SECTION:C2 A22205316002
36
Page

SECTION:C2 A22205316002
Program to implement JRadioButton in Jframe
import javax.swing.*;
public class Radios {
JFrame f;
Radios(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) PADMAGOAPL ");
JRadioButton r2=new JRadioButton("B) MSD ");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new Radios();
}
}

37
Page

SECTION:C2 A22205316002
38
Page

SECTION:C2 A22205316002
Program to implement JComboBox in Jframe
import javax.swing.*;
public class Combos {
JFrame f;
Combos(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args) {
new Combos();
}
}

39
Page

SECTION:C2 A22205316002
40
Page

SECTION:C2 A22205316002
Program to implement JList in Jframe

import javax.swing.*;
public class Lis
{
Lis(){
JFrame f= new JFrame();
DefaultListModel<String> l1 = new DefaultListModel<>();
l1.addElement("cse");
l1.addElement("mech");
l1.addElement("chem");
l1.addElement("civul"); a
JList<String> list = new JList<>(l1);
list.setBounds(100,100, 75,75);
f.add(list);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new Lis();
}}
41
Page

SECTION:C2 A22205316002
42
Page

SECTION:C2 A22205316002
Program to implement JSpinner in Jframe
import javax.swing.*;
public class SpinnerExample {
public static void main(String[] args) {
JFrame f=new JFrame("Spinner Example");
SpinnerModel value =
new SpinnerNumberModel(5, //initial value
0, //minimum value
10, //maximum value
1); //step
JSpinner spinner = new JSpinner(value);
spinner.setBounds(100,100,50,30);
f.add(spinner);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}

43
Page

SECTION:C2 A22205316002
44
Page

SECTION:C2 A22205316002
Program to implement confirm dialog box in Jframe
import javax.swing.JOptionPane;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class JOs{


public static void main(String[] args) {
JDialog.setDefaultLookAndFeelDecorated(true);
int response = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
System.out.println("No button clicked");
} else if (response == JOptionPane.YES_OPTION) {
System.out.println("Yes button clicked");
} else if (response == JOptionPane.CLOSED_OPTION) {
System.out.println("JOptionPane closed");
}
}
}

45
Page

SECTION:C2 A22205316002
46
Page

SECTION:C2 A22205316002
Program to implement Input dialog box in Jframe
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Inputs {

public static void main(String[] a) {


JFrame frame = new JFrame();
Object result = JOptionPane.showInputDialog(frame, "Enter printer name:");

System.out.println(result);
}

47
Page

SECTION:C2 A22205316002
48
Page

SECTION:C2 A22205316002
Program to fetch record from database and print it.
import java.sql.*;
public class dbs
{
public static void main(String args[])
{ try{
Connection con;
Statement st;
ResultSet rs;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/access1.accdb");
System.out.println("test");
st=con.createStatement();
rs=st.executeQuery("select * from info");
while(rs.next())
{ System.out.println(rs.getString(1)+" , "+rs.getString(2));
}
con.close();
}
catch(Exception e)
{ System.out.println(e);
}
}
}
49
Page

SECTION:C2 A22205316002
50
Page

SECTION:C2 A22205316002
Program to add and fetch record of STATE,CITY AND VILLAGE
from database and print it.
//program to add state
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class ins extends Frame
{
Label l1 ;
TextField un;
Button b1;
ins() {
l1=new Label("state: ");
un=new TextField();
b1=new Button("OK");
l1.setBounds(50,50,80,20);
un.setBounds(150,50,80,20);
b1.setBounds(70,150,70,20);
add(l1);
add(b1);
add(b2);
add(un);
add(pas);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
}
});
51

b1.addActionListener
Page

SECTION:C2 A22205316002
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Connection con;
Statement st;
ResultSet rs;

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/state.accd
b");
System.out.println("test");
String s = un.getText();
st=con.createStatement();
st.executeUpdate("Insert into state (SNAME) values( '"+s+"')");
System.out.print("insered sucessfully;");
con.close();
}
catch(Exception f)
{
System.out.println(f);
}
}
}
);
52
Page

SECTION:C2 A22205316002
setLayout(null);
setVisible(true);
setSize(500,500);
}
public static void main(String args[])
{
new ins();
}
}
//program to add city and fetch village
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class insc extends Frame
{
Choice c1;
TextField t1;
Button b1;
insc()
{
c1 = new Choice();
t1 = new TextField();
b1 = new Button("add");

c1.setBounds(50,100,80,20);
t1.setBounds(150,100,80,20);
53

b1.setBounds(130,150,80,20);
Page

SECTION:C2 A22205316002
add(c1);
add(t1);
add(b1);
try
{
Connection con;
Statement st;
ResultSet rs;

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");
con=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/state.accdb");
st=con.createStatement();
rs=st.executeQuery("select SNAME from state");
System.out.print("test");
while(rs.next())
{
c1.add(rs.getString("SNAME"));
}
con.close();
}
catch(Exception f)
{
System.out.println(f);
}

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
54

dispose();
Page

SECTION:C2 A22205316002
});

setLayout(null);
setVisible(true);
setSize(500,500);
b1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try { Connection conc;
Statement stc;

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");

conc=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/state.accdb");
stc=conc.createStatement();

String sc = t1.getText();
String ss = c1.getItem(c1.getSelectedIndex());
stc=conc.createStatement();
stc.executeUpdate("Insert into city (SNAME,CNAME) values( '"+ss+"','"+sc+"')");
System.out.println("sucessfully");
}
catch(Exception e)
{
55

System.out.print(e);
Page

SECTION:C2 A22205316002
}
}
}
);

}
public static void main(String args[])
{
new insc();
}
}
//Program to add village and fetch city and country
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class insv extends Frame implements ItemListener
{
Choice c1;
Choice c2;
TextField t1;
Button b1;
insv()
{
c1 = new Choice();
c2 = new Choice();
t1 = new TextField();
56

b1 = new Button("add");
Page

SECTION:C2 A22205316002
c1.setBounds(50,100,80,20);
c2.setBounds(150,100,80,20);
t1.setBounds(130,150,80,20);
b1.setBounds(130,250,80,20);

add(c1);
add(c2);
add(t1);
add(b1);
c1.addItemListener(this);
try
{
Connection con;
Statement st,st2;
ResultSet rs,rs2;

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");
con=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/state.accdb");
st=con.createStatement();
rs=st.executeQuery("select SNAME from state");

System.out.print("test");
while(rs.next())
{
c1.add(rs.getString("SNAME"));
}
con.close();
57

}
Page

catch(Exception f)

SECTION:C2 A22205316002
{
System.out.println(f);
}

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
}
});

setLayout(null);
setVisible(true);
setSize(500,500);
b1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
try {
Connection conc;
Statement stc;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");

conc=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/state.accdb");
stc=conc.createStatement();

String sc = c2.getItem(c2.getSelectedIndex());
58

String ss = c1.getItem(c1.getSelectedIndex());
Page

SECTION:C2 A22205316002
String sv = t1.getText();
stc=conc.createStatement();
stc.executeUpdate("Insert into village (SNAME,CNAME,VNAME) values(
'"+ss+"','"+sc+"','"+sv+"')");
System.out.println("sucessfully");
t1.setText(" ");
t1.requestFocus();
}
catch(Exception e)
{
System.out.print(e);
}
}
}
);

}
public void itemStateChanged(ItemEvent ae)
{ c2.removeAll();
String s = c1.getSelectedItem();
try{
Connection con2;
Statement st2;
ResultSet rs2;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
59

System.out.println("test");
Page

SECTION:C2 A22205316002
con2=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/state.accdb");
st2 = con2.createStatement();
rs2 = st2.executeQuery("select CNAME from city where SNAME ='"+s+"'");
System.out.print("city sucess");
while(rs2.next())
{
c2.add(rs2.getString("CNAME"));
}
}
catch(Exception e)
{
System.out.print(e);
}

}
public static void main(String args[])
{
new insv();
}
}

60
Page

SECTION:C2 A22205316002
61
Page

SECTION:C2 A22205316002
Program to add and fetch the max roll no of the student from
database.
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class student extends Frame{

Label l1 ;
Label l2;
TextField un;
TextField pas;
Button b1;
Button b2;
Welcome w;
student()
{
l1=new Label("Rollno : ");
l2=new Label("student name : ");
un=new TextField();
pas=new TextField();
b1=new Button("OK");
b2=new Button("CANCEL");

l1.setBounds(50,50,80,20);
l2.setBounds(50,90,80,20);
un.setBounds(150,50,80,20);
62

pas.setBounds(150,90,80,20);
Page

b1.setBounds(70,150,70,20);

SECTION:C2 A22205316002
b2.setBounds(170,150,70,20);

add(l1);
add(l2);
add(b1);
add(b2);
add(un);
add(pas);

try{
Connection conm;
Statement stm;
ResultSet rsm;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");

conm=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/student.accdb");
stm=conm.createStatement();
rsm=stm.executeQuery("select MAX(R_no) from student") ;
while(rsm.next())
{
String rollm = rsm.getString(1);

un.setText(rollm);

}}
63

catch(Exception j)
Page

SECTION:C2 A22205316002
{
System.out.print(j);
}
setLayout(null);
setVisible(true);
setSize(500,500);
b1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
Connection con;
Statement st,st2;
ResultSet rs;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/student.ac
cdb");
System.out.println("test");
st=con.createStatement();
String s1 = un.getText();
String s2 = pas.getText();

st.executeUpdate("Insert into student(R_no,SNAME) values('"+s1+"','"+s2+"')");


64

st2=con.createStatement();
Page

rs=st.executeQuery("select MAX(R_no) from student") ;

SECTION:C2 A22205316002
while(rs.next())
{
String roll = rs.getString(1);
Integer i = Integer.parseInt(roll);
i++;
String f = i.toString(i);
un.setText(f);
pas.setText("");

un.requestFocus();
}

con.close();
}
catch(Exception f)
{
System.out.println(f);
}
}

}
);
65
Page

SECTION:C2 A22205316002
}

public static void main(String args[])


{
new student();
}}

66
Page

SECTION:C2 A22205316002
67
Page

SECTION:C2 A22205316002
Program to implement login validation using awt and database
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Welcome extends Frame
{
Welcome(String s1)
{
Label l1 =new Label();
setSize(250,250);
l1.setBounds(120,120,70,20);
l1.setText(" Welcome "+s1);
add(l1);
}
}
public class logindb extends Frame{

Label l1 ;
Label l2;
TextField un;
TextField pas;
Button b1;
Button b2;
Welcome w;
logindb()
{
l1=new Label("username : ");
l2=new Label("password : ");
68

un=new TextField();
Page

pas=new TextField();

SECTION:C2 A22205316002
b1=new Button("OK");
b2=new Button("CANCEL");

pas.setEchoChar('$');
l1.setBounds(50,50,80,20);
l2.setBounds(50,90,80,20);
un.setBounds(150,50,80,20);
pas.setBounds(150,90,80,20);
b1.setBounds(70,150,70,20);
b2.setBounds(170,150,70,20);

add(l1);
add(l2);
add(b1);
add(b2);
add(un);
add(pas);
setLayout(null);
setVisible(true);
setSize(500,500);

b1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
69

{
Page

Connection con;

SECTION:C2 A22205316002
Statement st;
ResultSet rs;
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
System.out.println("test");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Users/RAJA/Desktop/jdbc/access21.a
ccdb");
System.out.println("test");
st=con.createStatement();
rs=st.executeQuery("select * from user");
while(rs.next())
{
if(pas.getText().equals(rs.getString(2)));
{
setVisible(false);
w=new Welcome(rs.getString(1));
w.setVisible(true);
}
}
con.close();
}
catch(Exception f)
{
System.out.println(f);
}
}

}
);
70
Page

SECTION:C2 A22205316002
b1.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
}});
}

public static void main(String args[])


{
new logindb();
}}

71
Page

SECTION:C2 A22205316002
MAIL SYSTEM USING JSP

SIGN IN

72
Page

SECTION:C2 A22205316002
//html sign in page

<html>

<head>

<title>Signin</title>

</head>

<body>

<form action="index_action.jsp" method=POST>

<table border=0 align=center>

<tr><td>Email id:</td><td><input type=text name=T6 ></td></tr>

<tr><td>Password:</td><td><input type=password name=T7 ></td></tr>

<tr><td></td><td><a href="http://localhost:8080/gopal/Signup.jsp">Signup</a></td></tr>

<tr><td></td><td><input type=submit value=Signin></td></tr>

</table>

</form>

</body>

</html>

//Index_action.jsp

<%@ page import="java.sql.*" %>

<%!

String s1,s2,s4;

%>

<%

s1=request.getParameter("T6");

session.setAttribute("sessname",s1);

s2=request.getParameter("T7");
73

Connection con=null;
Page

Statement st=null;

SECTION:C2 A22205316002
ResultSet rs=null;

try

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program
Files/Apache Software Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

st=con.createStatement();

rs=st.executeQuery("select password from Entry where emailid='"+s1+"' ");

while(rs.next())

s4=rs.getString("password");

if(s2.equals(s4))

response.sendRedirect("Home.jsp");

else

response.sendRedirect("Signup2.jsp");

catch(Exception e)

}
74

%>
Page

SECTION:C2 A22205316002
SIGN UP PAGE

75
Page

SECTION:C2 A22205316002
//HTML signup page

<html>

<head>

<title>Signup</title>

</head>

<body>

<form action="Signup_action.jsp" method=POST>

<table border=0 align=center>

<tr><td>Name:</td><td><input type=text name=T1 required></td></tr>

<tr><td>Email id:</td><td><input type=text name=T2 required></td></tr>

<tr><td>Password:</td><td><input type=password name=T3 required></td></tr>

<tr><td>Contact:</td><td><input type=text name=T4 required></td></tr>

<tr><td>Gender:</td><td><input type=radio name=T5 value=Male>Male<br><input type=radio


name=T5 value=FeMale>FeMale</td></tr>

<tr><td></td><td><input type=submit value=Signup></td></tr>

</table>

</form>

</body>

</html>

//Signup_action.jsp

<%@ page import="java.sql.*" %>

<%!

String s1,s2,s3,s4,s5;

%>

<%

s1=request.getParameter("T1");
76

s2=request.getParameter("T2");
Page

s3=request.getParameter("T3");

SECTION:C2 A22205316002
s4=request.getParameter("T4");

s5=request.getParameter("T5");

%>

<%

Connection con=null;

PreparedStatement ps=null;

try{

Class.forName("net.ucanaccess.jdbc.Ucanaccess”);

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program Files/Apache Software


Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

ps=con.prepareStatement("insert into Entry(E_name,emailid,password,contactno,gender)


values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"')");

ps.executeUpdate();

catch(Exception e)

response.sendRedirect("index.jsp");

%>

77
Page

SECTION:C2 A22205316002
HOME PAGE

78
Page

SECTION:C2 A22205316002
//HTML content of Home.jsp

<html>

<frameset rows=10%,*>

<frame src="Home_Top.jsp">

<frameset cols=*>

<frame src="Home_Left.jsp">

</frameset>

</frameset>

</html>

//Home_left.jsp

<html>

<body style="background-color:DodgerBlue;">

<%@ page import="java.sql.* "%>

<table border=0 align=center>

<tr><td><a href="http://localhost:8080/gopal/Compose.jsp">Compose</a></td></tr>

<%!

String s=null;

%>

<%

String name=(String)session.getAttribute("sessname");

Connection con=null;

Statement st=null;

ResultSet rs=null;

try

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
79

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program
Page

Files/Apache Software Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

SECTION:C2 A22205316002
st=con.createStatement();

rs=st.executeQuery("select count(*) from inbox where sender = '"+name+"' and


status='unread' ");

while(rs.next())

s = rs.getString(1);

catch(Exception e)

%>

<tr><td><a
href="http://localhost:8080/gopal/inbox_action.jsp"">inbox</a>(<%out.println(s);%>)</td></tr>

<tr><td><a href="http://localhost:8080/gopal/Sent.jsp">Sent</a></td></tr>

<tr><td><a href="">Trash</a></td></tr>

</table>

</body>

</html>

//Home_Top.jsp

<html>

<body style="background-color:Tomato;">

<table border=0 align=center>

<tr><td><h1>WELCOME:</h1></td><td><h1><%String name=(String)session.getAttribute("sessname");
out.println(name);%></h1></td></tr>

</table>
80

</body>
Page

</html>

SECTION:C2 A22205316002
81
Page

SECTION:C2 A22205316002
Compose page

82
Page

SECTION:C2 A22205316002
//compose.jsp

<html>

<body>

<form action="Compose_action.jsp" method=post>

<a href="http://localhost:8080/gopal/Home_Left.jsp">HOME</a>

<table border=0 align=center>

<tr><td>To:</td><td><input type=text name=T10></td></tr>

<tr><td>Subject:</td><td><input type=text name=T11></td></tr>

<tr><td>Message:</td><td><textarea rows=10 cols=50 name=T12></textarea></td></tr>

<tr><td></td><td><input type=submit value=send></td></tr>

</table>

</form>

</body>

</html>

//compose_action.jsp

<html>

<body>

<%@ page import="java.sql.*" %>

<%!

String s1,s2,s3,name,s4;

String[] s5;

%>

<%

name=(String)session.getAttribute("sessname");

s1=request.getParameter("T10");

s2=request.getParameter("T11");

s3=request.getParameter("T12");
83

s4="unread";
Page

s5= s1.split(",");

SECTION:C2 A22205316002
%>

<%

Connection con=null;

Statement st=null;

PreparedStatement ps,ps2;

try {Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program Files/Apache Software
Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

for(int i=0;i<s5.length;i++)

{st=con.createStatement();

out.println(s5[i]);

ps=con.prepareStatement("insert into sent(To,Subject,Message,Host)


values('"+s5[i]+"','"+s2+"','"+s3+"','"+name+"')");

s2=con.prepareStatement("insert into inbox(From,Subject,Message,sender,status)


values('"+name+"','"+s2+"','"+s3+"','"+s5[i]+"','"+s4+"')");

ps.executeUpdate();

ps2.executeUpdate();}

catch(Exception e)

%>

<table border=0 align=center>

<tr><td><a href="http://localhost:8080/gopal/Home_Left.jsp">HOME</a></td></tr>

<tr><td><% out.println("Message Sent Successfully"); %></td></tr>

</table>

</body>

</html>
84
Page

SECTION:C2 A22205316002
SENT MAIL PAGE

85
Page

SECTION:C2 A22205316002
//sent.jsp

<html>

<body>

<form>

<a href="http://localhost:8080/gopal/Home_Left.jsp">HOME</a>

<%@ page import="java.sql.* "%>

<%!

String name;

%>

<%

name=(String)session.getAttribute("sessname");

Connection con=null;

Statement st=null;

ResultSet rs=null;

try

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program
Files/Apache Software Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

st=con.createStatement();

rs=st.executeQuery("select * from sent where Host = '"+name+"'");

out.println("<table align=center cellspacing=10px


cellpadding=10px><tr><th>To</th><th>Subject</th><th>Message</th></tr>");

while(rs.next())
86
Page

%>

SECTION:C2 A22205316002
<%out.println("<tr><td>"+rs.getString("To")+"</td><td>"+rs.getString("Subject")+"</td><td><textarea
rows=5 cols=50 disabled>"+rs.getString("Message")+"</textarea></td></tr><tr><td></td></tr>");%>

<%

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

catch(Exception e)

%>

</form>

</body>

</html>

87
Page

SECTION:C2 A22205316002
INBOX PAGE

88
Page

SECTION:C2 A22205316002
//Inbox_action.jsp

<html>

<body>

<%@ page import="java.sql.* "%>

<%!

String name,s1,s2;

%>

<%

name=(String)session.getAttribute("sessname");

s1="unread";

s2="read";

Connection con=null;

Statement st=null;

ResultSet rs=null;

try

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program
Files/Apache Software Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

st=con.createStatement();

rs=st.executeQuery("select * from inbox where sender = '"+name+"'");

out.println("<table align=center cellspacing=10px


cellpadding=10px><tr><th>From</th><th>Subject</th></tr>");

while(rs.next())

{ String s3=rs.getString("status");%> <% if(s3.equals(s1))


89

{ %>
Page

SECTION:C2 A22205316002
<%out.println("<tr><td><b><a
href=http://localhost:8080/gopal/message.jsp?"+rs.getInt("e_id")+">"+rs.getString("From")+"</a></b><
/td><td>"+rs.getString("Subject")+"</td></tr>");%>

<% } %>

<% if(s3.equals(s2))

{ %>

<%

out.println("<tr><td><a href=http://localhost:8080/gopal/message.jsp?"+rs.getInt("e_id")+">"+

rs.getString("From")+"</a></td><td>"+rs.getString("Subject")+"</td></tr>"); %>

<% } %> <%

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

catch(Exception e)

%>

</html>

</body>
90
Page

SECTION:C2 A22205316002
MESSAGE PAGE

91
Page

SECTION:C2 A22205316002
//message.jsp

<html>

<body>

<form action="Message_Delete.jsp" method=POST>

<%@ page import="java.sql.* "%>

<%!

String name,s1,s2;

%>

<a href="http://localhost:8080/gopal/inbox_action.jsp">Back</a>

<%

s2=request.getQueryString();

name=(String)session.getAttribute("sessname");

s1="read";

Connection con=null;

Statement st=null;

ResultSet rs=null;

PreparedStatement ps;

try

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program Files/Apache Software


Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

st=con.createStatement();

rs=st.executeQuery("select * from inbox where e_id='"+s2+"' ");

out.println("<table align=center cellspacing=10px cellpadding=10px>");

while(rs.next())

{
92

String s3=rs.getString("status");
Page

SECTION:C2 A22205316002
%>
<% if(s1.equals(s3))

{%>

<%out.println("<input type=hidden value="+rs.getString("From")+" name='c1'><input type=hidden


value="+rs.getString("Message")+" name='c2'><input type=hidden value="+rs.getInt("e_id")+"
name='c3'><tr><td>From:</td><td>"+rs.getString("From")+"</td></tr><tr><td>Subject</td><td>"+rs.ge
tString("Subject")+"</td></tr><tr><td>Message:</td><td><textarea rows=5 cols=50
disabled>"+rs.getString("Message")+"</textarea></td></tr><tr><td></td><td><input type=submit
value='Delete Message'></td></tr>");%>

<%
}%> <%

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

Catch(Exception e)

ps=con.prepareStatement("update inbox set status='"+s1+"'where sender='"+name+"' ");

ps.executeUpdate();

ps.close();

%> <a href="Compose.jsp">Reply</a>

</form>

</body>

</html>

//Message_delete.jsp

<html>

<body>

<%@ page import="java.sql.*" %>

<%!
93

String s1,s2,s3;
Page

SECTION:C2 A22205316002
%>

<%

s1=request.getParameter("c1");

s2=request.getParameter("c2");

s3=request.getParameter("c3");

%>

<%

Connection con=null;

Statement st=null;

PreparedStatement ps,ps2;

try {

Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");

con=DriverManager.getConnection("jdbc:ucanaccess://C:/Program Files/Apache Software


Foundation/Tomcat 9.0_Tomcat/webapps/gopal/Email.accdb");

ps=con.prepareStatement("insert into trash(emailid,message) values('"+s1+"','"+s2+"'


ps2=con.prepareStatement("Delete from inbox where e_id ="+s3+"");
ps.executeUpdate();

ps2.executeUpdate();

response.sendRedirect("inbox_action.jsp");

catch(Exception e)

%>

</body>

</html>
94
Page

SECTION:C2 A22205316002

You might also like