Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Thursday, 6 December 2012

student information system using java jdbc odbc

java student information system program jdbc odbc

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

public class StudInformation extends JFrame implements ActionListener
{
    JTextField txt_id,txt_name,txt_age,txt_course,txt_semester,txt_tutor;
    JLabel lbl_title,lbl_id,lbl_name,lbl_age,lbl_course,lbl_semester,lbl_tutor;
    JButton btn_insert,btn_delete,btn_update,btn_clear,btn_search;
       
    Connection c;
   
    public StudInformation()
    {
        lbl_title=new JLabel("Student Information");
        lbl_id=new JLabel("Id");
        lbl_name=new JLabel("Name");
        lbl_age=new JLabel("Age");
        lbl_course=new JLabel("Course");
        lbl_semester=new JLabel("Semester");
        lbl_tutor=new JLabel("Tutor");   

        txt_id=new JTextField(20);
        txt_name=new JTextField(20);

Servlet program for change background color

Change background color using servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ColorServlet extends HttpServlet
{
    public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
    {
        res.setContentType("Text/Html");
        String c=req.getParameter("color");
        PrintWriter pw=res.getWriter();
        if(c.equals("red"))
            pw.println("<body BGCOLOR=red>");
        if(c.equals("green"))
            pw.println("<body BGCOLOR=green>");
        if(c.equals("green"))
            pw.println("<body BGCOLOR=green>");

Running face using java applet

Java applet program to implementing running face

import java.awt.*;
import java.applet.*;

/*
    <Applet    code="runFace"
        height=500
        width=500
    >
    </Applet>
*/

public class runFace extends Applet implements Runnable
{
    int h1,h2,h3,h4;
    int ley1,ley2,ley3,ley4;
    int rey1,rey2,rey3,rey4;
    int leyld1,leyld2,leyld3,leyld4;
    int reyld1,reyld2,reyld3,reyld4;
    int ns1,ns2,ns3,ns4;
    int mt1,mt2,mt3,mt4;
    int flag;
   
    public void init()
    {
        h1=40;h2=40;h3=150;h4=150;
        ley1=70;ley2=70;ley3=30;ley4=20;
        rey1=120;rey2=70;rey3=30;rey4=20;
        reyld1=125;reyld2=65;reyld3=150;reyld4=65;
        leyld1=70;leyld2=65;leyld3=95;leyld4=65;
        mt1=90;mt2=140;mt3=50;mt4=10;
        ns1=112;ns2=90;ns3=112;ns4=125;
        setBackground(Color.cyan);
    }
    public void start()
    {   

Read data from databse to table JAVA program using JDBC ODBC

Java program to read database into jtable JDBC ODBC

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

/*This program may show depricated error while compiling this is not a problem we can run it by using java EmpInform*/
public class EmpInform extends JFrame
{
    Connection con;
    JTable jtb;
    Vector data;
    Vector columns;
    Vector row;
    Container c;
   
    public EmpInform()
    {
        try{
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con=DriverManager.getConnection("jdbc:odbc:emp1"); //emp1 is the DSN name
                Statement st=con.createStatement();
                String query="select * from employee";//employee is the table name
                ResultSet rs=st.executeQuery(query);
                ResultSetMetaData md = rs.getMetaData(); 

Factorial using RMI

RMI program for find factorial of a number

RMI interface

import java.rmi.*;
public interface myInterface extends Remote
{
    double fact(int i)throws RemoteException;
}

RMI server

import java.rmi.*;
import java.rmi.server.*;
public class myServer extends UnicastRemoteObject implements myInterface
{
    public myServer()throws RemoteException
    {
        super();
    }

    public double fact(int i)throws RemoteException
    {
        int f=1;
        int n=1;
        while(n<=i)
        {
            f=f*n;
            n++;
        }
        return(f);

    }
    public static void main(String args[])
    {
        try{
            myServer ms=new myServer();
            Naming.rebind("rmi://hsj./myInterface",ms);
/*  bolded area must replace with your computer name */
            System.out.print("Start...");
           
        }

RMI chat program

Chat program using java RMI

RMI Interface program

myInterface.java
import java.rmi.*;
public interface myInterface extends Remote
{
    public String message(String m)throws RemoteException;
}

Server program

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

public class myServer extends UnicastRemoteObject implements myInterface

JSP custom tag with body

 Custom tag JSP

Steps for creating custom tag get from here

code for creating jsp custom tag with body

Tag implementation


TagBody.java

package st;

import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;


public class TagBody extends TagSupport
{

     public int doStartTag() throws JspException
     {
        try{
               JspWriter out = pageContext.getOut();
               out.println( "<table border=1>");
           
                      out.println("<tr><td>  ");
                  

            }
        catch(Exception ex)
        {
        }
        return EVAL_BODY_INCLUDE;
     }

    public int doEndTag() throws JspException

Custom tag using JSP without body

JSP custom tag without body

steps

first create a folder named as st in WEB-INF\classes directory

Then create java file named as HellTag.java  inside st

paste the following on  HellTag.java file

package st;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

public class HellTag extends TagSupport
{
    public int doStartTag()
    {
        try{
            JspWriter js=pageContext.getOut();
            js.print("Hello");
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        return SKIP_BODY;
    }
}

compile the HelloTag.java file by following manner
javac HellTag.java -classpath "C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\jsp-api.jar"

Then create st.tld file inside the WEB-INF directory

Friday, 16 November 2012

Employee Information System using JAVA and JDBC ODBC

 Java Program to implement employee information system using JAVA and JDBC ODBC

 Employee Information System


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


public class EmpInformation extends JFrame implements ActionListener
{
    JLabel lbl_title,lbl_id,lbl_name,lbl_quali,lbl_design,lbl_salary;
    JButton btn_save,btn_update,btn_delete,btn_search,btn_clear,btn_exit;
    JTextField txt_id,txt_name,txt_quali,txt_design,txt_salary;

    JPanel p1,p2,p3;
   
    Connection con;
   
    public EmpInformation()
    {
        lbl_title=new JLabel("EMPLOYEE INFORMATION SYSTEM");
        lbl_id=new JLabel("ID");
        lbl_name=new JLabel("Name");
        lbl_quali=new JLabel("Qualification");
        lbl_design=new JLabel("Designation");
        lbl_salary=new JLabel("Salary");
       
        btn_save=new JButton("Save");
        btn_update=new JButton("Update");
        btn_delete=new JButton("Delete");
        btn_search=new JButton("Search");
        btn_clear=new JButton("Clear");
        btn_exit=new JButton("Exit");
       
        txt_id=new JTextField(20);
        txt_name=new JTextField(10);
        txt_quali=new JTextField(10);
        txt_design=new JTextField(10);
        txt_salary=new JTextField(10);
       
        btn_save.addActionListener(this);
        btn_update.addActionListener(this);
        btn_delete.addActionListener(this);
        btn_search.addActionListener(this);
        btn_clear.addActionListener(this);
        btn_exit.addActionListener(this);
       
        p1=new JPanel();
        p2=new JPanel();
        p3=new JPanel();

        p1.setLayout(new FlowLayout());
        p1.add(lbl_title);
       
        p2.setLayout(new FlowLayout());
        p2.add(btn_save);
        p2.add(btn_update);
        p2.add(btn_delete);
        p2.add(btn_search);
        p2.add(btn_clear);
        p2.add(btn_exit);
       
        p3.setLayout(new GridLayout(5,2));
        p3.add(lbl_id);
        p3.add(txt_id);
        p3.add(lbl_name);
        p3.add(txt_name);
        p3.add(lbl_quali);
        p3.add(txt_quali);
        p3.add(lbl_design);
        p3.add(txt_design);
        p3.add(lbl_salary);
        p3.add(txt_salary);
       
        Container c=getContentPane();
        c.setLayout(null);
        p1.setBounds(30,20,400,30);
        p3.setBounds(10,90,200,200);
        p2.setBounds(10,300,300,90);
   
        c.add(p1);
        c.add(p2);
        c.add(p3);
    }

    public void actionPerformed(ActionEvent ae)       
    {
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:emp");
        }
        catch(Exception ex)
        {
            System.out.println(ex.getMessage());
        }
        String st=ae.getActionCommand();
       
        if(st.equals("Clear"))
        {
            txt_id.setText("");
            txt_name.setText("");
            txt_quali.setText("");
            txt_design.setText("");
            txt_salary.setText("");
        }
        if(st.equals("Exit"))
        {
            System.exit(0);
        }
        if(st.equals("Search"))
        {
            String se=txt_id.getText();
            try{
           
            Statement st1=con.createStatement();
            String query="Select *from employee where id='"+ se +"'";
            ResultSet rs=st1.executeQuery(query);
            while(rs.next())
            {
                txt_id.setText(rs.getString("id"));
                txt_name.setText(rs.getString("name"));
                txt_quali.setText(rs.getString("qualification"));
                txt_design.setText(rs.getString("designation"));
                txt_salary.setText(rs.getString("salary"));   
            }
            }
            catch(Exception ex)
            {
            }
        }
        if(st.equals("Delete"))
        {
            try{
            Statement st2=con.createStatement();
            String id1=txt_id.getText();
            String query2="Delete * from employee where id='"+id1+"'";
            st2.executeUpdate(query2);
            }
            catch(Exception ex)
            {
            }
            JOptionPane.showMessageDialog(null,"Successfully deleted");
            txt_id.setText("");
            txt_name.setText("");
            txt_quali.setText("");
            txt_design.setText("");
            txt_salary.setText("");
        }
        if(st.equals("Update"))
        {
            String s1=txt_id.getText();
            String s2=txt_name.getText();
            String s3=txt_quali.getText();
            String s4=txt_design.getText();
            String s5=txt_salary.getText();
            try{
               
                PreparedStatement ps=con.prepareStatement("Update employee set id=?,name=?,qualification=?,designation=?,salary=? where id=?");
                ps.setString(1,s1);
                ps.setString(2,s2);
                ps.setString(3,s3);
                ps.setString(4,s4);
                ps.setString(5,s5);
                ps.setString(6,s1);
                ps.executeUpdate();
                txt_id.setText("");
                txt_name.setText("");
                txt_quali.setText("");
                txt_design.setText("");
                txt_salary.setText("");
                JOptionPane.showMessageDialog(null,"Successfully updated");
            }
            catch(Exception ex)
            {
            }
        }
        if(st.equals("Save"))
        {   
           
            String s1=txt_id.getText();
            String s2=txt_name.getText();
            String s3=txt_quali.getText();
            String s4=txt_design.getText();
            String s5=txt_salary.getText();
            try{
               
                PreparedStatement ps=con.prepareStatement("insert into employee values(?,?,?,?,?)");
                ps.setString(1,s1);
                ps.setString(2,s2);
                ps.setString(3,s3);
                ps.setString(4,s4);
                ps.setString(5,s5);
                ps.executeUpdate();
                txt_id.setText("");
                txt_name.setText("");
                txt_quali.setText("");
                txt_design.setText("");
                txt_salary.setText("");
                JOptionPane.showMessageDialog(null,"Successfully inserted");
            }
            catch(Exception ex)
            {
            }
        }

    }
   
    public static void main(String args[])
    {
        EmpInformation emp=new EmpInformation();
        emp.setVisible(true);
        emp.addWindowListener(new WindowAdapter()
        {
              public void windowClosing(WindowEvent e)
            { 
                           System.exit(0);      
            }        
        });
        emp.setSize(new Dimension(600,500));
    }
}

Output

Employee Information System using JAVA and JDBC ODBC
Employee Information System using JAVA and JDBC ODBC

Tuesday, 30 October 2012

Digital clock java applet program

This is a java program to display Digital clock in an applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
/*
    <applet code="DigiClock"
        width=400
        height=300
    >
    </applet>
*/
public class DigiClock extends Applet implements Runnable
{
       public Date d;
       Thread t;
       public void init()
       {
           d=new Date();
        Font f=new Font("casteller",Font.ITALIC,56);
          setFont(f);
        t=new Thread(this);
        t.start();
         }
      public void run()
        {
           repaint();
           while(true)

Calculator using Java

Simple calculator using Java Frame

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

public class calculator extends Frame implements ActionListener
{
    public static Boolean first,operatorflag,resultflag,equaltoflag;
    public static double firstvalue;
    public static String operator;

    JButton   btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9;
    JButton btn_0,btn_plus,btn_minus,btn_mul,btn_div,btn_equal,btn_clear;
    JTextField txt_display;   

    public calculator()
    {
        first=false;
        operator="";
        operatorflag=false;
        resultflag=false;
        equaltoflag=false;

        btn_1=new JButton("1");
        btn_2=new JButton("2");
        btn_3=new JButton("3");
        btn_4=new JButton("4");
        btn_5=new JButton("5");
        btn_6=new JButton("6");
        btn_7=new JButton("7");
        btn_8=new JButton("8");
        btn_9=new JButton("9");
        btn_0=new JButton("0");
        btn_plus=new JButton("+");
        btn_minus=new JButton("-");
        btn_mul=new JButton("*");
        btn_div=new JButton("/");
        btn_equal=new JButton("=");
        btn_clear=new JButton("C");
       
        txt_display=new JTextField(20);
        txt_display.setEditable(false);

Change Background Color of an Applet

Dynamically Change Background Color of an applet using java scroll bar Control

/* Applet program to control the background color using scroll bars */
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

/*
    <APPLET    CODE="BackColor"
        HEIGHT=600
        WIDTH=600
    >
    </APPLET>
*/

public class BackColor extends Applet implements AdjustmentListener
{
    Scrollbar js_red,js_green,js_blue;
    Label lbl_red,lbl_green,lbl_blue;
    Color c;
    public void init()
    {
       
        js_red=new Scrollbar(Scrollbar.HORIZONTAL,10,2,0,256);
        js_green=new Scrollbar(Scrollbar.HORIZONTAL,10,2,0,256);
        js_blue=new Scrollbar(Scrollbar.HORIZONTAL,10,2,0,256);
        lbl_red=new Label("Red  "+" 0");
        lbl_green=new Label("Green "+"0");
        lbl_blue=new Label("Blue "+"0");               
        js_red.addAdjustmentListener(this);
        js_green.addAdjustmentListener(this);
        js_blue.addAdjustmentListener(this);
       
       
        js_red.setBounds(20,20,150,20);
        js_green.setBounds(20,50,150,20);
        js_blue.setBounds(20,80,150,20);
        lbl_red.setBounds(190,20,75,20);
        lbl_green.setBounds(190,50,75,20);
        lbl_blue.setBounds(190,80,75,20);
       
        Panel jp=new Panel();
        jp.setLayout(null);
        jp.add(js_red);
        jp.add(lbl_red);
        jp.add(js_green);
        jp.add(lbl_green);
        jp.add(js_blue);
        jp.add(lbl_blue);
        jp.setBounds(20,20,250,120);
       

        this.add(jp);

    }

Custome Exception Handling Using Java

This is a program to illustrate custom exception handling in java

import java.io.*;

class MyException extends Exception
{
    MyException(int i)
    {
        if(i==0)
            System.out.println("Division by zero");
    }
    MyException(int i,int j)
    {
        if(j>i)
            System.out.println("Dinominator greater than nominator");   
    }
}

class ExceptionDemo
{
    int a,b;
    public static void main(String args[])
    {
       
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        try
        {
        a=Integer.parseInt(br.readLine());
        b=Integer.parseInt(br.readLine());
       
            if(b==0)
                throw new MyException(a);
            else if(b>a)
                throw new MyException(a,b);
                             else
                System.out.println("Result= " + a/b );
        }
        catch(Exception ex)
        {
            System.out.println("Error " + ex.getMessage());
        }
    }
}