Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (2 votes)
3K views

Advanced Java Lab Manual

The document contains 12 programs related to Java programming. Program 1 demonstrates how to execute a SELECT query using JDBC. Program 2 shows how to update customer information in a database using JDBC. Program 3 contains a simple servlet that generates plain text output.

Uploaded by

K M Imtiaz Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (2 votes)
3K views

Advanced Java Lab Manual

The document contains 12 programs related to Java programming. Program 1 demonstrates how to execute a SELECT query using JDBC. Program 2 shows how to update customer information in a database using JDBC. Program 3 contains a simple servlet that generates plain text output.

Uploaded by

K M Imtiaz Uddin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 31

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:");
while (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" ) ;
for( SQLWarning warn = conn.getWarnings(); warn != null; warn =
warn.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:" ) ;

while( 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.
<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="checkbox.CheckTest" />
<jsp:setProperty name="foo" property="fruit" param="fruit" />
<hr>
Th checked fruits (got using request) are: <br>
<%
fruits = request.getParameterValues("fruit");
%>
<ul>
<%
if (fruits != null) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (util.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
<br>
<hr>
Th checked fruits (got using beans) are <br>
<%
fruits = foo.getFruit();
%>
<ul>
<%

if (!fruits[0].equals("1")) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (util.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
</font>
</body>
</html>

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();
}
while(!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);
}
while(!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.*;
/*<applet code=list1.class 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);
jlist.setVisibleRowCount(5);
jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlist.addListSelectionListener(this);
c.setLayout(new FlowLayout());
c.add(scp);
}
public void valueChanged(ListSelectionEvent ise)
{
String st =" Choose Item";
st+=jlist.getSelectedIndex();
showStatus(st);
}
}

OUTPUT:

PROGRAM 12
Purpose: Write a code for Applet.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet",20,20);
}
}

OUTPUT :

You might also like