aspose file tools*
The moose likes JDBC and the fly likes Not able to add values in a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Not able to add values in a database" Watch "Not able to add values in a database" New topic
Author

Not able to add values in a database

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Dear Ranchers,

I am having a problem inputting values in a database.


.xml file



Index.jsp




ProcessInformation.java (servlet)



Database connectity is done and has been tested as success

Using jdbcdbc connection

DSN: AdressBook

Table Name: Contact_Details2

Yet the table is not displaying any updated info


http://www.lifesbizzare.blogspot.com || OCJP:81%
Ajeeth Kumar
Ranch Hand

Joined: Mar 30, 2005
Posts: 56
I havent tried compiling your code in my PC. But just some suggestions.

a) I think doGet used to be the default method that would be invoked. So doGet and doPost should have the same implementation if you want to ensure that your code works as you intend to .
b) Try printing the PreparedStatement stat before running the update to see what statement is actually appearing. Look for syntax errors by trying to run that in any SQL editor
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 146
Have you committed the transaction?
Nam Ha
Ranch Hand

Joined: Aug 05, 2011
Posts: 50

Hi Vishal,

What is the error message and stack trace?

From your post I think the servlet's method doPost() gets invoked correctly. However the problem might your SQL statement.
Does your table have auto increment primary key? If so, you should not pass a value for that column in the statement.


Java Object Creation
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Thank you all for your prompt response

There is no such error , i am being redirected to a blank page.
Jitesh, how to commit a transaction?

Ajeet, how to print out the PreparedStatement?
Nam Ha
Ranch Hand

Joined: Aug 05, 2011
Posts: 50

Vishal Hegde wrote:

There is no such error , i am being redirected to a blank page.



Yes, you see a blank page because you commented out all output statement in method processRequest().

Look at line 58:



The Servlet will flush all output to browser so your database code does not have chance to run.

Try to move line 58 to the end of doPost() method.

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Hi Nam Ha,

I followed your suggestions and kept processRequest(request, response); as the last line of dopost() this time i got an exception ...I am not sure which value is Null right now

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
com.example.Web.ProcessInformation.doPost(ProcessInformation.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
Nam Ha
Ranch Hand

Joined: Aug 05, 2011
Posts: 50

Hi Vishal,

The exception is thrown at line 71:


I don't know which line is exactly line 71 in your code now because you modified it.
But I guess some of your parameters were null at this section:



Have a look at this line in your Index.jsp:



there must be a space between "text" and "name".
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Hi Namha,

Line 71 are the below codes



I am not sure why is this creating an Null exception URL is assisnged some string value .
Nam Ha
Ranch Hand

Joined: Aug 05, 2011
Posts: 50

I think you are missing to instantiate the driver:

Akshay D Joshi
Greenhorn

Joined: Aug 04, 2011
Posts: 9
Hello Vishal....

I have gone through your code...

First you delete the work folder from your application.....
Then restart your tomcat.......
In ProcessInformation .java code ,i saw that you are getting the mobile number as

Mobile_Num=Integer.parseInt((request.getParameter("MOB").trim()));

But the Textbox name of Mobile No. in Index .jsp is Mob...
Therefor you are getting null pointer exception....
So either change "MOB" to "Mob" in java file or change "Mob" to "MOB" in Index.jsp.....

Now second point is commit the transaction before closing the connection as
con.commit();

Now Third point is you haven't forwarded to any jsp after executing query...

You can add following code in java after closing connection...

RequestDispatcher view=getServletContext().getRequestDispatcher("/Result.jsp");

view.forward(request,response);

Compile your java code...

And also add Result .jsp in Application folder Showing text Something like "Inserted successfully....".


Try It...
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Love you Namha and Akshay Joshiii finally added values in database

You people are studs to see my small mistakes

But one more query when i input my cell number(10 digiy numerical) it gives me error

java.lang.NumberFormatException: For input string: "9867169606"
java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
java.lang.Integer.parseInt(Integer.java:459)
java.lang.Integer.parseInt(Integer.java:497)
com.example.Web.ProcessInformation.doPost(ProcessInformation.java:71)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)
Nam Ha
Ranch Hand

Joined: Aug 05, 2011
Posts: 50

java.lang.NumberFormatException: For input string: "9867169606"


Hi Vishal,

Because the value 9,867,169,606 is too big to store in an integer number:
Java integers range from -2,147,483,648 to 2,147,483,647.

Try to use Long instead.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Thnks Namha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to add values in a database