Archive for the 'Java' Category

Oracle Blob (JDBC) - How to

Tuesday, May 27th, 2008

This morning I needed to insert a file’s contents into a blob on an oracle database so here is the method used to do that (look for a future post on dropping a file into an applet).

/* File Loaded here into a File Object as f */
PreparedStatement ps = conn.prepareStatement(”INSERT INTO TBL VALUES(?, ?)”);
ps.setString(1, f.getName());
FileInputStream fis = new FileInputStream(f);
ps.setBinaryStream(2, fis, fis.available());
ps.execute();
ps.close();

And tada your record will be loaded into the blob.

*** Not Shown ***
Oracle JDBC Connection - This example does not use any framework/connection helper (i.e. Hibernate/EJB)

SCJA

Friday, May 23rd, 2008

As most know I am studying to get my SCJA and making a much bigger deal out of it than it should be.  I have been through all the test prep books several times and am confident with the material however I find some odd aversion to taking the test.  I guess all of us experience fear of failure and more so when we work with the technology every day ;-).  Oh well I am going to purchase my exam ticket tomorrow and then just finally set a date to go take the thing.  Below is the link to find out more about the SCJA if needed.

http://www.sun.com/training/certification/java/scja.xml

Common Hibernate Options (hibernate.cfg.xml)

Tuesday, May 20th, 2008

Below is a pretty standard configuration which appears to work quickly using an oracle database with a datasource defined in an oracle application server.  I will be refining this over the next week as I performance tune the “massive app”.

<property name=”hibernate.dialect”>org.hibernate.dialect.OracleDialect</property>
<property name=”connection.datasource”>jdbc/xxx</property>
<property name=”current_session_context_class”>thread</property>
<property name=”hibernate.transaction.factory_class”>org.hibernate.transaction.JDBCTransactionFactory</property>
<property name=”cache.provider_class”>org.hibernate.cache.NoCacheProvider</property>
<property name=”show_sql”>true</property>
<property name=”hbm2ddl.auto”>none</property>
<property name=”connection.pool_size”>5</property>
<property name=”hibernate.max_fetch_depth”>0</property>
<property name=”hibernate.use_reflection_optimizer”>true</property>
<property name=”use_outer_join”>false</property>

JRuby Protecting me From Forgery

Tuesday, May 20th, 2008

Tonight I took it upon myself to attempt to get a JRuby app to deploy on an OC4J Instance and connect to an Oracle database.  Locally this was no issue however when I deployed in “production” mode up on the slice I kept getting this error:

ActionView::TemplateError (No :secret given to the #protect_from_forgery call.  Set that or use a session store capable of generating its own keys (Cookie Session Store).) in welcome/index.html.erb:

Needless to say I was a bit befuddled and couldn’t find much on the google pipes of consequence until I stumbled up on this line in application.rb (controllers folder):
protect_from_forgery #:secret => ‘xxxxxxxxxx’
I uncommented this line and life was good.  Apparently in the application server the session store cannot generate its own secret key.  Oddly enough this works fine (with the line commented) on Glassfish but not on OC4J.  So I am chalking it up to a server oddity for now until I have more time to research.

Hql Basics (Parameters)

Friday, May 16th, 2008

In using Hibernate Query Language to include a Parameter of Type Long in a statement you would use the following code:

Query q = session.createQuery(”from Class1 c where c.class2.class2_id = :class2_id”);
q.setLong(”class2_id”, class2_id);
q.list();…..

One thing to Immediately notice (if you are familiar with Hibernate) is that I am calling class2_id through class2.  This is because in my little world class2 has a one to many relationship with Class1.  This means there is an object called class2 joining them together in the annotations.  However, I digress.  The real take away is that a parameter called :class2_id was established in the query and the referenced in the setLong statement to set the value.  After that calling the q.list function will return the values pulled back from the query. And TADA!