Wednesday, April 6, 2011

A Sample Multithreading Program


Thread can be created 2 ways.
1. our thread name should be extends Thread
2. our thread name should be implements Runnable Interface

here the following program 2 threads are there.Mythread1,Mythread2 respectively..these two's are extends Thread.Thread is born when the our thread is created when the start() method calling on the particural thread object..

class Mythread1 extends Thread{
public void run(){
for(int i=1;i<=30;i++)

System.out.println(i);

}
}
class Mythread2 extends Thread{
public void run(){
for(int i=31;i<=50;i++)
System.out.println(i);
}
}
class MythreadTest{
public static void main(String args[]) throws Exception {
Mythread1 t1=new Mythread1();
Mythread2 t2=new Mythread2();
t2.start();
t1.start();

}
}

Sample Program for How to create User Defined Exception in Java Application


class MyException extends Exception{
}
class voting{
public static void isEligible(int age) throws MyException{
if(age<19)
throw new MyException();
else
System.out.println("can caste vote");
}
}
class MyTest{
public static void main(String args[]){
try{
int age=Integer.parseInt(args[0]);
voting.isEligible(age);
}
catch(MyException e){
e.printStackTrace();
System.out.println("Invalid Age can't vote");
}
catch(NumberFormatException e){
e.printStackTrace();
System.out.println("please supply age must be digits");
}
catch(Exception e){
e.getMessage();
}
}
}

Without Handling Exception in Sample Java Program


When the following program is compiling it is not giving any error.compiled Successfully.At the time of execution it Contains ArithematicException logica error in the program.

class WithoutHandle{
public static void main(String args[])
{
System.out.println("started");
int n,d,q=1;
n=Integer.parseInt(args[0]);
d=Integer.parseInt(args[1]);
q=n/d;
System.out.println("the result");
System.out.println("complete");
}
}

How to handle ArithematicException in Java


class WithHandle{
public static void main(String args[])
{
System.out.println("started");
int n,d,q=1;
n=Integer.parseInt(args[0]);
d=Integer.parseInt(args[1]);
try{
q=n/d;
System.out.println("the result value is"+q);
}
catch(ArithmeticException e){
System.out.println("Divisible fails...2nd value shouldn't be 0");
}
}
}

Sunday, March 20, 2011

Data Represent in the form User-defined Object (Bean)



public class Student {
private String studentNo;
private String studentName;
private float studentFee;

public float getStudentFee() {
return studentFee;
}
public void setStudentFee(float studentFee) {
this.studentFee = studentFee;
}
public String getStudentNo() {
return studentNo;
}
public void setStudentNo(String studentNo) {
this.studentNo = studentNo;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}

}

Thursday, March 3, 2011

Servlets- Inserting a Employee Details by using the PreparedStatement()


/*Before Executing dis program you should create Employee Table in the database And "emp.html" with eno,ename,salary field......*/

package com.edu.servlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class EmployeeServlet extends HttpServlet{
Connection con;
PreparedStatement ps;
public void init(ServletConfig config) throws ServletException{
String driver=config.getInitParameter("driver");
String cs=config.getInitParameter("url");
String usr=config.getInitParameter("user");
String pwd=config.getInitParameter("pwd");
 try
  {
  Class.forName(driver);
  con=DriverManager.getConnection(cs,usr,pwd);
  ps=con.prepareStatement("Insert into employee values(?,?,?)");
  }
 catch(ClassNotFoundException e){
 e.printStackTrace();
 }
 catch(SQLException e)
 {
 e.printStackTrace();
 }
}//init()
public void destroy(){
try
  {
   if(ps!=null)
     ps.close();
   if(con!=null)
     con.close();
  }
catch(SQLException e)
  {
  e.printStackTrace();
  }
  }
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
 String a=request.getParameter("empno");
 String name=request.getParameter("name");
 String c=request.getParameter("salary");
 int eno=Integer.parseInt(a);
 float salary=Float.parseFloat(c);
 try{
 ps.setInt(1,eno);
 ps.setString(2,name);
 ps.setFloat(3,salary);
 ps.executeUpdate();
 }catch(SQLException e)
 {
  e.printStackTrace();
 }
 response.sendRedirect("emp.html");
 }//doPost()
}//EmployyeeServlet class

HelloWorld Application By Using Servlets.......


package com.edu.servlets;
import javax.servlet.*;
import java.io.*;
public class HelloWorldServlet extends Servlet
throws ServletException,IOException{
public HelloWorldServlet(){
System.out.println("Construstor");
}

public  init(ServletConfig config){
System.out.println("init() initialised");
}
public destroy(){
System.out.println("destroy() initialised");
}
public service(ServletRequest request,ServletResponse response){
//request.getParameter("t");
//response.getParameter(
System.out.println("Hello World");
}
ServletConfig(getServletConfig config)
{
return "some thing";
}
ServletInfo(getServletInfo info)
{
return null;
}
}