Advanced Java File
Advanced Java File
Advanced Java File
1.
JDBC_Connection_Demo.java
2.
Write a program to implement JSP.
4.
Program to show validation of user using servlet.
try
{
conn = DriverManager.getConnection(connectionUrl, dbUser, dbPwd);
Statement stmt = conn.createStatement();
// INSERT A RECORD
stmt.executeUpdate("INSERT INTO EXPTABLE (NAME) VALUES (\"TINU K\")");
System.out.println("ID \tNAME");
System.out.println("============");
while(rs.next())
{
System.out.print(rs.getInt("id") + ".\t" + rs.getString("name"));
System.out.println();
}
if (conn != null)
{
conn.close();
conn = null;
}
}
catch (SQLException sqle)
{
System.out.println("SQL Exception thrown: " + sqle);
}
}
} //JDBC_Connection_Demo ends here
OUTPUT:
------
ID NAME
============
1. ANUSHKA K
2. GARVITA K
3. TINU
PROGRAM NO: 2
WRITE A PROGRAM TO IMPLEMENT JSP
Step 1) Create a folder in Tomcat’s webapps folder, say examples.
Step 2) Write the following program and save like an html file, with extension jsp, say
“time.jsp” in the above created folder
<html>
<head>
<title> Current TIme
</title>
</head>
<body>
<table align='center' >
<tr>
<td>
<b>Current Date and Time</b>
</td>
<td>
<%=new java.util.Date()%>
</td>
</tr>
</table>
</body>
</html>
Step 3) Start the Tomcat Service Runner in “C:\Program Files\Apache Software Foundation\
Tomcat 6.0\bin”
Step 4) Open the web browser and type the following URL to run the servlet from any client system:
http://localhost:8080/examples/time.jsp
or
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception i)
{
System.out.println(i);
}
}
}
OUTPUT:
PROGRAM NO: 4
RequestServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
public class RequestServlet extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res)throws
ServletException,lOException
{
res.setContentType("textƒhtml");
PrintWriter out=res.getWriter();
out.println("<html><body>");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
if(name.equals("ABCDE")&& pass.equals("123456"))
out.println("user is valid through service method");
else
out.println("user is invalid through service method");
out.println("<ƒbody><ƒhtml>");
}
Login.html
<html>
<body>
<form action="run3" method="get">
enter your name:
<input type="text" name="name"ƒ>
<br>
enter your password:
<input type="password" name="pass"ƒ>
<br>
<input type="submit"ƒ>
<ƒbody>
<ƒhtml>
Web.xml
<web−app>
<servlet>
<servlet−name>RequestServlet<ƒservlet−name>
<servlet−class>RequestServlet<ƒservlet−class>
<ƒservlet>
<servlet−mapping>
<servlet−name>RequestServlet<ƒservlet−name>
<url−pattern>ƒrun3<ƒurl−pattern>
<ƒservlet−mapping>
<ƒweb−app>
OUTPUT :
” Mozilla Firefox
File Edit View History Bookmarks 5crapBook Tools Help
.g http:}jocahost:7001}s1run3?namz .
hCCp://localhost:...abcde&pass=abcde —
Done
PROGRAM NO: 5
RequestDispatcher2.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class RequestDispatcher2 extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws
ServletException,lOException
{
res.setContentType("textƒhtml");
PrintWriter out=res.getWriter();
out.println("<html><body>");
String s="before dispatcher";
out.println(s);
try
{
RequestDispatcher rd=req.getRequestDispatcher("ƒrun2");
rd.include(req,res);
}
catch(Exception e)
{
out.println("exception");
}
out.println("after dispatcher");
out.println("<ƒbody><ƒhtml>");
}
}
DemoServlet2.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet2 extends GenericServlet
{
public void service(ServletRequest req, ServletResponse res)throws
ServletException,lOException
{
res.setContentType("textƒhtml");
PrintWriter out=res.getWriter();
out.println("<html><body>");
out.println("hello servlet with GenericServlet");
out.println("<ƒbody><ƒhtml>");
}
}
Web.xml
<web−app>
<servlet>
<servlet−name>RequestDispatcher2<ƒservlet−name>
<servlet−class>RequestDispatcher2<ƒservlet−class>
<ƒservlet>
<servlet−mapping>
<servlet−name>RequestDispatcher2<ƒservlet−name>
<url−pattern>ƒrun7<ƒurl−pattern>
<ƒservlet−mapping>
<servlet>
<servlet−name>DemoServlet2<ƒservlet−name>
<servlet−class>DemoServlet2<ƒservlet−class>
<ƒservlet>
<servlet−mapping>
<servlet−name>DemoServlet2<ƒservlet−name>
<url−pattern>ƒrun2<ƒurl−pattern>
<ƒservlet−mapping>
<ƒweb−app>
OUTPUT:
PROGRAM NO: 6
AIM: Program to design simple Login page using Struts.
Login.jsp
User.java
package net.codejava.struts;
public class User {
private String email;
private String password;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
LoginAction.java
package net.codejava.struts;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Action(
value = "login",
results = {
@Result(name = "success", location = "/WEB-INF/jsp/Login.jsp")
}
)
public class LoginAction extends ActionSupport {
// empty
}
DoLoginAction.java
package net.codejava.struts;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import com.opensymphony.xwork2.ActionSupport;
@Action(
value = "doLogin",
results = {
@Result(name = "input", location = "/WEB-INF/jsp/Login.jsp"),
@Result(name = "success", location = "/WEB-INF/jsp/Welcome.jsp")
}
)
public class DoLoginAction extends ActionSupport {
private User user;
public String execute() {
if ("nimda".equals(user.getPassword())) {
return SUCCESS;
} else {
return INPUT;
}
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Welcome.jsp
input.jsp
<%@taglibprefix="s"uri="/struts-tags"%>
<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<met ahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Struts2 beginner example application</title>
</head>
<body>
<center>
<h2>Calculate sum of two numbers</h2>
<s:form action="calculateSumAction"method="post">
<s:textfield name="x"size="10"label="Enter X"/>
<s:textfield name="y"size="10"label="Enter Y"/>
<s:submit value="Calculate"/>
</s:form>
</center>
</body>
</html>
Package net.codejava.struts;
Import com.opensymphony.xwork2.ActionSupport;
Result.jsp
<%@taglibprefix="s"uri="/struts-tags"%>
<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Sum Result</title>
</head>
<body>
Sum of <s:property value="x"/>
and <s:property value="y"/>
is:
<s:property value="sum"/>
</body>
</html>
PROGRAM NO: 8
package com.mycompany;
import java.io.Serializable;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
create
database
feedback
db;
create
table
GuestBo
ok(
vno int PRIMARY KEY AUTO_INCREMENT,
vname
varchar(5
0), msg
varchar(1
00), mdate
varchar(5
0)
)
GuestBookBean.java package mypack;
import
javax.persistence.*;
@Entity
@Table(name="gues
tbook")
public class GuestBookBean implements
java.io.Serializable { @Id
@GeneratedValue
@Column(name="
vno") private
Integer visitorNo;
@Column(name="
vname") private
String visitorName;
@Column(name="
msg") private
String msg;
@Column(name="
mdate") private
String msgDate;
public
GuestBookBean() {
}
public Integer getVisitorNo() { return
visitorNo; } public String getVisitorName()
{ return
visitorName; } public String getMsg() { return
msg; }
public String getMsgDate() { return msgDate; }
public void setVisitorNo(Integer vn) { visitorNo = vn ; }
public void setVisitorName(String vn) {
visitorName =
vn; } public void setMsg(String m) { msg =
m; }
public void setMsgDate(String md) { msgDate=md; }
}
Source packages →new → others→select category Hibernate →Hibernate
Configuration
Wizard
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/feedbackdb?
zeroDateTimeBehavior=co nvertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
--------------------------------
index.html----------------------------------------------------
<h1>Website Feedback Form for google.con </h1>
<form action="fb.jsp" >
PROGRAM NO: 10
package MyJdbc;
import java.sql.*;
import java.util.ArrayList; import java.util.Scanner;
break; case 2:
System.out.println("Name " +
" Contact no " + "Address " + "University Name");
ArrayList<UserDetails>arr=db.read();
if(arr.isEmpty()) System.out.println("Nothing to show."); else
for (UserDetailsur:arr
){
System.out.println(ur.name+"" + ur.contactno+" "+
ur.address+" " + ur.universityName+" ");
}
break; case 3 :
db.update();
break; case 4 :
System.out.print("Enter contact no. : "); s.nextLine();
String no=s.nextLine(); db.delete(no);
break; default:
System.out.println("Thank you");
}
}}
INSERTION
READ
UPDATING
BEFORE UDATING:
AFTER UPDATING
DELETING
AFTER DELETING