I have tried the following code
Database creation: create database studentdb;
create table stu_info ( ID int not null auto_increment, Name varchar(20), City varchar(20), Phone varchar(15), primary key(ID) ); datainsertion.jsp: <%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <HTML> <HEAD> <TITLE>Navigating in a Database Table </TITLE> </HEAD> <BODY bgcolor="#ffffcc"> <font size="+3" color="red"><br>Welcome Guest !</font> <FORM action="databaseinsertion.jsp" method="get"> <TABLE style="background-color: #ECE5B6;" WIDTH="30%" > <TR> <TH width="50%">Name</TH> <TD width="50%"><INPUT TYPE="text" NAME="name"></TD> </tr> <TR> <TH width="50%">City</TH> <TD width="50%"><INPUT TYPE="text" NAME="city"></TD> </tr> <TR> <TH width="50%">Phone</TH> <TD width="50%"><INPUT TYPE="text" NAME="phone"></TD> </tr> <TR> <TH></TH> <TD width="50%"><INPUT TYPE="submit" VALUE="submit"></TD> </tr> </TABLE> <% String name = request.getParameter("name"); String city = request.getParameter("city"); String phone = request.getParameter("phone"); String connectionURL ="jdbc:mysql://localhost:3306/studentdb"; Connection connection = null; PreparedStatement pstatement = null; Class.forName("com.mysql.jdbc.Driver").newInstance(); int updateQuery=0; if(name!=null && city!=null && phone!=null){ if(name!="" && city!="" && phone!="") { try { connection = DriverManager.getConnection(connectionURL,"root","root"); String queryString = "INSERT INTO stu_info(Name,City,Phone) VALUES (?, ?, ?)"; pstatement = connection.prepareStatement(queryString); pstatement.setString(1, name); pstatement.setString(2, city); pstatement.setString(3, phone); updateQuery = pstatement.executeUpdate(); if (updateQuery != 0) { %> <br> <TABLE style="background-color: #E3E4FA;" WIDTH="30%" border="1"> <tr><th>Data is inserted successfully in database.</th></tr> </table> <% } } catch (Exception ex) { out.println("Unable to connect to batabase."); } finally { pstatement.close(); connection.close(); } } } %> </FORM> </body> </html> While running this code on tomcat server i caught with following exception after clicking on submit button- exception org.apache.jasper.JasperException: An exception occurred processing JSP page /databaseinsertion.jsp at line 117 114: 115: finally { 116: 117: pstatement.close(); 118: 119: connection.close(); 120: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause java.lang.NullPointerException org.apache.jsp.databaseinsertion_jsp._jspService(databaseinsertion_jsp.java:172) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs. iam unable to find what was wrong!!!!!
Please Resolve it , thanks in advance
Please visit the following link:
Ads