LAB MANUAL
OF
ADVANCE JAVA
PROGRAM 1
Purpose: A Program to execute select query using JDBC
import [Link].*;
class SelectFromPer {
public static void main(String argv[]) {
try {
[Link] ("[Link]");
String url = "jdbc:odbc:SSPer";
Connection con = [Link](url,"North","Ken");
Statement stmt = [Link]();
R sultSet rs = [Link]("SELECT
Surname,FirstName,Category FROM student");
[Link]("Class is SelectFromPer\n");
[Link]("Found row:");
while ([Link]()) {
String Surname = [Link](1);
String FirstName = [Link](2);
int Category = [Link](3);
[Link] (" Surname=" + Surname);
[Link] (" FirstName=" + FirstName);
[Link] (" Category=" + Category);
[Link](" \n");
}
[Link]();
[Link]();
}
catch ([Link] ex) {
[Link]();
}
}
}
PROGRAM 2
Purpose: A Program to Update Customer Information.
import [Link].* ;
class JDBCUpdate
{
public static void main( String args[] )
{
try
{
[Link]( "[Link]" ) ;
Connection conn = [Link](
"jdbc:odbc:Database" ) ;
for( SQLWarning warn = [Link](); warn != null; warn =
[Link]() )
{
[Link]( "SQL Warning:" ) ;
[Link]( "State : " + [Link]() ) ;
[Link]( "Message: " + [Link]() ) ;
[Link]( "Error : " + [Link]() ) ;
}
Statement stmt = [Link]() ;
int rows = [Link]( "UPDATE Cust SET CUST_NO = 9842
WHERE CUST_NO = 9841" ) ;
[Link]( rows + " Rows modified" ) ;
[Link]() ;
[Link]() ;
}
catch( SQLException se )
{
[Link]( "SQL Exception:" ) ;
while( se != null )
{
[Link]( "State : " + [Link]() ) ;
[Link]( "Message: " + [Link]() ) ;
[Link]( "Error : " + [Link]() ) ;
se = [Link]() ;
}
}
catch( Exception e )
{
[Link]( e ) ;
}
}
}
PROGRAM 3
Purpose: A simple servlet that just generates plain text.
package hall;
import [Link].*;
import [Link].*;
import [Link].*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = [Link]();
[Link]("hello world");
}
}
OUTPUT:
PROGRAM 4
Purpose: A Program which displays cookie id.
Import [Link].*;
Import [Link].*;
Import [Link].*;
public class CookieExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
[Link]("text/html);
PrintWriter out = [Link]();
Cookie[] cookies = [Link]();
for (int i = 0; i < [Link]; i++) {
Cookie c = cookies[i];
String name = [Link]();
String value = [Link]();
[Link](name + " = " + value);
}
String name = [Link]("cookieName);
if (name != null && [Link]() > 0) {
String value = [Link]("cookieValue");
Cookie c = new Cookie(name, value);
[Link](c);
}
}
}
PROGRAM 5
Purpose : A program for basic arithmetic functions.
<html>
<head>
<title>JSP 2.0 Expression Language - Basic Arithmetic</title>
</head>
<body>
<h1>JSP 2.0 Expression Language - Basic Arithmetic</h1>
<hr>
This 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.
<br>
<blockquote>
<code>
<table border="1">
<thead>
<td><b>EL Expression</b></td>
<td><b>Result</b></td>
</thead>
<tr>
<td>\${1}</td>
<td>${1}</td>
</tr>
<tr>
<td>\${1 + 2}</td>
<td>${1 + 2}</td>
</tr>
<tr>
<td>\${1.2 + 2.3}</td>
<td>${1.2 + 2.3}</td>
</tr>
<tr>
<td>\${1.2E4 + 1.4}</td>
<td>${1.2E4 + 1.4}</td>
</tr>
<tr>
<td>\${-4 - 2}</td>
<td>${-4 - 2}</td>
</tr>
<tr>
<td>\${21 * 2}</td>
<td>${21 * 2}</td>
</tr>
<tr>
<td>\${3/4}</td>
<td>${3/4}</td>
</tr>
<tr>
<td>\${3 div 4}</td>
<td>${3 div 4}</td>
</tr>
<tr>
<td>\${3/0}</td>
<td>${3/0}</td>
</tr>
<tr>
<td>\${10%4}</td>
<td>${10%4}</td>
</tr>
<tr>
<td>\${10 mod 4}</td>
<td>${10 mod 4}</td>
</tr>
<tr>
<td>\${(1==2) ? 3 : 4}</td>
<td>${(1==2) ? 3 : 4}</td>
</tr>
</table>
</code>
</blockquote>
</body>
</html>
OUTPUT:
PROGRAM 6
Purpose: A Program to display a String
<html>
<head>
<title>JSP 2.0 Examples - Hello World SimpleTag Handler</title>
</head>
<body>
<h1>JSP 2.0 Examples - Hello World SimpleTag Handler</h1>
<hr>
<p>This tag handler simply echos "Hello, World!" It's an example of
a very basic SimpleTag handler with no body.</p>
<br>
<b><u>Result:</u></b>
<mytag:helloWorld/>
</body>
</html>
OUTPUT:
PROGRAM 7
Purpose :A Program to create check boxes.
<html>
<body bgcolor="white">
<font size=5 color="red">
<%! String[] fruits; %>
<jsp:useBean id="foo" scope="page" class="[Link]" />
<jsp:setProperty name="foo" property="fruit" param="fruit" />
<hr>
Th checked fruits (got using request) are: <br>
<%
fruits = [Link]("fruit");
%>
<ul>
<%
if (fruits != null) {
for (int i = 0; i < [Link]; i++) {
%>
<li>
<%
[Link] ([Link](fruits[i]));
}
} else [Link] ("none selected");
%>
</ul>
<br>
<hr>
Th checked fruits (got using beans) are <br>
<%
fruits = [Link]();
%>
<ul>
<%
if (!fruits[0].equals("1")) {
for (int i = 0; i < [Link]; i++) {
%>
<li>
<%
[Link] ([Link](fruits[i]));
}
} else [Link] ("none selected");
%>
</ul>
</font>
</body>
</html>
PROGRAM 8
Purpose: A program to generate plain text
import [Link];
import [Link];
import [Link];
import [Link];
public class SimpleBean extends JLabel implements Serializable
{
public SimpleBean()
{
setText( "Hello world!" );
setOpaque( true );
setBackground( [Link] );
setForeground( [Link] );
setVerticalAlignment( CENTER );
setHorizontalAlignment( CENTER );
}
}
PROGRAM 9
Purpose: Write server side and client side program.
// Client side Program.
import [Link].*;
import [Link].*;
import [Link].*;
public class MyClient
{
Socket s;
DataOutputStream dout;
public MyClient()
{
try
{
s=new Socket("[Link]",1889);
dout = new DataOutputStream([Link]());
[Link]("Enter the data to send to server");
clientChat();
}
catch(IOException ioe)
{
[Link](ioe);
}
catch(Exception e)
{
[Link](e);
}
}
public void clientChat() throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader([Link]));
String st;
do
{
st=[Link]();
[Link](st);
[Link]();
}
while();
}
public static void main(String args[])
{
new MyClient();
}
}
// Server side program.
import [Link].*;
import [Link].*;
import [Link].*;
public class MyServer
{
ServerSocket ss;
Socket s;
DataInputStream dis;
public MyServer()
{
try
{
[Link]("server started/n waiting for client requrst.....");
ss=new ServerSocket(1889);
s=[Link]();
[Link]("client connected");
dis =new DataInputStream([Link]());
serverChat();
}
catch(IOException ioe)
{
[Link](ioe);
}
catch(Exception e)
{
[Link](e);
}
}
public void serverChat() throws IOException
{
String str;
do
{
str=[Link]();
[Link]("client message : : " + str);
}
while();
}
public static void main(String args[])
{
new MyServer();
}
}
OUT PUT
PROGRAM 10
Purpose: Program to perform RMI.
//Implementing the remote interface
import [Link].*;
public interface Calculator extends [Link]
{
public long add(long a,long b) throws RemoteException;
public long sub(long a,long b) throws RemoteException
}
// implementation of remote class
import [Link].*;
class CalculatorImpl extends [Link]
implements Calculator
{
public CalculatorImpl() throws [Link]
{
super();
}
public long add(long a,long b) throws [Link]
{
return a+b;
}
public long sub(long a,long b) throws [Link]
{
return a-b;
}
}
//Generation of stub class
by typing rmic CalculatorImpl in command prompt
// Implementing Server class & Binding Remote class Object.
import [Link];
public class CalculatorServer
{
public CalculatorServer()
{
try
{
Calculator c = new CalculatorImpl();
[Link]("rmi://localhost:1099/CalculatorService",c);
}
catch(Exception e)
{
[Link](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 [Link].*;
import [Link].*;
import [Link].*;
/*<applet code=[Link] width =300 height = 200>
</applet>*/
public class list1 extends JApplet implements ListSelectionListener
{
JList jlist;
public void init()
{
Container c = getContentPane();
String[] str = new String[15];
for(int i=0;i<15;i++)
{
str[i] = "Item_Selection"+(i+1);
}
jlist = new JList(str);
JScrollPane scp = new JScrollPane(jlist);
[Link](5);
[Link](ListSelectionModel.SINGLE_SELECTION);
[Link](this);
[Link](new FlowLayout());
[Link](scp);
}
public void valueChanged(ListSelectionEvent ise)
{
String st =" Choose Item";
st+=[Link]();
showStatus(st);
}
}
OUTPUT:
PROGRAM 12
Purpose: Write a code for Applet.
import [Link].*;
import [Link].*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
[Link]("A Simple Applet",20,20);
}
}
OUTPUT :