Java Programming (BCSE 3074) Lab File: JULY 2020
Java Programming (BCSE 3074) Lab File: JULY 2020
Java Programming (BCSE 3074) Lab File: JULY 2020
Submitted by
JULY ‘2020
Create a table USER with above details using SQL commands. Establish JDBC
connection and insert 3 records into the table USER as follows using a java
program :
6 For the same table created in Program 5, display all the records existing in the
table USER using java program. 9
7 Using the table of Program 5, change the UserType for Ganesh to Admin
11
12 Write a simple JSP program to print the current date and time.
21
Aim: Write a Java program to create an ArrayList, add all the months of a year and print the
same.
CODE:
import java.util.*;
class Exp1{
public static void main(String args[]){
ArrayList<String> alist=new ArrayList<String>();
alist.add("Jan");
alist.add("Feb");
alist.add("Mar");
alist.add("Apr");
alist.add("May");
alist.add("Jun");
alist.add("Jul");
alist.add("Aug");
alist.add("Sep");
alist.add("Oct");
alist.add("Nov");
alist.add("Dec");
//displaying elements
System.out.println(alist);
}
}
Output:
Aim: Create an ArrayList that can store only Strings. Create a printAll method that will print all
the elements of the ArrayList using an Iterator.
Code:
import java.util.*;
Output:
Aim: Write a program that will have a Vector which is capable of storing Employee objects. Use
an Iterator and enumeration to list all the elements of the Vector.
Code:
import java.util.Vector;
import java.util.Enumeration;
Output:
Aim: Develop a java class with a instance variable H1 (HashSet) add a method
saveCountryNames(String CountryName) , the method should add the passed country to a
HashSet (H1) and return the added HashSet(H1).
Develop a method getCountry(String CountryName) which iterates through the HashSet and
returns the country if exist else return null.
NOTE: You can test the methods using a main method.
Code:
import java.util.HashSet;
import java.util.Iterator;
while (it.hasNext()) {
if (it.next().equals(CountryName))
return CountryName;
}
return null;
}
public static void main(String[] args) {
Exp3 countries = new Exp3();
countries.saveCountryNames("India");
countries.saveCountryNames("USA");
countries.saveCountryNames("Pakistan");
countries.saveCountryNames("Bangladesh");
countries.saveCountryNames("China");
Output:
Aim:
Create a table USER with above details using SQL commands. Establish JDBC connection and insert 3
records into the table USER as follows using a java program :
Code:
mysql_tables.sql
create database SampleDB;
use SampleDB;
try {
Aim: For the same table created in Program 5, display all the records existing in the table USER
using java program.
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
try {
Aim: Using the table of program 5, change the usertype for Ganesh to Admin
Code:
import java.sql.*;
// Database credentials
static final String USER = "username";
static final String PASS = "password";
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
Output:
C:\>java JDBCExample
Connecting to a selected database...
Connected database successfully...
Inserting records into the table...
Inserted records into the table...
Goodbye!
Aim: Delete the row form table USER (Program 5) where the Name is Hari using a java
program.
Code:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
try {
Output:
Title: Write the servlet application to print the current date and time.
Code:
import java.io.*;
import javax.servlet.*;
Output:
Code:
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wishing message</title>
</head>
<body>
<a href = "http://localhost:2020/WishSrvApp/test">Get wishing</a>
</body>
</html>
WishApp.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class WishApp extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException,
ServletException
{
//set response content type
res.setContentType("text/html");
//get printWrite obj
PrintWriter pw = res.getWriter();
//write request processing logic to generate wish message
Calendar cal = Calendar.getInstance();
//get current hours of the day
int hour = cal.get(Calendar.HOUR_OF_DAY);//24 hrs format
//generate wish message
if(hour<12)
pw.println("Good Morning!!");
else if (hour < 16)
pw.println("Good afternoon");
else if(hour<20)
pw.println("Good evening");
web.xml
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>WishApp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>abc</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Output:
(When we click on the above link In the morning it displays following message.)
Code:
SessionDemo.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#e5f7c0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<h2 align=\"center\">Session Infomation</h2>\n" +
"<table border=\"1\" align=\"center\">\n" +
"<tr bgcolor=\"#eadf8c\">\n" +
"<th>Session info</th><th>value</th></tr>\n" +
"<tr>\n" +
" <td>id</td>\n" +
" <td>" + session.getId() + "</td></tr>\n" +
"<tr>\n" +
" <td>Creation Time</td>\n" +
" <td>" + createTime +
" </td></tr>\n" +
"<tr>\n" +
" <td>Time of Last Access</td>\n" +
" <td>" + lastAccessTime +
" </td></tr>\n" +
"<tr>\n" +
" <td>User ID</td>\n" +
" <td>" + userID +
" </td></tr>\n" +
"<tr>\n" +
" <td>Number of visits</td>\n" +
" <td>" + visitCount + "</td></tr>\n" +
"</table>\n" +
"</body></html>");
}
}
web.xml
<web-app>
<servlet>
<servlet-name>abc</servlet-name>
<servlet-class>SessionDemo</servlet-class>
</servlet>
<servlet-mapping>
Output:
Aim: Write a simple JSP program to print the current date and time.
Code:
test.jsp
<%@ page import="java.io.*,java.util.*" %>
<html>
<head><title>JSPApp</title></head>
<body>
<form>
<fieldset style="width:20%; background-color: #ccffeb;">
<legend><b><i>JSP Application<i><b></legend>
<h3>Current Date and Time is :</h3>
<% java.util.Date d = new java.util.Date();
out.println(d.toString()); %>
</fieldset>
</form>
</body>
</html>
web.xml
<web-app>
<servlet>
<servlet-name>xyz</servlet-name>
<jsp-file>/test.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>xyz</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Output:
Code:
auto-refresh.jsp
<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Auto Refresh</title>
</head>
<body>
<center>
<form>
<fieldset style="width:20%; background-color:#e6ffe6;">
<legend>Auto refresh</legend>
<h2>Auto Refresh Example</h2>
<%
// Set refresh, autoload time as 1 seconds
response.setIntHeader("Refresh", 1);
// Get current time
Calendar calendar = new GregorianCalendar();
String am_pm;
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
out.println("Crrent Time: " + CT + "\n");
%>
</fieldset>
</form>
</center>
</body>
</html>
web.xml
<web-app>
<servlet>
<servlet-name>xyz</servlet-name>
<jsp-file>/auto-refresh.jsp</jsp-file>
</servlet>
Output: