JSP_DB_Interactions
//File: header.jsp <div style='color:blue;'> Header & Footer </div> <DIV>DINESH jee</DIV>
//File:newForm.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <%@ include file="header.jsp" %> <% out.print("Hello"); String driver=config.getInitParameter("dname"); out.print("driver name is="+driver); %> <form method='post' action='db_works.jsp'> <input type='text' placeholder='search text' name='st'> <input type='submit'> </form> </body> </html>
//File: db_works.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.sql.*"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.Connection"%> <%@page import="java.io.*"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% try{ out.print("HELLO"); out.print("<a href='insert.html'>insert new rec</a>"); Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3308/test?user=root&password="); out.print("Connected!"); Statement st=(Statement) c.createStatement(); ResultSet rs=st.executeQuery("SELECT cname,address,gender FROM customers"); out.print("<table border='1'<"); out.print("<tr><th>Name</th><th>Address</th><th>Gender</th><th>Action</th></tr>"); while(rs.next()){ out.println("<tr><td>"+rs.getString(1)+"</td><td>"+rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td><a href=del.jsp?v="+rs.getString(1)+">Delete</td></tr>"); } out.print("</table>"); c.close(); }catch(Exception ie){ System.out.print("Error: "+ie.getMessage()); } %> </body> </html>
//File: del.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@page import="java.sql.*"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.Connection"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% try{ Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3308/test?user=root&password="); Statement st = c.createStatement(); String v= request.getParameter("v"); st.executeUpdate("delete from customers where cname='"+v+"'"); out.print("<a href='db_works.jsp'>Back</a>"); c.close(); }catch(Exception se){ out.print("Errors: "+se.getMessage()); } %> </body> </html>