Share on Google+Share on Google+

REKHA
Insert data in mysql database through jsp using Prepared Statement ---- Please Resolve it
1 Answer(s)      4 years and 6 months ago
Posted in : JavaScript Questions

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

Ads

View Answers

October 3, 2012 at 12:32 PM











Related Tutorials/Questions & Answers:
Tutorials   
Java Spring Hibernate Struts Training code for programming How can I dynamically load user-entered data as content of jquery dialog? java webcam integration question java coding Fix Scrollbar position in a checkboxList after postback Information Save Alert Country wise redirection to files Interface in javascript connect database with javascript Zip Code ZipCode how to validate the email login How to play FLV video files on Ipad using HTML code or javascript code? Nitobi Grid is not working in IE10/IE11 jquery issue plese help RegEx Tree structure from json whose parents can be dragged and dropped from child newyorklife insurance getElementById not working javascript code for search in html file Implement The Skeleton Code Implement This Skeleton Code Mobile Automation using Appium Computer forensic application Java: Searching a string from doc files java linked list urgent !!! java linked list urgent!!! library management library management library management library management question Html form using JavaScript to display the table content java how to prevent no from unroundin off on clicking text box again in JSP and Jquery graph generation from xml design file display data from database in textbox when id entered in textbox Javascript Code for all fields how to get HTTP GET request value using javascript array Javascript Querry autocomplete(): Spring mvc with jquery: I am not getting correct value in the text filed. Please help me Create SpreadSheet same as excel using java applet Create SpreadSheet same as excel using java applet How to change focus among multiple select boxes? Write JQUERY Code Write JQUERY Code Converting jsp variable to java variable java

Ads

 
Advertisement null

Ads