You are on page 1of 29

Program :- 1

Aim:- WAP to execute select query using JDBC.


AIM: Write a program to execute a select Query using JDBC.
Java

Database

Connectivity (JDBC)

is

an application

programming

interface (API) for the programming language Java, which defines how a client
may access a database. It is part of the Java Standard Edition platform, from Oracle
Corporation. It provides methods to query and update data in a database, and is
oriented

towards relational

connections

to

any

databases.

ODBC-accessible

JDBC-to-ODBC bridge

data

source

in

enables

the Java

virtual

machine (JVM) host environment.


Sun Microsystems released JDBC as part of Java Development Kit (JDK) 1.1 on
February 19, 1997. Since then it has been part of the Java Platform, Standard
Edition (Java SE).
The JDBC classes are contained in the Java package java.sql and javax.sql.
Starting with version 3.1, JDBC has been developed under the Java Community
Process. JSR 54 specifies JDBC 3.0 (included in J2SE 1.4), JSR 114 specifies the
JDBC Rowset additions, and JSR 221 is the specification of JDBC 4.0 (included in
Java SE 6).
JDBC 4.1, is specified by a maintenance release 1 of JSR 221 and is included in
Java SE 7.
The latest version, JDBC 4.2, is specified by a maintenance release 2 of JSR
221 and is included in Java SE 8.

Five Step to Connect a database in java:1. Register the Driver class


2. Creating Connection
3. Creating Statement
4. Execute Queries
5. Closing Connection

import java.sql.*;
class jdbc
{
public static void main(String as[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=
DriverManager.getConnection("jdbc:odbc:ranga");
Statement st=c.createStatement();
ResultSet r= st.executeQuery("select * from Ankur");
while(r.next())

{
System.out.println(r.getInt(1)+" "+r.getString(2)+"
"+r.getInt(3));
}
while(r.next())
{
System.out.println(r.getInt(1)+" "+r.getString(2)+"
"+r.getInt(3));
}
c.close();
}
catch(SQLException e)
{
System.out.println( Exception Handeled+e);
}
catch(Exception e)
{
System.out.println( Exception Handeled+e);
}
}
}

Output:-

Program :- 2
AIM: Write a program to execute a Update Query using delete command.
import java.sql.*;
class update
{
public static void main(String as[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c= DriverManager.getConnection("jdbc:odbc:babi");
Statement st=c.createStatement();
ResultSet r= st.executeQuery("select * from ranga");
int i=st.executeUpdate("delete * from Ankur where ID=2");
if(i==1)
{
System.out.println("Deleted");
c.commit();
}
System.out.println("After Updation");
r= st.executeQuery("select * from mayank");

while(r.next())
{
System.out.println(r.getInt(1)+" "+r.getString(2)+" "+r.getInt(3));
}
c.close();
}
catch(SQLException e)
{
}
catch(Exception e)
}

Output:-

Program :- 3

Aim:-A simple servlet that generate plane text.


<!DOCTYPE html PUBLIC "-//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>Simple Example</title>
</head>
<body>
<div>
<h1>Hey This is just an text</h1>
</div>
</body>
</html>

Output:-

Program :- 4

Aim:- WAP to add cookies.


Index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="http://localhost:8080/cookies/Addcookie">
Name: <input type="text" name="user">
Age : <input type="text" name="age">
<input type="submit" value="Submit Data">
</form>
</body>
</html>

Addcookies.java
package cookiesTest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Addcookie extends HttpServlet {
<code>GET</code> and <code>POST</code>
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String name = request.getParameter(user);
String age = request.getParameter(age);
Cookie ck1 = new Cookie("UserName", name);
Cookie ck2 = new Cookie("UserAge", age);
response.addCookie(ck1);

response.addCookie(ck2);
out.println("<a href=>Cookies Here Found</a>");
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the


+ sign on the left to edit the code.">

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

Output:-

Output after Submit:-

Program:- 5
Aim:- Jsp for Show current date and time.
<html>
<body>
<p>&nbsp;</p>
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="460"
bgcolor="#EEFFCA">
<tr>
<td width="100%"><font size="6" color="#008000">&nbsp;Date
Example</font></td>
</tr>
<tr>
<td width="100%"><b>&nbsp;Current Date and time is:&nbsp; <font
color="#FF0000">
<%= new java.util.Date() %>
</font></b></td>
</tr>
</table>
</center>
</div>
</body>
</html>

Output:-

Program:-6
Aim:- Jsp page for login
Index.jsp

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1 align="center"> LOGIN FORM </h1>
<form METHOD=POST ACTION="login.jsp">
Username : <input type="text" name="uname">
Password : <input type="password" name="password">
<input type="submit" value="LOGIN">
</form>

</body>
</html>

Login.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.*,javax.servlet.http.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<html lang="en">
<head>
<title>Welcome</title>
</head>

<body>
<sql:setDataSource
var="usr"
url="jdbc:mysql://localhost:3306/dc"
driver="com.mysql.jdbc.Driver"
user="root" password="system"
/>
<sql:query dataSource="${usr}" var="result1" >
select * from login where uname=?
<sql:param value="${param.uname}"/>
</sql:query>

<c:forEach var="row1" items="${result1.rows}">


<c:set var="flag1" scope="application" value="1"/>
</c:forEach>
<sql:query dataSource="${usr}" var="result2" >
select * from login where uname=? and password=?
<sql:param value="${param.uname}"/>
<sql:param value="${param.password}"/>
</sql:query>

<c:forEach var="row2" items="${result2.rows}">


<c:set var="flag2" scope="application" value="1"/>
</c:forEach>

<c:choose>
<c:when test="${flag2 eq 1}">
<c:forEach var="row2" items="${result2.rows}">
<c:set var="flag2" scope="application" value="1"/>
<h1>
Welcome !! ... <c:out value="${row2.uname}"/>

</c:forEach>
<c:set var="flag1" scope="application" value="0"/>

<c:set var="flag2" scope="application" value="0"/>


</c:when>
<c:when test="${flag1 eq 0}">
<h1>
User Not Registered ...
</h1>

<c:set var="user_flag" scope="application" value="0"/>


<c:set var="flag" scope="application" value="0"/>
</c:when>
<c:otherwise>
<h1>
Invalid Password , Try Again !!!
</h1>
<c:set var="user_flag" scope="application" value="0"/>
<c:set var="flag" scope="application" value="0"/>
</c:otherwise>
</c:choose>
</body>

</html>

Output:-

Output After login:-

Program :- 7

Aim:- To Implement JavaBeans.


Index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<h1>Java Beans example</h1>
<form action="first.jsp" method="get">
Enter USN : <input type="text" name="usn"/><br><br>
Enter Name : <input type="text" name="name"/><br><br>
Enter Course : <input type="text" name="course"/>
<input type="submit"/>
</form>
</html>

First.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="stud" scope="request" class="Beans.StudentBeans"/>
<jsp:setProperty name="stud" property="*"/>

<jsp:forward page="display.jsp"/>
</body>
</html>

display.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hi There</title>
</head>
<body>
<jsp:useBean id="stud" scope="request" class="Beans.StudentBeans"/>
USN : <jsp:getProperty name="stud" property="usn"/>
Name : <jsp:getProperty name="stud" property="name"/>
Course : <jsp:getProperty name="stud" property="course"/>
</body>
</html>

StudentBeans.java
package Beans;

public class StudentBeans {


private String usn;
private String name;
public String getName() {

return name;
}
private String course;
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public void setName(String name) {
this.name = name;
}
public String getUsn() {
return usn;
}
public void setUsn(String usn) {
this.usn = usn;
}

Output:-

Output After Submit:-

Program:- 8

Aim:- A program to implement rmi.


AddI.java:import java.rmi .Remote;
public interface AddI extends Remote
{
public int add(int x, int y) throws Exception;
}
AddC.java :import java.rmi.server.*;
public class AddC extends UnicastRemoteObject implements AddI
{
public AddC() throws Exception
{
super();
}
public int add(int x, int y)
{
return x+y;
}
}
Server.java
import java.rmi.*;
public class ser
{
public static void main(String a[]) throws Exception
{
AddC obj=new AddC();
Naming.rebind("ADD",obj);
System.out.println("Server Started");
}
}
Client.java:-

import java.rmi.*;
public class Client
{
public static void main(String a[]) throws Exception
{
AddI obj=(AddI)Naming.lookup("ADD");
int n =obj.add(5,4);
System.out.println("Addition is: " +n);
}
}

Output Server:-

Output Client:-

You might also like