Advanced Java Programming
Advanced Java Programming
Advanced Java Programming
2
1.1 Installing NetBeans on Windows
1.2 Executing “Hello World” Java program
1.3 Establishing Connection to Oracle Database
1.4 Implementing Servlet progam
2 Experiment : JDBC Programs using Statement
2.1 A program to test the connection with the database
2.2 A program to create a table
2.3 A program to insert record in a table
2.4 A program to fetch records from a table
2.5 A program to update record in a table
2.6 A program to delete record from a table
3 Experiment : JDBC Programs using PreparedStatement
3.1 A program to insert a record and select records
3.2 A program to update a record
4 Experiment : JDBC Programs using CallableStatement
4.1 A program to execute a procedure to compute a square
5 Experiment : Servlet Programming
5.1 Servlet Execution on tomcat
5.2 A servlet program to print hello world
5.3 A servlet program to display request details
5.4 A servlet program to handle user form
5.5 A servlet program to create a cookie
5.6 A servlet program to display cookie
5.7 A servlet program to do session tracking
6 Experiment : JSP Programming
6.1 JSP program to display hello world.
6.2 JSP program to demonstrate arithmetic operations
6.3 JSP program to demonstrate jsp:forward action tag
6.4 JSP program to request implicit object
6.5 Developing a web application to insert record into Oracle Database using JSP and
JDBC
7 Experiment : JSTL Programs
7.1 Write a JSTL program to demonstrate core tags
7.2 Write a JSTL program to find the given Number is Even OR Odd using CORE Tags
in JSTL
7.3 Write a JSTL program to demonstrate IF Statement using Core Tags
7.4 Write a JSTL program to demonstrate Nested IF Statement Using Core Tags
7.5 Write a JSTL program to demonstrate XML tags to parse an XML document
7.6 Write a JSTL program to demonstrate XML tags to read an XML document
Experiment 1: Working with NetBeans 8.2
Objective: To learn the steps for the installation of NetBeans on Windows. Establish connection
with the database from NetBeans. Execute Java and Servlet programs on NetBeans.
1.1 Installing NetBeans on Windows: Steps to install NetBeans
1.2 Executing “Hello World” Java program: Steps to execute “Hello World” program on
NetBeans
For each Java application, you need to create a "project" to keep all the source files, classes and
relevant resources.
1. From "File" menu ⇒ Choose "New Project...”.
2. The "Choose Project" dialog pops up ⇒ Under "Categories", choose "Java" ⇒ Under
"Projects", choose "Java Application" ⇒ "Next".
3. The "Name and Location" dialog pops up ⇒ Under "Project Name", enter "FirstProject"
⇒ In "Project Location", select a suitable directory to save your works ⇒ Uncheck "Use
Dedicated Folder for Storing Libraries" ⇒ Uncheck "Create Main class" ⇒ Finish.
1. Right-click on "FirstProject" ⇒ New ⇒ Java Class (OR choose the "File" menu ⇒ "New
File..." ⇒ Categories: "Java", File Types: "Java Class" ⇒ "Next").
2. The "Name and Location" dialog pops up ⇒ In "Class Name", enter "Hello" ⇒ Delete the
content in "Package" if it is not empty ⇒ "Finish".
3. The source file "Hello.java" appears in the editor panel. Enter the following codes:
Public class Hello {
Public static void main(String [] args) {
System.ou.println(“Hello World”);
}
}
Step 3: Compile & Execute
There is no need to "compile" the source code in NetBeans explicitly, as NetBeans performs the
so-called incremental compilation (i.e., the source statement is compiled as and when it is entered).
To run the program, right-click anywhere in the source (or from the "Run" menu) ⇒ Run File.
Observe the output on the output console.
Notes:
You should create a NEW Java project for EACH of your Java application.
Nonetheless, NetBeans allows you to keep more than one programs in a project. To run a
particular program, open and right-click on the source file ⇒ Run File.
1.3 Establishing a Connection to Oracle Database: steps to create a new connection to the
database.
1. Start the Oracle database.
2. Open the Services window (Window > Services). In the Services window, right-click the
Databases node and choose New Connection.
3. In the New Connection wizard, select Oracle Thin in the Driver dropdown list.
4. Click Add and locate the ojdbc6.jar file that you previously downloaded. Click Next.
5. In the Customize Connection panel of the wizard, enter the following values and click
Next.
Name Value
Username Enter the username that you used during database installation. The default
is “system”.
Password Enter the password for the selected username. The default is “manager”
6. Click Test Connection to confirm that the IDE is able to connect to the database. Click
Next. If the attempt is successful, the message "Connection succeeded" is displayed in the
wizard.
1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
2. Connection URL: The connection URL for the oracle10G database
is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin
is the driver, localhost is the server name on which oracle is running, we may also use IP
address, 1521 is the port number and XE is the Oracle service name. You may get all these
information from the tnsnames.ora file.
3. Username: The default username for the oracle database is system.
4. Password: It is the password given by the user at the time of installing the oracle database
1 A prepared statement is one that contains placeholders (in form question marks ?) for dynamic
values will be set at runtime. For example: SELECT * from Users WHERE user_id=?
Here the value of user_id is parameterized by a question mark and will be set by one of the
setXXX() methods from the PreparedStatement interface, e.g. setInt(int index, int value).
import java.sql.*;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
if (con != null) {
else {
con.close();
catch (Exception e) {
e.printStackTrace();
}// main
}// class
Experiment 2: JDBC Programs using Statement
2.2 A program to create a table
import java.sql.*;
Statement st = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
addr varchar2(10))";
st = con.createStatement();
st.executeUpdate(qry);
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}//main
}//class
Experiment 2: JDBC Programs using Statement
2.3 A program to insert record into the database
import java.sql.*;
Statement st =null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "manager");
st =con.createStatement();
int eid=662;
name = "'"+name+"'";
addr = "'"+addr+"'";
System.out.println(qry);
else {
st.close();
con.close();
catch (Exception e) {
e.printStackTrace();
}//main
}//class
Experiment 2: JDBC Programs using Statement
2.4 A program to update a record in the database
import java.sql.*;
Statement st = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
String qry = "update employee set addr = " + addr + " where eid = 662";
System.out.println(qry);
st = con.createStatement();
if (result != 0) {
} else {
st.close();
con.close();
} catch(Exception e) {
e.printStackTrace();
}//main
}//class
Experiment 2: JDBC Programs using Statement
2.4 A program to delete a record in the database
import java.sql.*;
Statement st = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
st = con.createStatement();
System.out.println(qry);
if (result != 0) {
else {
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}//main
}//class
Experiment 3: JDBC Programs using PreparedStatement
3.1 A program to insert a record and select records
import java.sql.*;
public class SelectPrepared {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "manager");
ps = con.prepareStatement("insert into employee values(?,?,?)");
// set values to query parameters
ps.setInt(1, 10);
ps.setString(2, "Alice");
ps.setString(3, "Hyd");
int result = ps.executeUpdate();
if (result != 0) {
System.out.println("Record is successfully inserted");
} else {
System.out.println("Inserting record is failed");
}
String qry = "select * from employee where ename = ?";
ps = con.prepareStatement(qry);
// set value to query parameter
ps.setString(1, "Alice");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t"
+ rs.getString(3));
}
// close jdbc objects
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}}}
Experiment 3: JDBC Programs using PreparedStatement
3.2 A Program to update records
import java.sql.*;
public class UpdatePrepared {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe", "system", "manager");
ps = con.prepareStatement("update employee set addr=? where eid=?");
// set values to query parameters
ps.setString(1, "Telangana");
ps.setInt(2, 10);
int result = ps.executeUpdate();
if (result != 0) {
System.out.println("RECORD IS SUCCEFULLY UPDATED");
} else {
System.out.println("RECORD IS FAILED TO UPDAE");
}
// selecting data from student
ps = con.prepareStatement("select * from employee where ename=?");
// set value to query parameter
ps.setString(1, "Alice");
rs = ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t"
+ rs.getString(3));
}
// close jdbc objects
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Experiment 4: JDBC Programs using CallableStatement
4.1 A program to execute a procedure to compute a square
Square procedure
create or replace procedure square(x IN number, y out number) is
BEGIN
y := x * x;
END;
/
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;
Class.forName("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe",
"system", "manager");
cStmt.setInt(1, 20);
cStmt.registerOutParameter(2, Types.INTEGER);
cStmt.execute();
cStmt.close();
}
}
Experiment 5: Servlet Programming
5.1 Servlet Execution on tomcat
Servlets Tomcat: Install Tomcat server of your choice version by downloading from Apache site.
Many versions of Tomcat can be found in the following link. https://tomcat.apache.org/download-
80.cgi
Check with the documentation of what JDK or JRE version is compatible to the
specific Tomcat version. For example I loaded Tomcat 5.0 and is compatible with JDK 6.0.
While installation, it asks the port number and I have entered 8089 (default is displayed as 8080. I
have not preferred 8080 for the reason on many systems Oracle server will be working on 8080.
For this reason better choose a different port number). Later, give your own password.
Step 1: When installed, Tomcat gives many folders of which a few are given hereunder used for
execution. See the following one.
C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib\servlet-api.jar;
Keep the above JAR file in the classpath of Windows Environment Variables; else the servlet
program will not be compiled.
Note: Now, a fresher should be careful here in the following steps. Steps are very simple but
should be followed carefully. Any small mistake committed, Tomcat simply refuses to execute
your servlet.
Step 2: Creating your own directory structure.
You also get the following folders.
C:\Program Files\Apache Software Foundation\Tomcat\webapps
In the above webapps folder create your own new folder xxx.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorldSrv extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
//set response content type
resp.setContentType("text/html");
//get PrintWriter object from response object
PrintWriter out = resp.getWriter();
out.println("<h1>Welcome to Servlet</h1>");
out.println("<h3>Hello World</h3>");
//close the stream
out.close();
}// doGet(-,-)
}// class
Web.xml
<web-app>
<servlet>
<servlet-name> HelloWorldSrv </servlet-name>
<servlet-class> HelloWorldSrv </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> HelloWorldSrv </servlet-name>
<url-pattern>/hello</url-pattern >
</servlet-mapping >
</web-app>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
resp.setContentType("text/html");
out.println("<h3>REQUEST INFORMATION</h3>");
}//doGet
}//class
Web.xml
<web-app>
<servlet>
<servlet-name> RequestInfoSrv</servlet-name>
<servlet-class> RequestInfoSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> RequestInfoSrv</servlet-name>
<url-pattern>/request</url-pattern >
</servlet-mapping >
</web-app>
Experiment 5: Servlet Programming
5.4 A servlet program to handle user forms
RequestParams.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>RequestParams</title>
</head>
<body>
<pre>
<h1>Request Parameters</h1>
</pre>
</form>
</body>
</html>
RequestParametersSrv
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RequestParametersSrv extends HttpServlet {
resp.setContentType("text/html");
out.close();
}//doGet
}//class
Web.xml
<web-app>
<servlet>
<servlet-name> RequestParametersSrv</servlet-name>
<servlet-class> RequestParametersSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> RequestParametersSrv</servlet-name>
<url-pattern>/RequestParametersSrv</url-pattern >
</servlet-mapping >
</web-app>
Experiment 5: Servlet Programming
5.5 A servlet program to create a cookie
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
resp.setContentType("text/html");
resp.addCookie(ck1);
resp.addCookie(ck2);
ck3.setMaxAge(1800);
ck4.setMaxAge(1800);
// add cookies
resp.addCookie(ck3);
resp.addCookie(ck4);
// generate Response
Web.xml
<web-app>
<servlet>
<servlet-name> SetCookiesSrv</servlet-name>
<servlet-class> SetCookiesSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> SetCookiesSrv</servlet-name>
<url-pattern>/cookie</url-pattern >
</servlet-mapping >
</web-app>
Experiment 5: Servlet Programming
5.6 A servlet program to display a cookie
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
resp.setContentType("text/html");
if (ck != null) {
}//doGet
}//class
Web.xml
<web-app>
<servlet>
<servlet-name> ShowCookiesSrv</servlet-name>
<servlet-class> ShowCookiesSrv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> ShowCookiesSrv</servlet-name>
<url-pattern>/showcookie</url-pattern >
</servlet-mapping >
</web-app>
<html>
<head>
<meta charset="ISO-8859-1">
<title>SessionTraking</title>
</head>
<body>
</pre>
</form>
</body>
</html>
Firstsrv.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
resp.setContentType("text/html");
ses.setAttribute("pname", pname);
ses.setAttribute("fname", fname);
out.println("</form>");
Secondsrv.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
resp.setContentType("text/html");
out.println("<pre><h1>SESSION TRACKING</h1>");
out.println("<h1>FORM1 DATA: Name: " + pname + " & " + "F_Name: "+fname
+ "<h1>");
out.println("<h1>FORM2 DATA: Income: " + income + " & " + "Tax: " +
tax+"</h1><pre>");
out.println("<a href='SessionTracking.html'><h4>Home</h4></a>");
Web.xml
<web-app>
<servlet>
<servlet-class>FirstSrv</servlet-class>
</servlet>
<servlet-mapping>
</servlet-mapping>
<servlet>
<servlet-class>SecondSrv</servlet-class>
</servlet>
<servlet-mapping>
</servlet-mapping>
</web-app>
Experiment 6 : JSP Programming
6.1 JSP program to display hello world.
<html>
<body>
<%
%>
</body>
</html>
Experiment 6 : JSP Programming
6.2 JSP program to demonstrate arithmetic operations\
Arithmetic.html
<html>
<body>
<form action="arithmetic.jsp">
</form>
</body>
</html>
Arithmetic.jsp
<%
int opr1=Integer.parseInt(request.getParameter("op1"));
int opr2=Integer.parseInt(request.getParameter("op2"));
int sum=opr1+opr2;
int diff=opr1-opr2;
int mul=opr1*opr2;
int div=opr1/opr2;
%>
<body>
</body>
</html>
printdate2.jsp
<html>
<body>
</body>
</html>
Experiment 6 : JSP Programming
6.4 JSP program to request implicit object
Implicitobj.html
<form action="implicitobj.jsp">
</form>
Implicitobj.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
Experiment 6 : JSP Programming
6.5 Developing a web application to insert record into Oracle Database using JSP and
JDBC
Insertrec.html
<html>
<head>
</head>
<body>
<form action="InsertRec">
</form>
</body>
</html>
insertrec.jsp
<%
int id = Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name").toString();
%>
<%Class.forName("oracle.jdbc.driver.OracleDriver");
%>
Connection Established
stat.executeUpdate("insert into emp values('" +id+ "','" +name +"','"+ sal+ "')");
%>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
</body>
</html>
Experiment 7 : JSTL Programs
7.2 Write a JSTL program to find the given Number is Even OR Odd using CORE Tags in
JSTL
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
</head>
<body>
<c:choose>
<c:when test="${num%2==0}">
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
</body>
</html>
Experiment 7 : JSTL Programs
7.3 Write a JSTL program to demonstrate IF Statement using Core Tags
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
</head>
<body>
</c:if>
</body>
</html>
Experiment 7 : JSTL Programs
7.4 Write a JSTL program to demonstrate Nested IF Statement Using Core Tags
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
</head>
<body>
<c:choose>
</c:when>
</c:when>
<c:otherwise>
Income is undetermined...
</c:otherwise>
</c:choose>
</body>
</html>
Experiment 7 : JSTL Programs
7.5 Write a JSTL program to demonstrate XML tags to parse an XML document
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>x:if Tags</title>
</head>
<body>
<h2>Vegetable Information:</h2>
<c:set var="vegetables">
<vegetables>
<vegetable>
<name>onion</name>
<price>40</price>
</vegetable>
<vegetable>
<name>Potato</name>
<price>30</price>
</vegetable>
<vegetable>
<name>Tomato</name>
<price>90</price>
</vegetable>
</vegetables>
</c:set>
</x:if>
</body>
</html>
Experiment 7 : JSTL Programs
7.6 Write a JSTL program to demonstrate XML tags to read an XML document
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html> <head>
<title>XML Tags</title></head>
<body>
<h2>Vegetable Information:</h2>
<c:set var="vegetable">
<vegetables>
<vegetable>
<name>onion</name>
<price>40/kg</price>
</vegetable>
<vegetable>
<name>Potato</name>
<price>30/kg</price>
</vegetable>
<vegetable>
<name>Tomato</name>
<price>90/kg</price>
</vegetable> </vegetables>
</c:set>
</body>