Archive | java

JSON — not just for AJAX

Mar 12th, 20071 Comment

I recently blogged on the evils of mixing Java and JavaScript in JSP pages but didn’t give any good ways around it.  I mean, you have a list or array in Java and you want JavaScript in the JSP page to access this data — how do you do that?

Let me introduce you to to Json-lib.

You’ve probably heard of JSON before and people rant and rave about it on using JSON with AJAX instead of XML[1].  And I don’t think there is anything wrong it that, but I didn’t want or need AJAX — I just need to move data from Java to JavaScript in a JSP page.  And JSON makes it oh so easy.

  1. In your Servlet, get and arrange your data the way you like it.
  2. Then using Json-lib make a JSONObject like so: JSONObject jsMap = JSONObject.fromObject(jobsMap);.
  3. Put the string of that JSON object into your request.  You know what I mean: request.setAttribute("JSON_MAP",jsMap.toString());.
  4. In your JSP page, just eval that string: var jobMap = eval(<%= request.getAttribute("JSON_MAP");%>.

And that’s it — your structure has been transfered from Java to JavaScript with no fuss nor muss.

The Json-lib jar is actually quite small. But the bad thing is that it has quite a few dependencies that their page doesn’t explain well and that doesn’t come with the download.  I needed to grab commons-beanutils, commons-logging, and ezmorph to get things rolling with Json-lib.  But it has sure been worth it.
[1] Then should it be called AJAJ?

Weblogic, Oracle and Blobs — oh my!

Feb 12th, 2007No Comments

The problem — upload a file and put it into a Blob in an Oracle database. The file upload was easy (Thanks Jakarta!) but saving it to the database was a different story. I found many, many examples on the web, but none of them quite worked. Then I read a post about someone struggling with the same thing, only with Weblogic 6. I’m using 8.1, but I used the idea and it worked! I’m still astounded by it.

The problem is that Weblogic’s database layer doesn’t inherit well. A class loader is probably the cause. Oh goody – I love class loader problems! But BEA snuck a helper class in to make this easy.

Actually, I’m not sure if the real problem is Weblogic, Oracle or even the people that gave us the “Blob standard”, but whatever it is, the solution is messy. It’s takes two updates to the same row! My apologies for WordPress messing up the code.

sqlStmt.append("UPDATE table_name");
sqlStmt.append(" BLOB_FIELD=empty_blob()");
sqlStmt.append(" where IDX=?");

pStmt = con.prepareStatement(sqlStmt.toString());

pStmt.setString(1,user);
pStmt.setTimestamp(2,nowSQL);
pStmt.setString(3,trainSymbol);

pStmt.executeUpdate();
con.commit();

// now we need turn off autocommit
boolean lastAuto = con.getAutoCommit();
con.setAutoCommit(false);

// then select the blob for update
StringBuffer sqlStmt2 = new StringBuffer();
sqlStmt2.append("select BLOB_FIELD from table_name");
sqlStmt2.append(" where IDX=? for UPDATE");

pStmt = con.prepareStatement(sqlStmt2.toString());
pStmt.setString(1,trainSymbol);

ResultSet rs2 = pStmt.executeQuery();

while (rs2.next()) {

Object o = rs2.getObject(1);

weblogic.jdbc.wrapper.Blob cast1 = (weblogic.jdbc.wrapper.Blob) o;
BLOB myblob = (BLOB) cast1.getVendorObj();

OutputStream outstream = myblob.getBinaryOutputStream();

outstream.write(blobArray);

outstream.flush();
outstream.close();

}
rs2.close();
con.commit();
con.setAutoCommit(lastAuto);

Note the first thing I did was not to insert the Blob directly into the database — instead I added an empty blob. Why? Because Oracle says so.

Then I turn off Auto Commit, because you can’t to a SELECT... FOR UPDATE with Auto Commit turn on. Why? Cuz Oracle says so.

Why do we need to do a SELECT... FOR UPDATE anyway? Because Oracle, or the blob people, say so.

Why did you need another ResultSet? Weird things happened when I didn’t. I’m not sure who to blame for that — Oracle, BEA, or the person who wrote the DAO class I had to put this into.
What’s with this cast to weblogic.jdbc.wrapper.Blob? I need to do that to get Oracle’s Blob, and casting from java.sql.Blob object didn’t work. Why not? Because BEA says so. I need to do that cast so I could get Oracle’s Blob from getVenderObj()

The rest is elementary — I get an output stream from the blob, write a byte array to it (i.e. blobArray and then cleaned up.

Jython 2.2b1

Feb 9th, 2007No Comments

Testers wanted.

The Jython Project
The Jython development team is proud to announce a new release: Jython 2.2 Beta1!

It’s been a long, wild ride from Jython 2.1 to this point. Let’s help them get this out the door quickly.

Congrats to all the developers to get the code to this point!

Eclipse vs Emacs: who wins?

Jan 23rd, 2007No Comments

When I started doing Java development, I thought I was going to use Emacs less and less and Eclipse more and more.  But I was wrong — I find myself copying text out of Eclipse all the time to put into Emacs. In Emacs, I write a keyboard macro to wrangle the text into submission, run that macro on every line, and then paste it back into Eclipse.  Messy?  Yep.  Quicker than doing it manually?  Yep.

However, although I love Emacs, it doesn’t handle all the little nits that you need for J2EE development like Eclipse does.  And auto-completion and running unit tests inside Eclipse is just gravy on top of that.

Of course, I use Eclipse’s Emacs key bindings, which makes things easier. But just because it has the same keystrokes doesn’t make it a replacement.  I can’t do keyboard macros inside Eclipse, nor can I write some Lisp function (or another language) quickly to get some other dirty work done.

Just yesterday I ran into a seemingly simple problem that uncovered a whole nest of issues.[1]  I was switching from JSP to Action class to another JSP , trying to get a handle on the problem.  Eclipse was bogging down.  It was slow, and then it was telling me that I had problems in my JSP that I didn’t have (this was probably Aptana’s fault).  I finally got tired of it, shut down Eclipse, and aimed Emacs at the files.  In a short time (and a short time before I had to head home) I discovered the issue, and thought how to best fix it in the evening.

If I left the story here, Emacs would be the winner.  But there is more . . .

This morning, I fired up Emacs right away and started working.  I thought I had a fix, so I tried to a build.  It failed building the JSP’s and gave me the line numbers with the errors.  Try as I might, I couldn’t figure out what was wrong.  So I fired up Eclipse and opened the JSP.  Right away, it said what the problem was All Hail Aptana!).  I fixed it and the build went.

So, really, which is the better tool?  Neither one.  And both.  Because without the both of them, I wouldn’t have been able to solve it.  I used them together to approach the problem and correct it.
Another interesting thing to note — Eclipse is not really an IDE and Emacs is not really an editor.  They are both frameworks — Eclipse is a framework around Java and SWT while Emacs is a framework around Lisp.  I just happen to use the IDE and the editor written in those frameworks.
[1] Actually it was a small problem that someone dealt with badly, and that caused a delicate balance in the code.  I broke that balance.

You can to more with it than just hit “Generate Getters and Setters”

Jan 18th, 20071 Comment

I’ll put aside my distaste for getters and setters and just assume they are a fact of life. And, for me now, they are — all of the data classes in the project I inherited have them. Okay, I can deal. And it seems that each one has internal classes representing data that is a part of the data. Not ideal, but I can live with it.

But this is the crap I don’t want to deal with:

 owner = item.getLastUpdate().getOwner().getName();

But what is even worse:

owner=null;
if (item.getLastUpdate()!=null ) {
if (item.getLastUpdate().getOwner()!=null) {
owner = item.getLastUpdate().getOwner().getName();
}}}

This is comical the first time you see it. It makes me want to scream the fifth time. But by the tenth time I just want to cry.

You see, just because you have a class has that a bunch of private properties and the results of “Generate Getters and Setters” does not mean that the class has to stay like that. You can add your own methods that may make life easier. Like so:

 public String getLastOwnerName() {
if (this.getLastUpdate()==null) { return null; }
if (this.getLastUpdate().getOwner()==null) { return null; }
return this.getLastUpdate().getOwner().getName();
}

You might be alarmed that this getter has no setter. That’s okay — you will survive. It’s worth it if you can replace the above if statement that is in your code ten times with the following:

owner = item.getLastOwnerName();

Wow — isn’t that so much nicer? And, what is even better is that, if how to change how you get the last owner name, you only have to change one method, not ten. How about that!

This concludes my rant.

Page 5 of 6« First...«23456»