Java

Variable Scope:


public class VarScopeProg
{
 public static void main(String[] args)
 {
   int A=10;
   {
     int B=20;
     System.out.println("Block A: "+A);
     System.out.println("Block B: "+B);
   }//scope of B=20 ends here
   {
   int B=30;//scope of B=30 starts here
   System.out.println("Block A: "+A);
   System.out.println("Block B: "+B);
   }//scope of B=30 ends here
   int B=40;//scope of B=40 starts here
   {
     System.out.println("Block A: "+A);
     System.out.println("Block B: "+B);

   }
 }//scope of all the variables end here
}

Use Of Class:

/*This is a Java program which shows the
use of class in the program
*/
class Rectangle
{
 int height;
 int width;
 void area()
 {
  int result=height*width;
  System.out.println(“The area is ”+result);
 }
}
class classDemo
{
 public static void main(String args[])
 {
  Rectangle obj=new Rectangle();
  obj.height=10;//setting the attribute values
  obj.width=20;//from outside of class
  obj.area();//using object method of class is called
 }
}

Type Casting

public class TypeCastProg
{
  public static void main(String[] args)
  {
     double x;
     int y;
     x=25.50;
     System.out.println("Value of [double] x: "+x);
     System.out.println("Conversion of double to integer");
     y=(int)x;
     System.out.println("Value of [integer] y: "+y);
     int m;
     double n;
     m=10;
     n=(double)m;
     System.out.println("Value of [integer] m: "+m);
     System.out.println("Conversion of integer to double");
     System.out.println("Value of [double] n: "+n);

  }
}

Exception Handling

class ExceptionDemo
{
 public static void main(String args[])
 {
   try
   { 
    int a,b;
    a=5;
    b=a/0;
   }
  catch(ArithmeticException e)
  {
   System.out.println("Divide by Zero\n");
  }
  System.out.println("...Executed catch statement");
 }
}

Sample Student form using jsp

Sample Student form using jsp


<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn=
DriverManager.getConnection("jdbc:mysql://localhost:3306/students","root","system");
out.write("Connected to mysql!!!");
stmt=conn.createStatement();
if(request.getParameter("action") != null)
{
String Studname=request.getParameter("Studname");
String Studaddress=request.getParameter("Studaddress");
String Studphone=request.getParameter("Studphone");
stmt.executeUpdate("insert into students_table(name,address,phone) values
(' "+Studname+" ',' "+Studaddress+" ',' "+Studphone+" ')" );
rs=stmt.executeQuery("select * from students_table");
%>
<html>
<body>
<center>
<h2>Student List</h2>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td><b>Roll No</b></td>
<td><b>Student Name</b></td>
<td><b>Address</.b></td>
<td><b>Phone</.b></td>
</tr>
  <%
int num=1;
while(rs.next()){
%>
<tr>
  <td><%=num%></td>
  <td><%=rs.getString("name")%></td>
  <td> <%=rs.getString("address")%></td>
  <td> <%=rs.getString("phone")%></td>
</tr>
<%
num++;
}
rs.close();
stmt.close();
conn.close();
%>
</table>
</center>
</body>
</html>
<%}else{%>
<html>
<head>
<title>Student Registartion Demo</title>
<script language="javascript">
    function validation(Form_obj)
{
if(Form_obj.Studname.value.length==0){
alert("Please,fill up the remaining information!!");
Form_obj.Studname.focus();
return false;
}
if(Form_obj.Studaddress.value.length==0){
alert("Please,fill up the remaining information!!");
Form_obj.Studaddress.focus();
return false;
}
if(Form_obj.Studphone.value.length==0){
alert("Please,fill up the remaining information!!");
Form_obj.Studphone.focus();
return false;
}
return true;
}
</script>
</head>
<body>
<center>
<form action="StudentForm.jsp" method="post"
name="entry" onSubmit="return validation(this)">
<input type="hidden" value="list" name="action">
<table border="3" cellpadding="0" cellspacing="0">
<tr>
<td>
<table>
         <tr>
<td colspan="2" align="center">
<h2>Student Entry Form</h2></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Student Name:</td>
<td><input name="Studname" type= "text" size="50"></td>
</tr>
<tr>
<td>Address:</td>
<td><input name="Studaddress" type="text" size="50"></td>
</tr>

<td>Phone:</td>
<td><input name="Studphone" type="text" size="15"></td>
</tr>

<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%}%>

Creating a thread by using Runnable Interface

Creating a thread by using Runnable Interface


class Thread_demo implements Runnable
{
  String str,msg;
  Thread t;
  Thread_demo(String str)
  {
   msg=str;
   t=new Thread(this,str);
   t.start();//directs to the run method
  }
  public void run()//thread execution starts
  {
    for(int i=1;i<=5;i++)
    {
      System.out.println(msg);
    }
    try
    {
        Thread.sleep(1000);
    }
    catch(InterruptedException e)
    {
     System.out.println("Exception in Thread handling");
    }
   }
}
public class Many_Thread_runnable
{
  public static void main(String args[])
  {
    Thread_demo t1=new Thread_demo("First Thread");
    Thread_demo t2=new Thread_demo("Second Thread");
    Thread_demo t3=new Thread_demo("Third Thread");
  }

creating a three thread by extending the Thread class

 creating a three thread by extending the Thread class


/******************************************************
This is a Java program which creates a three thread
by extending the Thread class
*******************************************************/
class Thread_demo extends Thread
{
  String msg;
  Thread_demo(String str)
  {
   msg=str;
   start();//directs to the run method
  }
  public void run()//thread execution starts
  {
    for(int i=1;i<=5;i++)
    {
      System.out.println(msg);
    }
    try
    {
        Thread.sleep(1000);
    }
    catch(InterruptedException e)
    {
     System.out.println("Exception in Thread handling");
    }
   }
}
public class Many_Thread
{
  public static void main(String args[])
  {
    Thread_demo t1=new Thread_demo("First Thread");
    Thread_demo t2=new Thread_demo("Second Thread");
    Thread_demo t3=new Thread_demo("Third Thread");
  }

Applet Program in java

Applet Program in java


import java.awt.*;
import java.applet.*;
/*
<applet code="MethodDemo" width=350 height=200>
</applet>

*/

public class MethodDemo extends Applet
{
 String msg;
 public void init()
 {
  msg="init method: ";
 }
  public void start()
 {
  msg=msg+"start method: ";
 }
 public void stop()
 {
  msg=msg+"stop method: ";
 }
 public void destroy()
 {
  msg=msg+"destroy method: ";
 }
 public void paint(Graphics g)
 {
  msg=msg+"paint method: ";
  g.drawString(msg,50,30);
 }

Servlet Life Cycle


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

public class LifeCycle extends GenericServlet
{
    public void init(ServletConfig config) throws ServletException
   {
        System.out.println("init");
    }

    public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException
     {
        System.out.println("from service");
        PrintWriter out = response.getWriter();
        out.println("Twinkle Twinkle Little Stars.");
        out.print("How I wonder what you are.");
    }
    public void destroy()
   {
        System.out.println("destroy");
    }
 }



No comments:

Comment

Comment Box is loading comments...