You are on page 1of 28

INDEX OF PROGRAMS

S. No Program Name Page No.


1. Program to implement inheritance
2. Program to create threads using thread
class
3a. Program to create package mypackage and
create package class Rect
3b. Program to create Circle class using
mypackage
3c. Program using package mypackage
4. Program to implement exception handling
5. Program to implement String functions
6. Program to take values from console
using I/O package
7. Program to read existing file using i/o
package
8. Program to implement tabbed_pane using
swing
9. Program to implement JTree using swing
10a. Program to create employee table using
JDBC
10b. Program to insert values into table
using JDBC
10c. Program to update values of employee
table using JDBC
10d. Program to display employee information
using JDBC
11. Program to implement JDBC using swing
12a. Program to create remote interface using
RMI
12b. Program to implement the remote
interface using Unicast Remote Object
12c. Program to create server using RMI
12d. Program to create client using RMI
13a. Program to check if given id and
password are valid using servlet
13b. HTML program to enter user id# and
password
14a. Program to implement JDBC through JSP
14b. HTML program to enter user id and name
/*PROGRAM TO IMPLEMENT INHERITANCE*/

class Student
{
int rollNo;

Student(int a)
{
rollNo = a;
}

void putNo()
{
System.out.println("Roll No: "+rollNo);
}
}

class Test extends Student


{
int sub1,sub2;

Test(int x,int y)
{
super(6006);
sub1 = x;
sub2 = y;
}

void putTest()
{
System.out.println("Marks");
System.out.println("sub1: "+sub1+" sub2: "+sub2);
}
}

class Result extends Test


{
int total;

Result()
{
super(79,86);
total=sub1+sub2;
}

void putTotal()
{
System.out.println("total: "+total);
}
}

class InheritanceImpl
{
public static void main(String args[])
{
Result r1=new Result();

1
r1.putNo();
r1.putTest();
r1.putTotal();
}
}

2
/*PROGRAM TO CREATE THREADS USING THREAD CLASS*/

class ThreadImpl extends Thread


{
public void run()
{
for(int i=1;i<=3;i++)
System.out.println(i);
}

public static void main(String ap[])


{
try
{
ThreadImpl t =new ThreadImpl();
t.start();
ThreadImpl t1=new ThreadImpl();
t1.start();
sleep(1000);
ThreadImpl t2=new ThreadImpl();
t2.start();
System.out.println("main thread");
}
catch(Exception j)
{
System.out.println("main thread interrupted");
}
}
}

3
/*PROGRAM TO CREATE PACKAGE MYPACKAGE AND CREATE PACKAGE CLASS
RECT*/

package mypackage;

public class Rect


{
public int l,b;

public void display(int x,int y)


{
l=x;
b=y;
System.out.println("AREA OF RECTANGLE: " + (l*b) );
}
}

/*PROGRAM TO CREATE CIRCLE CLASS USING MYPACKAGE*/

package mypackage;

public class Circle


{
public int r;

public void display(int x)


{
r = x;
System.out.println("Area of circle: "+((22/7)*r*r));
}
}

/*PROGRAM USING PACKAGE MYPACKAGE*/

import mypackage.*;

class PackageImpl
{
public static void main(String a[])
{
Rect r=new Rect();
Circle c=new Circle();
r.display(10,20);
c.display(10);
}
}

4
/*PROGRAM TO IMPLEMENT EXCEPTION HANDLING*/

class ExceptionHandlingImpl
{
public static void main(String a[])
{
try
{
int b[]={10,20,30,40};
System.out.println(b[5]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("unsufficient index value:
"+e.getMessage());
}

try
{
int c=20;
int d=0;
System.out.println(c/d);
}
catch(ArithmeticException e)
{
System.out.println("don't divide by zero:
"+e.getMessage());
}
finally
{
System.out.println("demo of exception handling!");
}
}
}

5
/*PROGRAM TO IMPLEMENT STRING FUNCTIONS*/

public class StringFunctionsImpl


{
public static void main(String a[])
{
String s="GOOD MORNING";
String s1=s.toLowerCase(); //lowercase
System.out.println(s1);
String s2=s1.toUpperCase(); //Uppercase
System.out.println(s2);
String s3="hello";
String s4="h";
int p=s3.indexOf("l"); //index from left
System.out.println(p);
int p1=s3.lastIndexOf("l"); //index from right
System.out.println(p1);
boolean b=s3.equals("hello"); //equals & equalsIgnoreCase
System.out.println(b);
System.out.println(s3.equalsIgnoreCase("hello"));
int v=s4.compareTo("i"); //comparing strings
System.out.println(v);
String s5=s.substring(0,2); //sub string
System.out.println(s5);
char x[]=s.toCharArray();//char array

for(int i=0;i<s.length();i++)
{
System.out.println(x[i]);
}

String e=s.replace('G','h'); //Replace


System.out.println(e);
}
}

6
/*PROGRAM TO TAKE VALUES FROM CONSOLE USING I/O PACKAGE */

import java.io.*;

class ConsoleIOImpl
{
public static void main(String a[])
{
try
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));

String s[]=new String[100];

System.out.println("Enter some strings(* to end):");

for(int i=0;i<100;i++)
{
s[i]=br.readLine();

if(s[i].equals("*"))
break;
}

System.out.println("Strings you entered are: ");

for(int i=0;i<100;i++)
{
if(s[i].equals("*"))break;
System.out.println(s[i]);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

7
/*PROGRAM TO READ EXISTING FILE USING I/O PACKAGE*/

import java.io.*;

class FileIOImpl
{
public static void main(String a[]) throws IOException
{
int i;
FileInputStream fin=null;
try
{
fin=new FileInputStream(a[0]);

try
{
do
{
i=fin.read();
if(i!=-1)
System.out.print((char)i);
}while(i!=-1);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
catch(FileNotFoundException e)
{
System.err.println("Input file not found!");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("Usage java FileIOImpl <input file
name>");
}
catch(NullPointerException e)
{
System.err.println("Usage java FileIOImpl <input file
name>");
}
finally
{
fin.close();
}
}
}

8
/*PROGRAM TO IMPLEMENT TABBED_PANE USING SWING*/

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

/*<applet code="JTabbedPaneImpl" height="200"


width="200"></applet>*/

public class JTabbedPaneImpl extends JApplet


{
JTabbedPane tp;
public void init()
{
tp=new JTabbedPane();
tp.addTab("Town",new TownPanel());
tp.addTab("Flower",new FlowerPanel());
Container c=getContentPane();
c.add(tp);
}
}

class TownPanel extends JPanel implements ActionListener


{
JTextField t;
JButton b,b1;

TownPanel()
{
b=new JButton("Secunderabad");
b1=new JButton("Hyderabad");
t=new JTextField(15);
b.addActionListener(this);
b1.addActionListener(this);
add(b);
add(b1);
add(t);
}

public void actionPerformed(ActionEvent a)


{
if(a.getSource()==b)
t.setText(a.getActionCommand());
else
if(a.getSource()==b1)
t.setText(a.getActionCommand());
}
}

class FlowerPanel extends JPanel implements ActionListener


{
JTextField t;
JButton b,b1;

FlowerPanel()
{

9
b=new JButton("Rose");
b1=new JButton("Lillis");
t=new JTextField(15);
b.addActionListener(this);
b1.addActionListener(this);
add(b);
add(b1);
add(t);
}

public void actionPerformed(ActionEvent a)


{
if(a.getSource()==b)
t.setText(a.getActionCommand());
else
if(a.getSource()==b1)
t.setText(a.getActionCommand());
}
}

10
/*PROGRAM TO IMPLEMENT JTREE USING SWING*/

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

/*<applet code="JTreeImpl" width="200"


height="200"></applet>*/

public class JTreeImpl extends JApplet


{
Container con;
JTextField f;
DefaultMutableTreeNode top,a,a1,a2,b,b1,b2,c;
JTree t;
JScrollPane sp;
JPanel p;

public void init()


{
con=getContentPane();
con.setLayout(new BorderLayout());

p=new JPanel();
p.setLayout(new GridLayout());

f=new JTextField(15);
p.add(f);

top=new DefaultMutableTreeNode("Top");
a=new DefaultMutableTreeNode("A");
b=new DefaultMutableTreeNode("B");
c=new DefaultMutableTreeNode("c");
top.add(a);
top.add(b);
top.add(c);

a1=new DefaultMutableTreeNode("a1");
a2=new DefaultMutableTreeNode("a2");
a.add(a1);
a.add(a2);

b1=new DefaultMutableTreeNode("b1");
b2=new DefaultMutableTreeNode("b2");
b.add(b1);
b.add(b2);

t=new JTree(top);
t.addMouseListener(new MouseAdapter());

int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;

sp=new JScrollPane(t,v,h);

11
con.add(sp,BorderLayout.WEST);
con.add(p,BorderLayout.CENTER);
}

class MouseAdapter implements MouseListener


{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}

public void doMouseClicked(MouseEvent me)


{
TreePath tp = t.getPathForLocation(me.getX(),
me.getY());

if(tp!=null)
f.setText(tp.toString());
}

public void mouseEntered(MouseEvent me){}


public void mouseReleased(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mousePressed(MouseEvent me){}
}
}

12
/*PROGRAM TO CREATE EMPLOYEE TABLE USING JDBC */

import java.sql.*;
import java.io.*;

public class JDBCEmpTableCreation


{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=
DriverManager.getConnection("jdbc:odbc:san", "scott",
"tiger");

Statement st=con.createStatement();

st.executeUpdate("create table empdb(e_roll number,


e_name varchar(20), basic number(7,2), da number(7,2),
ta number(7,2), hra number(7,2), pf number(7,2),
total_salary number(7,2))");

System.out.println("table created successfully!");

st.close();
con.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}

13
/*PROGRAM TO INSERT VALUES INTO TABLE USING JDBC*/

import java.sql.*;
import java.io.*;

public class JDBCEmpTableInsertion


{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection( "jdbc:odbc:san", "scott",
"tiger");

PreparedStatement ps = con.prepareStatement("insert
into empdb values(?,?,?,?,?,?,?,?)");

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

System.out.println("enter how many rows you want to


insert: ");
int nrows=Integer.parseInt(br.readLine());

String
strRno,strName,strBasic,strDA,strTA,strHRA,strPF;
int rno;
float basic,da,ta,hra,pf,ts;

for(int i=0;i<nrows;i++)
{
System.out.println("Enter employee roll number: ");
strRno=br.readLine();

System.out.println("Enter employee name: ");


strName=br.readLine();

System.out.println("Enter basic: ");


strBasic=br.readLine();

System.out.println("Enter da: ");


strDA=br.readLine();

System.out.println("Enter ta: ");


strTA=br.readLine();

System.out.println("Enter hra: ");


strHRA=br.readLine();

System.out.println("Enter pf: ");


strPF=br.readLine();

rno=Integer.parseInt(strRno);

14
basic=Float.parseFloat(strBasic);
da=Float.parseFloat(strDA);
ta=Float.parseFloat(strTA);
hra=Float.parseFloat(strHRA);
pf=Float.parseFloat(strPF);

ts=(basic+da+ta+hra)-pf;

ps.setInt(1,rno);
ps.setString(2,strName);
ps.setFloat(3,basic);
ps.setFloat(4,da);
ps.setFloat(5,ta);
ps.setFloat(6,hra);
ps.setFloat(7,pf);
ps.setFloat(8,ts);

ps.executeUpdate();
}

System.out.println("Values inserted at runtime!");

ps.close();
br.close();
con.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}

15
/*PROGRAM TO UPDATE VALUES OF EMPLOYEE TABLE USING JDBC*/

import java.sql.*;
import java.io.*;

public class JDBCEmpTableUpdation


{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection( "jdbc:odbc:san", "scott",
"tiger");

Statement st=con.createStatement();
int i=st.executeUpdate("update empdb set total_salary=
basic + da+ta+hra-pf");

System.out.println(i+" rows updated.");

con.commit();
st.close();
con.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}

16
/*PROGRAM TO DISPLAY EMPLOYEE INFORMATION USING JDBC*/

import java.sql.*;
import java.io.*;

class JDBCEmpInfoDisplay
{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection( "jdbc:odbc:san", "scott",
"tiger");

Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from empdb");

while(rs.next())
{
System.out.print(rs.getInt(1)+":");
System.out.print(rs.getString(2)+":");
System.out.print(rs.getFloat(3)+":");
System.out.print(rs.getFloat(4)+":");
System.out.print(rs.getFloat(5)+":");
System.out.print(rs.getFloat(6)+":");
System.out.print(rs.getFloat(7)+":");
System.out.print(rs.getFloat(8)+":");
System.out.println();
}

rs.close();
st.close();
con.close();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}

17
/*PROGRAM TO IMPLEMENT JDBC USING SWING*/

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

public class JDBCSwingImpl extends JFrame implements


ActionListener
{
Connection con;
Statement st;
PreparedStatement ps;
ResultSet rs;
JTextField tDA,tTA,tHRA,tPF,tEId,tEName,tBasic,tTotalSalary;
JLabel lDA,lTA,lHRA,lPF,lEId,lEName,lBasic,lTotalSalary;
JButton bSave,bShow;

JDBCSwingImpl()
{
tDA=new JTextField(10);
tTA=new JTextField(10);
tHRA=new JTextField(10);
tPF=new JTextField(10);
tEId=new JTextField(10);
tEName=new JTextField(10);
tBasic=new JTextField(10);
tTotalSalary=new JTextField(10);
tTotalSalary.setEnabled(false);

lDA=new JLabel("DA",JLabel.CENTER);
lTA=new JLabel("TA",JLabel.CENTER);
lHRA=new JLabel("HRA",JLabel.CENTER);
lPF=new JLabel("PF",JLabel.CENTER);
lEId=new JLabel("Employee ID",JLabel.CENTER);
lEName=new JLabel("Employee Name",JLabel.CENTER);
lBasic=new JLabel("Basic",JLabel.CENTER);
lTotalSalary=new JLabel("Total Salary",JLabel.CENTER);

bSave=new JButton("Save");
bSave.addActionListener(this);
bShow=new JButton("Show");
bShow.addActionListener(this);

setLayout(new GridLayout(9,2));
setSize(300,300);

add(lEId);
add(tEId);
add(lEName);
add(tEName);
add(lBasic);
add(tBasic);
add(lDA);
add(tDA);
add(lTA);

18
add(tTA);
add(lHRA);
add(tHRA);
add(lPF);
add(tPF);
add(lTotalSalary);
add(tTotalSalary);

add(bSave);
add(bShow);
setVisible(true);
}

public void actionPerformed(ActionEvent a)


{
try
{
if(a.getSource()==bSave)
{
int eId;
String name;
float basic,da,ta,hra,pf,ts;
eId=Integer.parseInt(tEId.getText());
name=tEName.getText();
basic=Float.parseFloat(tBasic.getText());
da=Float.parseFloat(tDA.getText());
ta=Float.parseFloat(tTA.getText());
hra=Float.parseFloat(tHRA.getText());
pf=Float.parseFloat(tPF.getText());
ts=basic+da+ta+hra-pf;

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con =
DriverManager.getConnection("jdbc:odbc:san",
"scott","tiger");

ps=con.prepareStatement("insert into empdb


values(?,?,?,?,?,?,?,?)");

ps.setInt(1,eId);
ps.setString(2,name);
ps.setFloat(3,basic);
ps.setFloat(4,da);
ps.setFloat(5,ta);
ps.setFloat(6,hra);
ps.setFloat(7,pf);
ps.setFloat(8,ts);

ps.executeUpdate();
}
catch(Exception e)
{
System.err.println(e.getMessage());
}

19
finally
{
ps.close();
con.close();
}

}
if(a.getSource()==bShow)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(" jdbc:odbc:san",
"scott", "tiger");

st=con.createStatement();
rs=st.executeQuery("select * from empdb");
if(rs.next())
{
tEId.setText(new Integer( rs.getInt(1)).
toString());
tEName.setText(rs.getString(2));
tBasic.setText(new Float(rs.getFloat(3)).
toString());
tDA.setText(new Float(rs.getFloat(4)).
toString());
tTA.setText(new Float(rs.getFloat(5)).
toString());
tHRA.setText(new Float(rs.getFloat(6)).
toString());
tPF.setText(new Float(rs.getFloat(7)).
toString());
tTotalSalary.setText(new
Float(rs.getFloat(8)).toString());
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
finally
{
ps.close();
con.close();
}
}
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}

public static void main(String args[])


{
new JDBCSwingImpl();

20
}
}

21
/*PROGRAM TO CREATE REMOTE INTERFACE USING RMI*/

import java.rmi.*;

public interface AddServerIntf extends Remote


{
double add(double d1,double d2) throws RemoteException;
}

/*PROGRAM TO IMPLEMENT THE REMOTE INTERFACE USING UNICAST


REMOTE OBJECT*/

import java.rmi.*;
import java.rmi.server.*;

public class AddServerImpl extends UnicastRemoteObject


implements AddServerIntf
{

public AddServerImpl() throws RemoteException {}

public double add(double d1,double d2) throws


RemoteException
{
return d1+d2;
}
}

/*PROGRAM TO CREATE SERVER USING RMI*/

import java.rmi.*;
import java.net.*;

public class AddServer


{
public static void main(String args[])
{
try
{
AddServerImpl addServerImpl=new AddServerImpl();
Naming.rebind("AddServer",addServerImpl);
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}

22
/*PROGRAM TO CREATE CLIENT USING RMI*/

import java.rmi.*;

public class AddClient


{
public static void main(String args[])
{
try
{
String addServerURL="rmi://"+args[0]+"/AddServer";
System.out.println(addServerURL);
AddServerIntf addServerIntf
=( AddServerIntf)Naming.lookup(addServerURL);
double d1=Double.parseDouble(args[1]);
double d2=Double.parseDouble(args[2]);
System.out.println(addServerIntf.add(d1,d2));
}
catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("Usage java AddClient <URL> <Val1>
<Val2>");
}
catch(Exception e)
{
System.err.println(e.getMessage());
}
}
}

23
/*PROGRAM TO CHECK IF GIVEN ID AND PASSWORD ARE VALID USING
SERVLET*/

import javax.servlet.*; //for servlet Exception


import javax.servlet.http.*;
import java.io.*; //for print writer&IOException

public class ValidationServlet extends HttpServlet


{
public void service(HttpServletRequest
req,HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();

//retrieving user’s entry values into the servlet

String str1=req.getParameter("t1");
String str2=req.getParameter("t2");

//validation logic

if(str1.equals("Santosh")&&str2.equalsIgnoreCase("Java"))
{
out.println("<B>VALID</B><BR>");
out.println("Thank you");
}
else
{
out.println("<B>INVALID</B><BR>");
out.println("Sorry Try again");
}
out.close();
}
}

24
<!-HTML PROGRAM TO ENTER ID AND PASSWORD

<html>
<head>
<title>Servlet example</title>
</head>
<body>
<h3 align="center">User Name & Password</h3>
<FORM METHOD="get"
ACTION="http://localhost:8080/servlet/ValidationServlet">
User Name: <input type="text" name="t1"><br>
Password: <input type="password" name="t2"><br>
<input type="submit" value="send">
<input type="reset" value="clear">
</form>
</body>
</html>

25
/* PROGRAM TO IMPLEMENT JDBC THROUGH JSP */

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


<%
try
{
string s1=request.getParameter("t1");
string s2=request.getParameter("t2");

class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection
con=DriverManager.getConnection("jdbc:odbc:san",
"scott","tiger");

PreparedStatement ps=con.prepareStatement("insert into


login values(?,?)");

int n1=Integer.parseInt(s1);

ps.setInt(1,n1);
ps.setString(2,s2);
%>

<%="one row created"%>


<%
ps.close();
con.close();
}
catch(Exception e)
{}
%>

26
<!-PROGRAM TO ENTER ID AND NAME USING HTML

<html>
<title>
JDBC Using JSP example...
</title>
<body>
<h1><marquee>Welcome</marquee></h1>
<form name="f1" method=post action="JDBC_Using_JSP.jsp">
User ID: <input type=text id="t1"><br>
Password: <input type=text name="t2"><br>
<input type=submit value="Send">
</form>
</body>
</html>

27

You might also like