Friday, 13 March 2015

Deposit using multiple-threading

import java.io.*;
public class Deposit
       {
        static int balance = 1000;
  public static void main(String[ ] args)
   {
       PrintWriter out =
                          new PrintWriter(System.out,true);
       Account account = new Account(out);


           DepositThread first;
    DepositThread second;
    first = new DepositThread(account, 1000, "#1");
    second = new DepositThread(account, 1000, "\t\t\t\t#2");
           first.start( );
           second.start( );
           // wait for both the threads to finish


         try
       {
       first.join( );
       second.join( );
       } catch(InterruptedException e)
             {
             }
        out.println("Final balance is :" +balance);
        }
    }


class Account
       {
       PrintWriter out;
       public Account(PrintWriter out)
             {
       this.out = out;
       }

 synchronized public void deposit(int amount, String name)
             {
              int balance;

          
            out.println(name+" trying to deposit" +amount); 
       
              
                balance = getBalance();
out.println(name +" balance got is" +balance);   
       
           
             setBalance(balance+amount);
        
    out.println(name+" new balance set to "+ Deposit.balance);
     }
 
public int getBalance( )
               {
                try
                       {
                       Thread.sleep(5000);
                        } catch(InterruptedException e)
                                {
                                 }
                    return Deposit.balance;
                    }

public void setBalance(int balance)
             {
              try
            {
            Thread.sleep(5000);
             } catch(InterruptedException e)
                                      {
                                      }
                      Deposit.balance=balance;
                      }
               }              


         
   
class DepositThread extends Thread
       {
       Account account;
       int amount;
       String message;
       public DepositThread(Account account,
                                int amount,String message)
               {
                this.account = account;
                this.amount = amount;
                this.message = message;
                }
public void run( )
    {
           account.deposit(amount, message);
           }
    }

Java Oracle Connection

import java.sql.*;
class OracleCon{
public static void main(String args[]){
try{
//load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
 
//create  the connection object
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","System","root");
 
//create the statement object
Statement stmt=con.createStatement();
 
//execute query
ResultSet rs=stmt.executeQuery("select * from STUDENTS");
while(rs.next())
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3)+"   "+rs.getString(4)+"  "+rs.getString(5));
 
//close the connection object
con.close();
 
}
catch(Exception e)
{ System.out.println(e);
}
 
}
} 

Simple Login in Applet

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/*
<applet code="LoginForm" height=800 width=500>
</applet>
*/

public class LoginForm extends Applet implements ActionListener
{
    Label l1,l2,l3;
    TextField t1,t2;
    Button b1;

    public void init()
    {
        l1=new Label("User Name :");
        add(l1);
       
        l2=new Label("Password  :");
        add(l2);
       
        l3=new Label();
        add(l3);
       
        t1=new TextField(20);
        add(t1);
       
        t2=new TextField(20);
        t2.setEchoChar('*');
        add(t2);
       
        b1=new Button("LogIn");
        add(b1);

        b1.addActionListener(this);
       
    }
   
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSourse()==b1)
        {
            l3.setText("Welcome "+t1.getText());
        }
    }
}

Saturday, 28 February 2015

Program for jsp forward

index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<jsp:forward page="welcome.jsp">
<jsp:param value="anant mahale" name="name"/>
</jsp:forward>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>Welcome to welcome to jsp</h2>
 </body>
</html>


Program for jsp include

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<h2>This is Index page</h2>
<jsp:include page="welcome.jsp"></jsp:include>

</body>
</html>


welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Today's Date</title>
</head>
<body>
<h2> Welcome page included :) </h2>
</body>
</html>




Program for jsp usebean

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Index</title>
</head>
<body>
<form action="welcome.jsp" method="post">
<h3>Enter your user name and password</h3>
Name <input type="text" name="uname"/><br>
password <input type="password" name="pass"/><br>
<input type="submit" value="Register" />
</form>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Process</title>
</head>
<body>
<jsp:useBean id="t" class="com.getset.Info"></jsp:useBean>
<jsp:setProperty property="*" name="t"/>

Your User Name and password is<br>
<jsp:getProperty property="uname" name="t"/><br>
<jsp:getProperty property="pass" name="t"/>
</body>
</html>

info.java

package com.getset;

public class Info {
   
    String uname,pass;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

}