Professional Documents
Culture Documents
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
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
Output:-
Program :- 4
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>");
}
}
@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:-
Program:- 5
Aim:- Jsp for Show current date and time.
<html>
<body>
<p> </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"> Date
Example</font></td>
</tr>
<tr>
<td width="100%"><b> Current Date and time is: <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: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"/>
</html>
Output:-
Program :- 7
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;
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:-
Program:- 8
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:-