advanced java lab manual

Download Advanced Java Lab Manual

If you can't read please download the document

Upload: k-m-imtiaz-uddin

Post on 19-Dec-2015

33 views

Category:

Documents


3 download

DESCRIPTION

java

TRANSCRIPT

Document

LAB MANUAL

OF

ADVANCE JAVA

PROGRAM 1

Purpose: A Program to execute select query using JDBC

import java.sql.*;

class SelectFromPer {

public static void main(String argv[]) {

try {

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:SSPer";

Connection con = DriverManager.getConnection(url,"North","Ken");

Statement stmt = con.createStatement();

R

sultSet rs = stmt.executeQuery("SELECT

Surname,FirstName,Category FROM student");

System.out.println("Class is SelectFromPer\n");

System.out.println("Found row:");

w

h

ile (rs.next()) {

String Surname = rs.getString(1);

String FirstName = rs.getString(2);

int Category = rs.getInt(3);

System.out.print (" Surname=" + Surname);

System.out.print (" FirstName=" + FirstName);

System.out.print (" Category=" + Category);

System.out.print(" \n");

}

stmt.close();

con.close();

}

catch (java.lang.Exception ex) {

x.printStackTrace();

}

}

}

PROGRAM 2

Purpose: A Program to Update Customer Information.

import java.sql.* ;

class JDBCUpdate

{

public static void main( String args[] )

{

try

{

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ) ;

Connection conn = DriverManager.getConnection(

"

jdbc:odbc:Database" ) ;

fo

r( SQLWarning warn = conn.getWarnings(); warn != null; warn =

w

arn.getNextWarning() )

{

System.out.println( "SQL Warning:" ) ;

System.out.println( "State : " + warn.getSQLState() ) ;

System.out.println( "Message: " + warn.getMessage() ) ;

System.out.println( "Error : " + warn.getErrorCode() ) ;

}

Statement stmt = conn.createStatement() ;

int rows = stmt.executeUpdate( "UPDATE Cust SET CUST_NO = 9842

WHERE CUST_NO = 9841" ) ;

System.out.println( rows + " Rows modified" ) ;

stmt.close() ;

conn.close() ;

}

catch( SQLException se )

{

System.out.println( "SQL Exception:" ) ;

w

h

ile( se != null )

{

System.out.println( "State : " + se.getSQLState() ) ;

System.out.println( "Message: " + se.getMessage() ) ;

System.out.println( "Error : " + se.getErrorCode() ) ;

se = se.getNextException() ;

}

}

catch( Exception e )

{

System.out.println( e ) ;

}

}

}

PROGRAM 3

Purpose: A simple servlet that just generates plain text.

package hall;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

PrintWriter out = response.getWriter();

out.println("hello world");

}

}

OUTPUT:

PROGRAM 4

Purpose: A Program which displays cookie id.

Import java.io.*;

Import javax.servlet.*;

Import javax.servlet.http.*;

public class CookieExample extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse

response)

throws IOException, ServletException

{

response.setContentType("text/html);

PrintWriter out = response.getWriter();

Cookie[] cookies = request.getCookies();

for (int i = 0; i < cookies.length; i++) {

Cookie c = cookies[i];

String name = c.getName();

String value = c.getValue();

out.println(name + " = " + value);

}

String name = request.getParameter("cookieName);

if (name != null && name.length() > 0) {

String value = request.getParameter("cookieValue");

Cookie c = new Cookie(name, value);

response.addCookie(c);

}

}

}

PROGRAM 5

Purpose : A program for basic arithmetic functions.

JSP 2.0 Expression Language - Basic Arithmetic

JSP 2.0 Expression Language - Basic Arithmetic

T

h

is example illustrates basic Expression Language arithmetic.

Addition (+), subtraction (-), multiplication (*), division (/ or div),

and modulus (% or mod) are all supported. Error conditions, like

division by zero, are handled gracefully.


EL Expression

Result

\${1}

${1}

\${1 + 2}

${1 + 2}

\${1.2 + 2.3}

${1.2 + 2.3}

\${1.2E4 + 1.4}

${1.2E4 + 1.4}

\${-4 - 2}

${-4 - 2}

\${21 * 2}

${21 * 2}

\${3/4}

${3/4}

\${3 div 4}

${3 div 4}

\${3/0}

${3/0}

\${10%4}

${10%4}

\${10 mod 4}

${10 mod 4}

\${(1==2) ? 3 : 4}

${(1==2) ? 3 : 4}

OUTPUT:

PROGRAM 6

Purpose: A Program to display a String

JSP 2.0 Examples - Hello World SimpleTag Handler

JSP 2.0 Examples - Hello World SimpleTag Handler

This tag handler simply echos "Hello, World!" It's an example of

a very basic SimpleTag handler with no body.


Result:

OUTPUT:

PROGRAM 7

Purpose :A Program to create check boxes.

T

h

checked fruits (got using request) are:


T

h

checked fruits (got using beans) are

PROGRAM 8

Purpose: A program to generate plain text

import java.awt.Color;

import java.beans.XMLDecoder;

import javax.swing.JLabel;

import java.io.Serializable;

public class SimpleBean extends JLabel implements Serializable

{

public SimpleBean()

{

setText( "Hello world!" );

setOpaque( true );

setBackground( Color.RED );

setForeground( Color.YELLOW );

setVerticalAlignment( CENTER );

setHorizontalAlignment( CENTER );

}

}

PROGRAM 9

Purpose:

Write server side and client side program.

// Client side Program.

import java.io.*;

import java.net.*;

import java.util.*;

public class MyClient

{

Socket s;

DataOutputStream dout;

public MyClient()

{

try

{

s=new Socket("127.0.0.1",1889);

dout = new DataOutputStream(s.getOutputStream());

System.out.println("Enter the data to send to server");

clientChat();

}

catch(IOException ioe)

{

System.out.println(ioe);

}

catch(Exception e)

{

System.out.println(e);

}

}

public void clientChat() throws IOException

{

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

String st;

do

{

st=br.readLine();

dout.writeUTF(st);

dout.flush();

}

w

h

ile(!st.equals("stop"));

}

public static void main(String args[])

{

new MyClient();

}

}

// Server side program.

import java.io.*;

import java.net.*;

import java.util.*;

public class MyServer

{

ServerSocket ss;

Socket s;

DataInputStream dis;

public MyServer()

{

try

{

System.out.println("server started/n waiting for client requrst.....");

ss=new ServerSocket(1889);

s=ss.accept();

System.out.println("client connected");

dis =new DataInputStream(s.getInputStream());

serverChat();

}

catch(IOException ioe)

{

System.out.println(ioe);

}

catch(Exception e)

{

System.out.println(e);

}

}

public void serverChat() throws IOException

{

String str;

do

{

str=dis.readUTF();

System.out.println("client message : : " + str);

}

w

h

ile(!str.equals("stop"));

}

public static void main(String args[])

{

new MyServer();

}

}

OUT

PUT

PROGRAM 10

Purpose:

Program to perform RMI.

//Implementing the remote interface

import java.rmi.*;

public interface Calculator extends java.rmi.Remote

{

public long add(long a,long b) throws RemoteException;

public long sub(long a,long b) throws RemoteException

}

// implementation of remote class

import java.rmi.*;

class CalculatorImpl extends java.rmi.server.UnicastRemoteObject

implements Calculator

{

public CalculatorImpl() throws java.rmi.RemoteException

{

super();

}

public long add(long a,long b) throws java.rmi.RemoteException

{

return a+b;

}

public long sub(long a,long b) throws java.rmi.RemoteException

{

return a-b;

}

}

//Generation of stub class

by typing rmic CalculatorImpl in command prompt

// Implementing Server class & Binding Remote class Object.

import java.rmi.Naming;

public class CalculatorServer

{

public CalculatorServer()

{

try

{

Calculator c = new CalculatorImpl();

Naming.rebind("rmi://localhost:1099/CalculatorService",c);

}

catch(Exception e)

{

System.out.println(e);

}

public static void main(String args[])

{

new CalculatorServer();

}

}

OUTPUT:

PROGRAM 11

Purpose:

Write code for Java Swing component like JFrame, JLabel,

JComponent, JList.

import java.awt.*;

import javax.swing.*;

import javax.swing.event.*;

/*

*/

public class list1 extends JApplet implements ListSelectionListener

{

JList jlist;

public void init()

{

Container c = getContentPane();

String[] str = new String[15];

fo

r(int i=0;i