aspose file tools*
The moose likes JDBC and the fly likes Error trying to perform insert into table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Error trying to perform insert into table" Watch "Error trying to perform insert into table" New topic
Author

Error trying to perform insert into table

meenakshi venkateswaran
Greenhorn

Joined: Mar 10, 2005
Posts: 4
Hi all,

I am trying to insert a row of data into a database but I keep getting an exception..The code snippet as well as the exception are as follows:
public void insertQuestions() {
PreparedStatement s;
ResultSet rs;
SurveyVec surveyVec = new SurveyVec();
try {
System.out.println("Reached here");
s = con
.prepareStatement("INSERT INTO question (number, position, surveyname, question, answerchoice, answer) VALUES ('1', '2', 'a', 'b', 'c', 'd')");
s.setInt(1, surveyVec.getNumber());
s.setInt(2, 2);
s.setString(3, surveyVec.getName());
s.setString(4, surveyVec.getQn());
s.setString(5, surveyVec.getAnschoice());
s.setString(6, surveyVec.getAnswer());
String ss = s.toString();
System.out.println("ss is " + ss);
s.executeUpdate();
rs = s.getResultSet();
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2)
+ "<br>");
}

} catch (SQLException sqlEx) {
System.out.println("SQLException 1 roblem reading rows "
+ "from the Account Table:" + sqlEx.getMessage() + ":"
+ sqlEx.getErrorCode());
}

The error is :
java.lang.NullPointerException
surveysys.DBClass.insertQuestions(DBClass.java:42)
surveysys.SurveyController.doPost(SurveyController.java:56)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

Line 42 of my DBClass is the line "prepareStatement("INSERT INTO question (number, position, surveyname, question, answerchoice, answer) VALUES ('1', '2', 'a', 'b', 'c', 'd')"); "

I dont understand y a NullPointerException is being thrown.

Can someone please throw light on this?

Thanks in advance
Meenakshi
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
What is the value of the "con" variable when you print "Reached here"?


James Carman, President<br />Carman Consulting, Inc.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Why are you doing setXxx()??
Probably your "con" object is null.

What are you trying to do. You are intantiated a bean and then straight trying to get its properties. What are you expecting?
[ March 11, 2005: Message edited by: Adeel Ansari ]
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by meenakshi venkateswaran:

s = con.prepareStatement("INSERT INTO question (number, position, surveyname, question, answerchoice, answer) VALUES ('1', '2', 'a', 'b', 'c', 'd')");



meenakshi

you connecdtion could be null, but do modify your code as adeel said
What is doing sexXXX() ?

seems you have copied your test query in your code.

make your prepared statement as




hope this helps


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
meenakshi venkateswaran
Greenhorn

Joined: Mar 10, 2005
Posts: 4
Hi all

Thanks a lot for all your replies!!!

Meenakshi
 
jQuery in Action, 2nd edition
 
subject: Error trying to perform insert into table
 
Similar Threads
I have a problem with getDate() ?
Error 500 in root causes saying not enough value error
JDBC code with type 1 driver not working
error while entering the data into oracle database
How to write to new table?