How to read a record from a database Netbeans 6.0.1 (probably work in 6.0.0)

Ok, so you’ve got all excited about persistant database objects (you haven’t? oh you should get excited) and you want to plug them into your Javabeans enterprise app? BUT HOW.

Here’s a VERY basic instruction list.

Firstly right click on your java bean and create your entity classes from database. I put them in the same package as the bean. When you create the entity classes you will create a persistance unit. You will need that.

In your bean add the persistance lookup code, just under the main class declaration. For example:-

public class IntelligenceBean implements IntelligenceRemote, IntelligenceLocal { 
	private EntityManager em ;

Remember to change the unitName=”xxx” to your persistance unit name.

Now lets hookup the persintance object to your database

public void persist(Object object) { 
    }     public String testData() { 
       TblAccountType cust = (TblAccountType)em.find(TblAccountType.class, "Accounts                 "); 
      return cust.getAccountType(); 

Don’t forget to add the persists function. But the real one you should be interested in here is the testData(), this is in the bean and exposed to the remote interface. What i’ve done is to create a cust object that is one of my persistance objects “TblAccountType” then got the persistance object to go lookup the record with the key “ACCOUNTS       “, note that this is a badly built db because it has text primary keys. (Not mine I must add!).

Now thats VERY basic and actually pretty useless but it DOES get you connected to the database and pulling data.

Named Queries

One of the very cool things about entity beans is you can build special NAMED QUERIES in them (ok you could create these on your SQL server as views, I know).

If you have a look at an entity bean you’ll see the first named query done for you. I added one to search the account types called TblAccountType.findByAccountType. Here’s the code in the entity bean.

@NamedQueries({@NamedQuery(name = "TblAccountType.findByAccountType", query = "SELECT t FROM TblAccountType t WHERE t.accountType LIKE :accountType")})

and in my Enterprsie Java bea here’s a function to pull the first result. It is very elegant as you can see.

public String testQuery(String sWhat) { 
        Query q = em.createNamedQuery("TblAccountType.findByAccountType"); 
        q.setParameter("accountType", sWhat); 
        TblAccountType cust; 
        try { 
        cust = (TblAccountType)q.getSingleResult(); 
        } catch (Exception e) { 
            return "Error: Not found"; 
        return cust.getAccountType(); 

Note this is a string so that if you call the routine with:-


You will get the first record beginning with ‘a’

More advanced stuff later on.


About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on February 13, 2008, in Howto. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: