jdbc connection
<%-- view jsp --%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.Connection"%> <%@page import="java.sql.ResultSet"%> <%@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> <center> <h1>View Your Location</h1> <form action="view" method="POST"> <b> Enter Your Ignite Id:</b><input type="text" name="id" placeholder="IgniteId"><br><br> <input type="submit" value="SUBMIT" name="submit"><br><br><br> <table border="2"> <th>Name</th> <th>home</th> <th>Location</th> <% try { ResultSet rs = (ResultSet) request.getAttribute("data"); while (rs.next()) { %> <tr> <td><input type="text" value="<%=rs.getString("name")%>"</td> <td><input type="text" value="<%=rs.getString("home")%>"</td> <td><input type="text" value="<%=rs.getString("location")%>"</td> </tr> <% } } catch (Exception e) { } %> </table> </form> </center> </body> </html> <%-- view serverlet --%> PrintWriter out = response.getWriter(); String Id = request.getParameter("id"); try { Class.forName("com.mysql.jdbc.Driver"); Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/location", "root", "root"); String id=request.getParameter("id"); String query = "select * from posting where ignite_id='"+id+"'"; Statement s = null; s = (Statement) con.createStatement(); ResultSet rs = null; rs = s.executeQuery(query); request.setAttribute("data", rs); RequestDispatcher rd = request.getRequestDispatcher("view.jsp"); rd.forward(request, response); } catch (Exception e) { out.print(e.toString()); } finally { out.close(); } } <%-- submit jsp --%> <center> <form action="hit" method="POST"> <fieldset> <b> iGnite Id:</b><input type="text" name="id" placeholder="Ignite Id"><br><br> <b>Your Name:</b><input type="text" name="name" placeholder="Name"><br><br> <b> Home Town:</b><input type="text" name="home" placeholder="Home"><br><br> <b>Location:</b><input type="text" name="location" placeholder="Location"><br><br> <b>Gender</b><input type="radio" name="sex" value="male">Male <input type="radio" name="sex" value="female">Female<br><br> <b>Choice</b><input type="checkbox" name="choice" value="Kolkata">Kolkata <input type="checkbox" name="choice" value="Chennai">Chennai <input type="checkbox" name="choice" value="Mumbai">Mumbai <input type="checkbox" name="choice" value="Delhi">Delhi<br> <input type="submit" name="submit" value="Submit"> </fieldset> <center><a href="view.jsp"><h1>VIEW</h1></a></center> </form></center> <%-- submit serverlet --%> PrintWriter out=response.getWriter(); String Id=request.getParameter("id"); String Name=request.getParameter("name"); String Home=request.getParameter("home"); String Location=request.getParameter("location"); String Gender=request.getParameter("sex"); String Choice=request.getParameter("choice"); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/location", "root", "root"); String sql="INSERT INTO posting (ignite_id,name,home,location,gender,choice) VALUES('"+Id+"','"+Name+"','"+Home+"','"+Location+"','"+Gender+"','"+Choice+"')"; PreparedStatement ps=(PreparedStatement) con.prepareStatement(sql); ps.executeUpdate(); ps.close(); con.close(); } catch(Exception e) { out.print(e.toString()); } finally{ out.print("Submit Successfully"); out.close(); } }
Ads