Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Program to demonstrate Command Line Argument.
- public class cla
- {
- public static void main(String args[])
- {
- int a,b,c;
- a=Integer.parseInt(args[0]);
- b=Integer.parseInt(args[1]);
- c=a+b;
- System.out.println("the sum is "+c);
- }
- }
- 2.Program to demonstrate BufferedReader Class.
- import java.io.*;
- public class io_test
- {
- public static void main(String args[]) throws IOException
- {
- InputStreamReader ip=new InputStreamReader(System.in);
- BufferedReader br=new BufferedReader(ip);
- System.out.println("enter your name ");
- String s=br.readLine();
- System.out.println("your name is "+s);
- }
- }
- 3.Program to demonstrate Parameterized Constructor.
- class Box{
- double width;
- double height;
- double depth;
- Box(double w,double h,double d)
- {
- width=w;
- height=h;
- depth=d;
- }
- double volume(){
- return width*height*depth;
- }
- }
- class demo
- {
- public static void main(String args[])
- {
- Box ob1=new Box(5,10,15);
- double vol;
- vol=ob1.volume();
- System.out.println("the volume of the box is "+vol);
- }
- }
- 4.Program to demonstrate Function overloading.
- class overloading
- {
- void test()
- { System.out.println("no parameters");
- }
- void test(int a)
- {System.out.println("single integer passed:a= "+a);
- }
- void test(int a,int b)
- {System.out.println("two integers passed:a= "+a+" b= "+b);
- }
- void test(double a)
- {
- System.out.println("double value passed:a= "+a);
- }
- }
- class overload
- {
- public static void main(String args[])
- {
- overloading ob= new overloading();
- ob.test();
- ob.test(4);
- ob.test(7,9);
- ob.test(3.75);
- }
- }
- 5.Program to demonstrate Inheritance.
- class Room
- {
- int length;
- int breadth;
- Room(int x,int y)
- { length=x;
- breadth=y;
- }
- int area()
- {
- return(length*breadth);
- }
- }
- class Bedroom extends Room
- {
- int height;
- Bedroom(int x,int y,int z)
- { super(x,y);
- height=z;
- }
- int volume()
- { return(length*breadth*height);
- }
- }
- class inherit_demo
- {
- public static void main(String args[])
- {
- Bedroom room1=new Bedroom(14,12,10);
- int area=room1.area();
- int volume=room1.volume();
- System.out.println("Area ="+area);
- System.out.println("volume ="+volume);
- }
- }
- 6.Program to demonstrate Exception Handling.
- class excep
- {
- public static void main(String args[])
- {
- int a=10,b=5,c=5;
- int x,y;
- try
- {
- x=a/(b-c);
- }
- catch(ArithmeticException e)
- { System.out.println("division by zero not allowed ");
- }
- y=a/(b+c);
- System.out.println("y= "+y);
- }
- }
- 7.Program to demonstrate Applets and Event Handling.
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
- /* <applet code="add" width=250 height=200>
- </applet>
- */
- public class add extends Applet implements ActionListener
- {
- TextField t1,t2,t3;
- Button b1;
- public void init()
- {
- t1=new TextField(5);
- t2=new TextField(5);
- t3=new TextField(5);
- b1=new Button("add");
- Label l1=new Label("enter first number ",Label.LEFT);
- Label l2=new Label(" enter second number ",Label.LEFT);
- Label l3=new Label(" the sum is ",Label.LEFT);
- add(l1); add(t1);
- add(l2); add(t2);
- add(b1);
- add(l3); add(t3);
- b1.addActionListener(this);
- }
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getSource()==b1)
- {
- int a=Integer.parseInt(t1.getText());
- int b=Integer.parseInt(t2.getText());
- t3.setText(String.valueOf(a+b));
- }
- }
- }
- 8.Program to demonstrate MouseEvents class.
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
- /*<applet code="MouseEvents" width=400 height=400>
- </applet>
- */
- public class MouseEvents extends Applet implements MouseListener,MouseMotionListener{
- String msg=" ";
- int mousex=0,mousey=0;
- public void init()
- {
- addMouseListener(this);
- addMouseMotionListener(this);
- }
- public void mouseClicked(MouseEvent me){
- mousex=0;
- mousey=20;
- msg="mouse clicked";
- repaint();
- }
- public void mouseEntered(MouseEvent me){
- mousex=0;
- mousey=20;
- msg="mouse entered";
- repaint();
- }
- public void mouseExited(MouseEvent me){
- mousex=0;
- mousey=20;
- msg="mouse exited";
- repaint();
- }
- public void mousePressed(MouseEvent me){
- mousex=me.getX();
- mousey=me.getY();
- msg="down";
- repaint();
- }
- public void mouseReleased(MouseEvent me){
- mousex=me.getX();
- mousey=me.getY();
- msg="up";
- repaint();
- }
- public void mouseDragged(MouseEvent me){
- mousex=me.getX();
- mousey=me.getY();
- msg="*";
- showStatus("dragging mouse at"+mousex+","+mousey);
- repaint();
- }
- public void mouseMoved(MouseEvent me){
- showStatus("moving mouse at "+me.getX()+","+me.getY());
- repaint();
- }
- public void paint(Graphics g){
- g.drawString(msg,mousex,mousey);
- }
- }
- 9.Program to demonstrate tabbedpane using swings.
- import javax.swing.*;
- /* <applet code="tabbedpane" width=400 height=200>
- </applet>
- */
- public class tabbedpane extends JApplet {
- public void init()
- { JTabbedPane jtp=new JTabbedPane();
- jtp.addTab("semester",new semesterpanel());
- jtp.addTab("course",new coursepanel());
- jtp.addTab("hobbies",new hobbiespanel());
- getContentPane().add(jtp);
- }
- }
- class semesterpanel extends JPanel{
- public semesterpanel(){
- JButton b1=new JButton ("1st");
- add(b1);
- JButton b2=new JButton ("2st");
- add(b2);
- JButton b3=new JButton ("3rd");
- add(b3);
- JButton b4=new JButton ("4th");
- add(b4);
- }
- }
- class coursepanel extends JPanel{
- public coursepanel(){
- JCheckBox ch1=new JCheckBox("b.tech");
- add(ch1);
- JCheckBox ch2=new JCheckBox("bba");
- add(ch2);
- JCheckBox ch3=new JCheckBox("bca");
- add(ch3);
- JCheckBox ch4=new JCheckBox("bcom");
- add(ch4);
- }
- }
- class hobbiespanel extends JPanel{
- public hobbiespanel(){
- JComboBox cb=new JComboBox();
- cb.addItem("cricket");
- cb.addItem("music");
- cb.addItem("coin collection");
- add(cb);
- }
- }
- 10.Program to create a demo application form using swings.
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- /* <applet code="appform" height=300 width=300>
- </applet>
- */
- public class appform extends JApplet implements ActionListener
- {
- JLabel l1,l2,l3,l4;
- JButton b1,b2;
- JTextField t1,t2,t3;
- JTextArea ta1;
- JRadioButton rb1,rb2;
- JComboBox cb;
- public void init()
- {
- try
- { SwingUtilities.invokeAndWait(new Runnable()
- {public void run()
- { createGUI();
- }
- });
- }
- catch(Exception e)
- { System.err.println("createGUI was not successfull");
- }
- }
- private void createGUI()
- {
- setLayout(new FlowLayout());
- l1=new JLabel("name");
- add(l1);
- t1=new JTextField(15);
- add(t1);
- l2=new JLabel("enter your address",JLabel.LEFT);
- add(l2);
- ta1=new JTextArea(10,20);
- add(ta1);
- l3=new JLabel(" select gender",JLabel.LEFT);
- add(l3);
- rb1=new JRadioButton("M");
- rb2=new JRadioButton("F");
- add(rb1);
- add(rb2);
- ButtonGroup bg=new ButtonGroup();
- bg.add(rb1);
- bg.add(rb2);
- l4=new JLabel(" choose your nation");
- add(l4);
- cb=new JComboBox();
- cb.addItem("india");
- cb.addItem("america");
- cb.addItem("italy");
- cb.addItem("australia");
- cb.addItem("russia");
- cb.addItem("germany");
- cb.addItem("others");
- add(cb);
- System.out.println(" ");
- b1=new JButton("submit");
- add(b1);
- b1.addActionListener(this);
- }
- public void actionPerformed(ActionEvent ae)
- {
- if(ae.getSource()==b1)
- { JOptionPane.showMessageDialog(this,"your form has been submitted","successfull",JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }
- 11.Program to implement JTree.
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.tree.*;
- /*<applet code="jtree" height=200 width=300>
- </applet>
- */
- public class jtree extends JApplet{
- JTree tr;
- public void init()
- {
- Container contentpane=getContentPane();
- contentpane.setLayout(new BorderLayout());
- DefaultMutableTreeNode top=new DefaultMutableTreeNode("folder 1");
- DefaultMutableTreeNode a=new DefaultMutableTreeNode("games");
- top.add(a);
- DefaultMutableTreeNode a1=new DefaultMutableTreeNode("cricket");
- a.add(a1);
- DefaultMutableTreeNode a2=new DefaultMutableTreeNode("football");
- a.add(a2);
- DefaultMutableTreeNode b=new DefaultMutableTreeNode("books");
- top.add(b);
- DefaultMutableTreeNode b1=new DefaultMutableTreeNode("allamaraju");
- b.add(b1);
- DefaultMutableTreeNode b2=new DefaultMutableTreeNode("e balaguruswamy");
- b.add(b2);
- DefaultMutableTreeNode b3=new DefaultMutableTreeNode("herbert schildt");
- b.add(b3);
- tr =new JTree(top);
- contentpane.add(tr);
- }
- }
- 12. Program to implement Remote Method Invocation(RMI)
- Product.java
- import java.rmi.*;
- public interface product extends Remote{
- public String getname() throws RemoteException;
- }
- Productimpl.java
- import java.rmi.*;
- import java.rmi.server.*;
- public class productimpl extends UnicastRemoteObject implements product
- {
- public String name1;
- productimpl(String name) throws RemoteException
- {
- name1=name;
- }
- public String getname()
- {
- return name1;
- }
- }
- Productclient.java
- import java.rmi.*;
- import java.rmi.registry.*;
- public class productclient
- {
- public static void main(String s[])
- {
- try
- {
- product p=(product)Naming.lookup("rmi://127.0.0.1:1099/java");
- System.out.println("productname="+p.getname());
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
- Productserver.java
- import java.rmi.*;
- import java.rmi.registry.*;
- public class productserver
- {
- public static void main(String s[])
- {
- try{
- LocateRegistry.getRegistry(1099);
- productimpl p1 =new productimpl("java");
- Naming.bind("java",p1);
- }
- catch(java.lang.Exception e)
- {System.out.println(e);
- }
- }
- }
- 13.Program to add two numbers using Remote Method Invocation.
- Productadd.java
- import java.rmi.*;
- public interface productadd extends Remote{
- public int add() throws RemoteException;
- }
- Productimpladd.java
- import java.rmi.*;
- import java.rmi.server.*;
- public class productimpladd extends UnicastRemoteObject implements productadd
- {
- public int x,y;
- productimpladd(int x1,int y1) throws RemoteException
- {
- x=x1;
- y=y1;
- }
- public int add()
- {
- return (x+y);
- }}
- Productclientadd.java
- import java.rmi.*;
- import java.rmi.registry.*;
- public class productclientadd
- {
- public static void main(String s[])
- {
- try
- {
- productadd p=(productadd)Naming.lookup("rmi://127.0.0.1:1099/java");
- System.out.println("output = "+p.add());
- }
- catch(Exception e)
- {
- System.out.println(e);
- }
- }
- }
- Productserveradd.java
- import java.rmi.*;
- import java.rmi.registry.*;
- public class productserveradd
- {
- public static void main(String s[])
- {
- try{
- LocateRegistry.getRegistry(1099);
- productimpladd p1 =new productimpladd(6,5);
- Naming.bind("java",p1);
- }
- catch(java.lang.Exception e)
- {System.out.println(e);
- }
- }
- }
- 13. Program to show the contents of a database table using JDBC.
- import java.sql.*;
- public class jdbcex
- { public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con=DriverManager.getConnection("jdbc:odbc:g45","","");
- Statement st=con.createStatement();
- ResultSet rs=st.executeQuery("select * from g45");
- while(rs.next())
- { System.out.println(rs.getString(1)+"\t"+rs.getString(2));
- }
- }
- catch(Exception e)
- { System.out.println("the error has occurred: "+e);
- }
- }
- }
- 14. Program to enter a row into the database table.
- import java.sql.*;
- public class jdbcex2
- { public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con=DriverManager.getConnection("jdbc:odbc:g45","","");
- Statement st=con.createStatement();
- int n=st.executeUpdate("insert into g45 (Name,Roll_no) values('Gaurav Kalra',43)");
- ResultSet rs1=st.executeQuery("select * from g45");
- while(rs1.next())
- { System.out.println(rs1.getString(1)+"\t"+rs1.getString(2));
- }
- }
- catch(Exception e)
- { System.out.println("the error has occurred: "+e);
- }
- }
- }
- 16. Program to update a database table in JDBC.
- employee table
- employee
- emp_id name salary address
- e01 rajesh kumar 22000 delhi
- e02 shekar kapur 25000 mumbai
- e03 reena rai 20000 kolkata
- e04 ahmed qureshi 18000 allahabad
- e05 manish pandey 28000 delhi
- e06 Gaurav Mishra 40000 noida
- import java.sql.*;
- public class jdbcup
- { public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- String str="update employee set salary=29800 where emp_id='e01'";
- Connection con=DriverManager.getConnection("jdbc:odbc:g44","","");
- Statement st=con.createStatement();
- int n=st.executeUpdate(str);
- ResultSet rs1=st.executeQuery("select * from employee");
- while(rs1.next())
- { System.out.println(rs1.getString(1)+"\t"+rs1.getString(2)+"\t"+rs1.getString(3)+"\t"+rs1.getString(4));
- }
- }
- catch(Exception e)
- { System.out.println("the error has occurred: "+e);
- }
- }
- }
- 17.Program to implement Prepared Statement in JDBC.
- import java.sql.*;
- public class pst
- { public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- String query="select * from employee where salary>20000";
- Connection con=DriverManager.getConnection("jdbc:odbc:g44","","");
- PreparedStatement pst=con.prepareStatement(query);
- ResultSet rs1=pst.executeQuery();
- while(rs1.next())
- { System.out.println(rs1.getString(1)+"\t"+rs1.getString(2)+"\t"+rs1.getString(3)+"\t"+rs1.getString(4));
- }
- }
- catch(Exception e)
- { System.out.println("the error has occurred: "+e);
- }
- }
- }
- 18. Program to update a database table in JDBC using Prepared Statement.
- import java.sql.*;
- public class psup
- {
- public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con1=DriverManager.getConnection("jdbc:odbc:g44","","");
- Statement st1=con1.createStatement();
- String str="Select * from employee";
- String str1="update employee set salary=? where emp_id=? ";
- //int n=st1.executeUpdate(str1);
- PreparedStatement pst=con1.prepareStatement(str1);
- pst.setString(1,"35000");
- pst.setString(2,"e04");
- pst.executeUpdate();
- pst.close();
- ResultSet rs=st1.executeQuery(str);
- // System.out.println("Rows updated:= "+n);
- while(rs.next())
- { System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getString(4));
- }
- con1.commit();
- con1.close();
- }
- catch(Exception e)
- { System.out.println("Error has occured :"+e);
- }
- }
- }
- 19.Program to implement scrollable Result set in JDBC.
- import java.sql.*;
- public class scrl
- {
- public static void main(String args[])
- {
- try {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con=DriverManager.getConnection("jdbc:odbc:g44","","");
- String s1,s2,s3;
- Statement stmt = con.createStatement(
- ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- ResultSet rs = stmt.executeQuery("SELECT * FROM employee");
- System.out.println("Next()");
- while (rs.next()) {
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- }
- System.out.println("Previous()");
- while (rs.previous()) {
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- }
- System.out.println("First()");
- rs.first();
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- System.out.println("Last()");
- rs.last();
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- System.out.println("Absolute(2)");
- rs.absolute(2);
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- System.out.println("Relative(2)");
- rs.relative(2);
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- System.out.println("Relative(-1)");
- rs.relative(-1);
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- System.out.println("BeforeFirst()");
- rs.beforeFirst();
- rs.next();
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- System.out.println("AfterLast()");
- rs.afterLast();
- rs.previous();
- s1 = rs.getString(1);
- s2 = rs.getString(2);
- s3 = rs.getString(3);
- System.out.println(s1+"\t"+s2+"\t"+s3);
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- }
- }
- 20.Program to implement Updatable Result set in JDBC.
- import java.sql.*;
- public class update
- {
- public static void main(String args[])
- {
- try
- {
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- Connection con1=DriverManager.getConnection("jdbc:odbc:g44","","");
- Statement st1=con1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
- String str="Select * from employee";
- ResultSet rs=st1.executeQuery(str);
- String s1,s2,s3,s4;
- int i=0;
- System.out.println("Before Update:-");
- while(rs.next())
- {
- s1=rs.getString(1);
- s2=rs.getString(2);
- s3=rs.getString(3);
- s4=rs.getString(4);
- if(s2.equals("reena rai"))
- { rs.updateString(2,"reena sharma");
- rs.updateRow();
- }
- System.out.println(++i+" "+s1+"\t"+s2+"\t"+s3+"\t"+s4);
- }
- // go to last row to update
- rs.last();
- rs.updateString(1,"e06");
- rs.updateString(2,"vinay katiyar");
- rs.updateRow();
- System.out.println("\nAfter update:-");
- i=0;
- rs.beforeFirst();
- while(rs.next())
- { s1=rs.getString(1);
- s2=rs.getString(2);
- s3=rs.getString(3);
- s4=rs.getString(4);
- System.out.println(++i+" "+s1+"\t"+s2+"\t"+s3+"\t"+s4);
- }
- rs.close();
- con1.commit();
- con1.close();
- }
- catch(Exception e)
- { System.out.println("Error has occured :"+e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement