#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    361
    Rep Power
    13

    editing/adding on same page


    i've made a jsp page to add data to my db which has a number of input boxes on it. i fill them in, submit and all is (was!) well.

    i've now edited the same page to use it to edit the db data. i hit the page with a get request that carries with it the primary key of the record i want to edit. inside each value tag i've got an rs.getString() that fills the boxes up for me. i edit, submit, and all is (was!) well.

    now (yep, you've guessed it) when i go to the page to add data, i get lots of NullPointerExceptions because my rs isn't there.

    how do i fix this? do i wrap the resultset in another function that sorts it out? or is there a quick and elegant fix?
    Little more than a playground for the bugs that live beneath us...
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    361
    Rep Power
    13

    setup:


    I'm on Tomcat 4.0.4 which is handling the JSP requests for Apache 1.3.26 via mod_jk. I'm using j2sdk1.4.0 and it's all on Red Hat 7.3. db is MySQL.

    I realise the problem lies with my inability! I'm used to PHP, which you can drive a truck through and it won't feel it... JSP seems rather delicate!

    [in pseudo-code] i'm trying to do this:

    Code:
    if request method is post {
         if have primary key {
         update db
         }
         else {
         add new entry to db
         }
    }
    else {
         if have primary key {
         get record from db for editing
         }
         else {
         do nothing
         }
    }
    this means my form has to be prepared to show either emptiness or an existing record... and i don't want to duplicate the form or the page (as there are several pages like this and some are monsters!)
    Little more than a playground for the bugs that live beneath us...
  4. #3
  5. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Eh, I don't think you need to go that far

    Try something like:
    Code:
    <%= rs != null ? rs.getString() : "" %>
    You might want to examine the logic of your page and see if there's another way to accomplish what you're going for, but this way should work fine.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    361
    Rep Power
    13
    many thanks again, bricker42,

    i'd thought about that approach, and i was prepared to do it if I had to, but i was after something more elegant (as i'd also have to do a test for sql nulls), so i took your course of examining the logic...

    i have a series of 3 pages to update one record, so i have a function on the first call a resultset into my bean which sits there for the other pages to call fields from (with the getField(fieldName) method below). i'd tried this previously, but i think i still don't have my head around jsp/java scoping.

    Code:
    	public String getField(String fieldName) throws SQLException {
    		if (rs_prop==null) {
    		return "";
    		}
    		else {
    			if(rs_prop.getString(fieldName)==null) {
    			return "";
    			}
    			else {
    			String retval = Pattern.compile("\"").matcher(rs_prop.getString(fieldName)).replaceAll("\"").toString();
    			return retval;
    			}
    		}
    	}
    am i wise to have this recordset sitting there while the user goes through three pages? do i have to make sure it gets killed at the end? what's the difference between rs=null and rs.close()?

    please feel free to tell me to go read a book!
    Little more than a playground for the bugs that live beneath us...
  8. #5
  9. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, you can do it that way, sure . It will take more resources, though, since you have to leave the connection open until you're done with the record set.

    You might want to look into building a bean that holds one row of your data, though. It's a slightly less flexible method since you have to plan ahead of time for all your queries, but you should be doing that anyway

    What you get this way, though, is a bean that you can read from, write to, update, and build, all without having to call any sql. Depending on what you need you can have your bean load itself from from the database, or you could create the bean from a record set.

    Incidently, this is the basis of what ejbs do. It's a convenient way to encapsulate your data.
    Last edited by bricker42; October 9th, 2002 at 10:03 PM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    361
    Rep Power
    13
    now this is interesting!

    my bean has a connect() and a disconnect() method. i call the connect() and then i call a function (which takes the primary key of the record i want as an argument) which gets me my one row resultset. i then call the disconnect(). the disconnect method throws no errors.

    i then use the getField() from my previous post to get a field from the resultset which is resident in the bean and all seems fine.

    you warned "It will take more resources, though, since you have to leave the connection open until you're done with the record set" and yet i seem to have still got the resultset after closing the connection. am i kidding myself? is the connection still open?

    my bean has another method to empty the resultset (it sets rs_prop to null, but i wonder if i should be calling rs_prop.close()?) when i'm done with the row.

    i like your stuff about storing the bean in the db, but that seems real rocket science at the moment - does the bean serialize to a string? presumably this is not the way to go if you need a rich search capablity onto the stored info?

    am i right in saying ejbs are different from javabeans?

    thanks again for your help.
    Little more than a playground for the bugs that live beneath us...
  12. #7
  13. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, here's a quote from the ResultSet javadoc
    A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.
    Are you not closing your statement, maybe? I can't find anything about the statement being cleared when you close the connection, so that could be it.

    As for storing a bean in the database, yes, you can serialize it, but that's usually not the best solution. What I meant was you create your bean so that each column in your table has a coresponding property in the bean. Then, when your load function, it places values from the table into the properties in the bean.

    As for ejbs, they're kinda javabeans on crack. I haven't had a chance to use them yet, though, so I'm not all that familiar with them. From what I've read they're primarily for transaction management. They allow you to access/modify data without going to the database, then commit to the db when you're done.

IMN logo majestic logo threadwatch logo seochat tools logo