0% found this document useful (0 votes)
1 views16 pages

Jndi Program

The document outlines a Java web application that uses JNDI for database connectivity to manage flower data. It includes JSP pages for displaying flower information, inserting new flower records, and a servlet for processing database operations. The application connects to a JDBC DataSource and performs SQL queries to retrieve and insert data into a 'flower' table.

Uploaded by

Aziz Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views16 pages

Jndi Program

The document outlines a Java web application that uses JNDI for database connectivity to manage flower data. It includes JSP pages for displaying flower information, inserting new flower records, and a servlet for processing database operations. The application connects to a JDBC DataSource and performs SQL queries to retrieve and insert data into a 'flower' table.

Uploaded by

Aziz Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

JNDI PROGRAM:

Sample connection:

<%--

Document : newjsp

Created on : 7 Mar, 2015, 4:24:27 PM

Author : admin

--%>

<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.PreparedStatement"%>

<%@page import="java.sql.SQLException"%>

<%@page import="java.sql.Connection"%>

<%@page import="javax.sql.DataSource"%>

<%@page import="javax.naming.InitialContext"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


<title>JSP Page</title>

</head>

<body> section of the page:

<h1>Data in my Connection Pooled Database</h1>

<br>

<%

InitialContext ctx = new InitialContext();

//The JDBC Data source that we just created

DataSource ds = (DataSource) ctx.lookup("jdbc/sample");

Connection connection = ds.getConnection();

if (connection == null)

throw new SQLException("Error establishing connection!");

String query = "SELECT * FROM data";

PreparedStatement statement = connection.prepareStatement(query);

ResultSet rs = statement.executeQuery();
while (rs.next())

out.print(rs.getString("name") + "<br>");

%>

</body>

</body>

</html>

Output:
Index.html

<!DOCTYPE html>

<!--

To change this license header, choose License Headers in Project Properties.

To change this template file, choose Tools | Templates

and open the template in the editor.

-->

<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>Welcome to Duke Flower shop </H1>

To add a new flower <a href="insert.jsp">click here</a>

To view the list of flowers <A href="Select">click here</a>

</body>

</html>

Insert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h1>Insert Flower details!</h1>

<form action="Insert"><br/>

<br/>Enter flower id:<input type="text" name="fid" value="" />

<br/>Enter flower name :<input type="text" name="fname" value="" />

<br/>Enter flower description :<input type="text" name="fdesc" value="" />

<br/>Enter flower price :<input type="text" name="fprice" value="" />

<br/><input type="submit" value="Add" />

</form>

</body>

</html>

package model;

import java.io.IOException;

import java.io.PrintWriter;

Model
Insert.java

import java.sql.Connection;

import java.sql.PreparedStatement;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

@WebServlet(name = "Insert", urlPatterns = {"/Insert"})

public class Insert extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

/* TODO output your page here. You may use following sample code. */

try{

Context cx= new InitialContext();

DataSource ds= (DataSource)cx.lookup("jdbc/sample");

Connection con= ds.getConnection();


String sql="insert into flower values (?,?,?,?)";

int fid =Integer.parseInt( request.getParameter("fid"));

String fname = request.getParameter("fname");

String fdesc = request.getParameter("fdesc");

int fprice =Integer.parseInt( request.getParameter("fprice"));

PreparedStatement ps = con.prepareStatement(sql);

ps.setInt(1, fid);

ps.setString(2, fname);

ps.setString(3, fdesc);

ps.setInt(4, fprice);

ps.executeUpdate();

con.close();

response.sendRedirect("index.html");

}catch(Exception e){

out.println("Error occured : " +e);

View:
package view;

import java.io.IOException;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

@WebServlet(name = "Select", urlPatterns = {"/Select"})

public class Select extends HttpServlet {


protected void processRequest(HttpServletRequest request,
HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

out.println("<h1>List of available flowers</h1>");

try{

Context x = new InitialContext();

DataSource ds =(DataSource) x.lookup("jdbc/sample");

Connection con = ds.getConnection();

String sql="select * from flower";

PreparedStatement ps = con.prepareStatement(sql);

ResultSet rs =ps.executeQuery();

out.println("<table border='3'> ");

while(rs.next()){

out.println("<tr> <td>" + rs.getInt(1)+ "</td> <td>"+rs.getString(2)+"


</td> <td>"+rs.getString(3)+"</td> <td>"+rs.getInt(4)+"</td> </tr>");

out.println("</table");

con.close();
}catch(Exception e){

out.println("Error "+e);

out.println("</body>");

out.println("</html>");

}}

Step to JNDI Connection:


Go to database connect the jdbc/sample JNDI
Create the table Flower

Insert record

Jndi connection is over

Run the project


Click view the list of flower in database using jndi
Click insert the data into the database ..thenclick

Data are store in the database ..

You might also like