File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes How to update a database using web forms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to update a database using web forms" Watch "How to update a database using web forms" New topic
Author

How to update a database using web forms

campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
HI all!

I already can connect to the database and do updates and inserts directly using a small java apllication with a main method. I can also do updates using servlets. But I am trying to do update with a web form but I cant see no results in the database!



My code is :




I have in my web form in the name this:

name="name"
name="userID"
name="password"

SO anyone knows what is wrong!!
I have searched and read several tutorials but I can see no difference??

ANy help would be great!

Thanks
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What error(s) are you getting? That usually helps to determine what is wrong.


GenRocket - Experts at Building Test Data
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
I am not getting any error it compiles fine and even when I call the servlet it also do fine!

I am checking if it works through SQL Server query program to see if did any update but nothing??

If I write a direct update without the web form the servlet updaets the database but when I tried to get a user input from the form it doest update why??

This is my complete code?


So anything wrong?
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
The first thing to try is run a select query and be sure there actually is a row where UID = 'papai' and PWD = 'mamae' and Nome = 'parents'. If not that's why there no change to the database.

The next thing to check is what the primary key of this table is. If it is any of the fields your attempting to update then the update will fail. It should be throwing a SQLException if this is the case though.

The third thing is try this with the modified methods below. I noticed in your code that if you come to this servlet through the get method instead of post then it doesn't to the update right away. Also it will help to see what those parameters are so I output them. Standard out should go to your application server log - if not, re-write to the screen with the response object writing a small html output file.


[ June 03, 2004: Message edited by: Lu Battist ]
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
Another important thing to note is that is a bad idea to put your database connection in the init method. Sooner or later that connection is going to timeout and the servlet will stop working. Generally its better to always get and close your database connection in the same method.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65



So do you really think that in the case of an exception that it's appropriate to write the error to a log that may or may not be seen and continue as if nothing happened?

My guess is that an exception is being thrown and you are just not seeing it.

Here's a better idiom:



this will cause a ServletException to be thrown which is sure to catch your attention, and with the original SQL exception as the root cause so you can see all details of the original exception.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

I once lead a project where one of the members loved to write:



He didn't work there very long.

(He wasn't let go because he did this. It was because after repeated attempts to explain what an atrocity that was, he still didn't get it.)
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Well I am getting something in the tomcat when I type my name, userID and password it says:



and then more



So what Am I doing wrong?? Have read all tutorials I could find + done several ways of updating?

My servlet updates database fine when I dont use a web form.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60991
    
  65

Did you change your exception handling? You are getting a null pointer exception and without a stack trace all you can do is guess where it is.

Stop supressing the exception; this is exactly the reason I made my previous post. If the exception propogates out to the container it will display it to you in glorious detail!
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Well thanks for the help but I am at home now and can only test tomorrow at work!!

I am new in programming with java in the real world!

I put the exception in th e code but it doest compile it doesnt recognize

throws new ServletException?? Are you saying that I will have to create a class only to catch this exception??
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Well I think the exception erroe was that I didnt put this in the code for the update to make effect in the row:

int value = update.executeUpdate();

Correct me if I am wrong ok.
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Sorry I think is the statement:

Statement statement;

int value = statement.executeUpdate();
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Well I think it could be in the

Do you think that I can do this??

con.prepareStatement("update WIZUSUARIOS set UID = ' " + userID + ", PWD = '" + password + " , Nome = ' " + name + "where UID = 'papai' and PWD = 'mamae' and Nome = 'parents'");

SO what you guys think??

Help newbie please!

I have been long time without practice with Java but I can do it?? I just think I am missing somehing here!

It gives an error nullpointexception I think that the servlets is dying before getting the parameter from the user or or the connection is being closed before sending the parameter into the database.

What you think??
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Campos, I would suggest that we start over by you displaying your complete source code or at least the source code that generated the error and the complete stack trace of the error generated.

This will help us help you understand the problem(s).

Thanks.

Craig.
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
OK!

This is the source code:

Servlet[CODEpackage dbConnect;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class ServletLogin extends HttpServlet {

//Statement stm;
//ResultSet rs = null;
PreparedStatement update =null;
Connection con = null;
String DbUserName = "eloi";
String DbPassword = "trocar";

public void init() throws ServletException {

}

public void doGet(HttpServletRequest request , HttpServletResponse response)
throws IOException, ServletException{

doPost( request, response );
}

public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {

try{

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://ANDROMEDA:1433",DbUserName, DbPassword);


PreparedStatement update = con.prepareStatement("insert into WIZUSUARIOS (UID, PWD, Nome)"
+ "values(? ,? ,? )");

}
catch(Exception sqle ){
System.out.println("Error message at" +sqle);
//System.out.println("Error message at" + cne);

}

try{

update.setString(1,request.getParameter("userId"));
update.setString(2,request.getParameter("password"));
update.setString(3,request.getParameter("name"));
update.executeUpdate();
update.close();
con.close();

}
catch(SQLException e){
e.printStackTrace();

System.out.println(e.getMessage());
System.out.println("Error at" + e);
}
}

public void destroy() {
super.destroy();

try {
if(update != null)
update.close();
}
catch(SQLException e){
System.out.println("Error at" + e);
}
try{
if(con != null)
con.close();
}
catch (SQLException sqle) {
//throw new ServletException( "SQL error trying to do this or that: " + sqle);
System.out.println("Error at" + sqle);

}

}
}
[/CODE]

the web form is:


and the eror messsage is


Its funny because before it didnt come this message just a blank page??
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
Let's take one issue at a time. I believe you are getting a null exception
for section of code:


and the reason being is that you defined the following


as instance variables but later in your code and more specifically you redefined them again in the try/catch block


And if you remember in java 101, any variables defined within a block { } will go out of scope outside of that block. I hope am wording this correctly. Usually the compiler will complain about this, but because the same variables were defined earlier and were accessible to the entire class.
I hope I am explaining this correctly.


So if you at least change the statements to ...


This will hopefully solve this problem.

Craig
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
The variable for "user id" needs to be spelled the same.


I would follow Lu Battist suggestions:

Another important thing to note is that is a bad idea to put your database connection in the init method. Sooner or later that connection is going to timeout and the servlet will stop working. Generally its better to always get and close your database connection in the same method.


This would remove the need to use instance variable(not recommended) and this would also remove the code in the destroy method.

Craig
campos teixeira
Ranch Hand

Joined: May 23, 2004
Posts: 51
Thanks cj jack

I really miss out the userId argument type but I was trying to see it is works and I had a problem with eclipse and tomcat which didnt want to work??

Funny yearsterday was working fine and today I couldnt do nothing so now only monday to see is its working.

I did eveything that was said and now just need to test on monday as I dont have a database at home?? Will try to install ORacle 9i>

Anyway thanks very much for all the help from everyone.

Will let you guys know if it is worke or not! I hope it does
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to update a database using web forms