Download
Download
1.Write an Applet program to design a user interface to key-in the details of an employee.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class empapp extends Applet implements ActionListener,ItemListener
{
Panel p0,p1,p2;
TextField t1= new TextField(10);
TextField t2= new TextField(10);
TextField t3= new TextField(10);
TextField t4= new TextField(10);
TextField t5= new TextField(10);
TextField t6= new TextField(10);
Button clr=new Button("clear");
Button ok=new Button("submit");
public void init()
{
p0=new Panel();
p0.setLayout(new GridLayout(1,1));
p0.add(new Label(" EMPLOYEE INFORMATION "));
add(p0);
p1=new Panel();
p1.setLayout(new GridLayout(6,2));
p1.add(new Label("ID "));
p1.add(t1);
p1.add(new Label("NAME "));
p1.add(t2);
p1.add(new Label("DOB "));
p1.add(t3);
p1.add(new Label("DOJ "));
p1.add(t4);
p1.add(new Label("GENDER "));
p1.add(t5);
p1.add(new Label("SALARY "));
p1.add(t6);
add(p1);
p2=new Panel();
p2.setLayout(new GridLayout(1,2));
p2.add(clr);
clr.addActionListener(this);
p2.add(ok);
ok.addActionListener(this);
add(p2);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("submit"))
System.out.println("SUCCESSFULLY INSERTED");
if(s.equals("clear"))
{
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
t4.setText(" ");
t5.setText(" ");
t6.setText(" ");
}
repaint();
}
public void itemStateChanged(ItemEvent e)
{
repaint();
}
}
“ Success is what happens after you have survived all of your disappointments.“
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class selectitem extends Applet
{
String s[]=new String[20];
List lst;
Panel p1,p2,p3;
Button add,rem;
TextField t;
int i=0;
public void init()
{
p1=new Panel();
p2=new Panel();
add=new Button("ADD");
rem=new Button("REMOVE");
lst = new List();
p1.setLayout(new GridLayout(2,1));
p1.add(new Label(" ITEM LIST "));
p1.add(lst);
add(p1);
t=new TextField(10);
p2.setLayout(new GridLayout(2,2));
p2.add(t);
p2.add(add);
p2.add(new Label(" "));
p2.add(rem);
add.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (i<20)
{
s[i]=t.getText();
lst.addItem(s[i++]);
}
t.setText("");
}
}
);
lst.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
repaint();
}
}
);
rem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(lst.getSelectedIndex()!=-1)
{
lst.remove(lst.getSelectedIndex ());
i--;
}
}
}
);
add(p2);
}
}
“ Results happen over time, not overnight. Work hard, stay consistent and be patient. “
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class geometric extends Applet implements ItemListener
{
Choice c;
String m = " ";
public void init()
{
c = new Choice();
c.add("None");
c.add("Square");
c.add("Rectangle");
c.add("Ellipse");
c.add("Circle");
add(c);
c.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
m = " Selected Geometry: ";
m += c.getSelectedItem();
g.drawString(m,90,340);
if(c.getSelectedItem()=="Square")
{
g.fillRect(100,150,150,150);
}
if(c.getSelectedItem()=="Rectangle")
{
g.setColor(Color.green);
g.fillRect(50,150,250,150);
}
if(c.getSelectedItem()=="Ellipse")
{
g.setColor(Color.red);
g.fillOval(100,150,100,150);
}
if(c.getSelectedItem()=="Circle")
{
g.setColor(Color.blue);
g.fillOval(100,150,150,150);
}
}
}
“ The best way to gain self-confidence is to do what you are afraid to do. “
-Swati Sharma
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class mouseevent extends Applet implements MouseListener
{
String msg=" ";
final Font f=new Font("TimesRoman",Font.BOLD,30);
public void init()
{
addMouseListener(this);
}
public void paint(Graphics g)
{
g.setFont(f);
g.drawString(msg,20,30);
}
public void mouseEntered(MouseEvent me)
{
msg="mouse entered";
repaint();
}
public void mousePressed(MouseEvent me)
{
msg="mouse pressed";
repaint();
}
public void mouseClicked(MouseEvent me)
{
msg="mouse clicked";
repaint();
}
public void mouseExited(MouseEvent me)
{
msg="mouse exited";
repaint();
}
public void mouseReleased(MouseEvent me)
{
msg="mouse released";
repaint();
}
}
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class keyevent extends Applet implements KeyListener
{
final Font f=new Font("TimesRoman",Font.BOLD,30);
String msg=" ";
public void init()
{
addKeyListener(this);
requestFocus();
}
public void paint(Graphics g)
{
g.setFont(f);
g.drawString(msg,20,30);
}
public void keyPressed(KeyEvent k)
{
showStatus("KEY PRESSED");
int key=k.getKeyCode();
msg="PRESSED KEY IS: ";
switch(key)
{
case KeyEvent.VK_UP:
msg+="MOVE TO UP";
break;
case KeyEvent.VK_DOWN:
msg+="MOVE TO DOWN";
break;
case KeyEvent.VK_LEFT:
msg+="MOVE TO LEFT";
break;
case KeyEvent.VK_RIGHT:
msg+="MOVE TO RIGHT";
break;
default:
msg+="OTHER KEYS";
break;
}
repaint();
}
public void keyReleased(KeyEvent k)
{
showStatus("KEY RELEASED");
}
public void keyTyped(KeyEvent k)
{
showStatus("KEY TYPED");
msg=" TYPED KEY:";
msg+=k.getKeyChar();
repaint();
}
}
import java.io.*;
class writefile
{
public static void main(String args[]) throws Exception
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the text:");
String s= in.readLine();
PrintWriter out = new PrintWriter(new FileWriter("fileout.txt"));
out.println(s);
out.close();
}
catch(Exception e)
{
System.out.println("Writing failed");
}
}
}
import java.io.*;
class readfile
{
public static void main(String args[]) throws IOException
{
int i;
FileInputStream f;
try
{
f = new FileInputStream(args[0]);
}
catch(Exception e)
{
System.out.println("File not found/array index error");
return;
}
do
{
i = f.read();
if(i != -1)
System.out.print((char) i);
} while(i != -1);
f.close();
}
}
import java.io.*;
public class editfile
{
public static void main(String[] args)
{
File f=new File("edit.txt");
FileInputStream fi = null;
InputStreamReader in = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
String s;
try
{
fi = new FileInputStream(f);
in = new InputStreamReader(fi);
br = new BufferedReader(in);
while(true)
{
s=br.readLine();
if(s==null)
break;
sb.append(s);
}
String e= "abc";
int c = sb.indexOf(e);
sb.replace(c,c+e.length()," SIR.M.V ");
String e1 = "xyz";
int c1 = sb.indexOf(e1);
sb.replace(c1,c1+e1.length()," BOMMANAKATTE");
fi.close();
in.close();
br.close();
}
catch (Exception e)
{
System.out.println("Error");
}
try
{
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(sb.toString());
bw.close();
}
catch (Exception e)
{
System.err.println("Writing Error ");
}
}
}
“ Setting goals is the first step into turning the invisible into the visible. “
-Tony Robbins
9. Write a Java program with JDBC to store the details of a person on to an Oracle
database table.
import java.io.*;
import java.sql.*;
class dbinsert
{
public static void main(String[] args) throws Exception
{
String fname,lname,dob,adrs;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER DETAILS");
System.out.print("Enter first name : ");
fname=br.readLine();
System.out.print("Enter last Name : ");
lname=br.readLine();
System.out.print("Enter DOB : ");
dob=br.readLine();
System.out.print("Enter address : ");
adrs=br.readLine();
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
Connection con = DriverManager.getConnection(url,"system","tiger");
Statement stmt = con.createStatement();
stmt.executeUpdate("insert into person values ('"+fname+"','"+lname+"','"+dob+"','"+adrs+"')");
stmt.close();
con.close();
System.out.println("Inserted successfully");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
10. Write a Java program with JDBC to access and display the details of a person stored in
an Oracle database table.
import java.io.*;
import java.sql.*;
class dbaccess
{
public static void main(String[] args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter name : ");
String name=br.readLine();
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
Connection con = DriverManager.getConnection(url,"system","tiger");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from person where Fname = '"+name+"'");
while(rs.next())
{
String fname=rs.getString(1);
String lname=rs.getString(2);
String dob=rs.getString(3);
String addr=rs.getString(4);
System.out.println("DETAILS OF PERSON");
System.out.println("PERSON FNAME : "+fname);
System.out.println("PERSON LNAME : " +lname);
System.out.println("DATE OF BIRTH : " +dob);
System.out.println("ADDRESS : "+addr);
}
stmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
11.Write a Java program with JDBC to access and delete the details of a given person
stored in an Oracle database table.
import java.io.*;
import java.sql.*;
class dbdelete
{
public static void main(String[] args)throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter name : ");
String name=br.readLine();
ResultSet rs;
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
Connection con = DriverManager.getConnection(url,"system","tiger");
Statement stmt = con.createStatement();
stmt.executeUpdate("delete from person where Fname = '"+name+"'");
stmt.close();
con.close();
System.out.println("Successfully deleted");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
12. Write a Java GUI program to accept the details of an employee and store the same on
to an Oracle database table.
Table creation:
create table emp( eid number(6), ename char(15), dob char(15),
dpt char(12),salary number(6) );
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class GUIinsert extends JFrame
{
JLabel lid, lname, ldob, ldpt,lsal;
JTextField tid,tname,tdob,tdpt,tsal;
JButton ins,clr;
GUIinsert()
{
super("EMPLOYEE");
Container c=getContentPane();
c.setLayout(new GridLayout(6,2));
lid=new JLabel("EMPLOYEE ID:");
lname=new JLabel("EMPLOYEE NAME:");
ldob=new JLabel("DOB:");
ldpt=new JLabel("DEPARTMENT:");
lsal=new JLabel("SALARY:");
tid=new JTextField(5);
tname=new JTextField(5);
tdob=new JTextField(5);
tdpt=new JTextField(5);
tsal=new JTextField(5);
ins=new JButton("INSERT");
clr=new JButton("CLEAR");
c.add(lid);
c.add(tid);
c.add(lname);
c.add(tname);
c.add(ldob);
c.add(tdob);
c.add(ldpt);
c.add(tdpt);
c.add(lsal);
c.add(tsal);
c.add(ins);
c.add(clr);
clr.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tid.setText(null);
tname.setText(null);
tdob.setText(null);
tdpt.setText(null);
tsal.setText(null);
}
}
);
ins.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String s1=tid.getText();
String s2=tname.getText();
String s3=tdob.getText();
String s4=tdpt.getText();
String s5=tsal.getText();
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
Connection con = DriverManager.getConnection(url,"system","tiger");
PreparedStatement st=con.prepareStatement("insert into
emp(eid,ename,dob,dpt,salary) values(?,?,?,?,?)");
st.setString(1,s1);
st.setString(2,s2);
st.setString(3,s3);
st.setString(4,s4);
st.setString(5,s5);
st.executeUpdate();
JOptionPane.showMessageDialog(getContentPane()," SUCCESSFULLY
INSERTED");
st.close();
con.close();
}
catch(Exception e)
{
System.err.println(e);
JOptionPane.showMessageDialog(getContentPane(), "INSERTION FAILED");
}
}
}
);
}
public static void main(String args[])
{
GUIinsert g=new GUIinsert();
g.setSize(400,400);
g.setVisible(true);
g.setResizable(true);
}
}
13. Write a Java GUI program to access and display the details of a given employee stored
in Oracle database table.
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class GUIdisplay extends JFrame
{
JLabel lid;
JTextField tid;
JButton disp,clr;
GUIdisplay()
{
super("EMPLOYEE");
Container c=getContentPane();
c.setLayout(new GridLayout(2,2));
lid=new JLabel(" ENTER EMPLOYEE id:");
tid=new JTextField(8);
disp=new JButton("DISPLAY");
clr=new JButton("CLEAR");
c.add(lid);
c.add(tid);
c.add(disp);
c.add(clr);
clr.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
tid.setText(null);
}
}
);
disp.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
int value1=Integer.parseInt(tid.getText());
try
{
Class.forName ("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
Connection con = DriverManager.getConnection(url,"system","tiger");
//String s= "SELECT eid, ename, dob,dpt,salary FROM emp WHERE eid = ?";
PreparedStatement st=con.prepareStatement("SELECT eid, ename, dob,dpt,salary
FROM emp WHERE eid = ?");
st.setInt(1,value1);
ResultSet rs = st.executeQuery();
while (rs.next())
{
String eid=rs.getString(1);
String ename=rs.getString(2);
String edob=rs.getString(3);
String dpt=rs.getString(4);
String sal=rs.getString(5);
JOptionPane.showMessageDialog(getContentPane(),"EMPLOYEE DETAILS \n"
+"EMPLOYEE id : "+eid+"\n"
+"EMPLOYEE NAME : "+ename+"\n"
+"DATE OF BIRTH : "+edob+"\n"
+"EMPLOYEE dpt : "+dpt+"\n"
+"EMPLOYEE SALARY : "+sal+"\n");
}
con.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
JOptionPane.showMessageDialog(null,"ERROR IN DISPLAY");
}
}
}
);
}
public static void main(String args[])
{
GUIdisplay g=new GUIdisplay();
g.setSize(300,300);
g.setVisible(true);
g.setResizable(true);
}
}
14.Write a Java program to design a simple Client and Server components. Pass simple
text (static) from client to the server and a receipt acknowledgement (static) back to the
client.
Client program:
import java.io.*;
import java.net.*;
public class meclient
{
public static void main(String args[])
{
Socket c = null;
PrintWriter pw = null;
BufferedReader br = null;
try
{
c= new Socket("localhost",3456);
pw= new PrintWriter(c.getOutputStream(), true);
br= new BufferedReader(new InputStreamReader(c.getInputStream()));
System.out.println("Connected to Server");
pw.println("I am client");
String replay = br.readLine();
System.out.println("Replay from the server: " + replay);
pw.close();
br.close();
c.close();
}
catch(Throwable e)
{
System.out.println("Error: check it");
}
}
}
Server program:
import java.io.*;
import java.net.*;
public class meserver
{
public static void main(String args[])
{
ServerSocket s = null;
Socket c = null;
PrintWriter pw = null;
BufferedReader br = null;
try
{
s = new ServerSocket(3456);
System.out.println("waiting for client ");
c=s.accept();
System.out.println("client is connected");
pw= new PrintWriter(c.getOutputStream(),true);
br= new BufferedReader(new InputStreamReader(c.getInputStream()));
String msg = br.readLine();
System.out.println("Message from the client: " + msg);
pw.println("I am server");
pw.close();
br.close();
c.close();
s.close();
}
catch(Throwable e)
{
System.out.println("Error:CHECK IT ");
}
}
}
“ Do not think about what might go wrong. Think about what might go right. “