Weblog

All | Personal | Sun
« Previous month (Apr 2006) | Main | Next month (Jun 2006) »
20060605 Monday June 05, 2006

Embedding JavaDB in your desktop application JavaDB is very lightweight database that might be used for storing data in your desktop applications. I would like to show how to use embedded JavaDB in your applications. Let's to create desktop application that uses embedded database. In his mode the Derby database engine runs inside the same Java Virtual Machine (JVM) as the application. At first you need to download Derby database from here. Then, create new J2SE project and add derby.jar from Derby DB to project's classpath. We should load EmbeddedDriver, connect, add SQL code for inserting, selecting application data and we are done. See code snapshot:

    Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
    Connectionconn= 
     DriverManager.getConnection("jdbc:derby:myDB;create=true");
    conn.createStatement().execute("CREATE TABLE ...");
    ..... 
  
Now, run your applications and myDB directory with JavaDB files should be created in working directory. If you want to change a location when thies directory is created use derby.system.home Java system property. Persisting application data with embedded JavaDB is easy, isn't it? Posted by pblaha ( Jun 05 2006, 05:40:22 PM CEST ) Permalink

JDBCRealm Tutorial Edson Carlos Ericksson Richter just completed his tutorial about JDBC authentication in Glassfish. He wrote all steps that are needed for using this authentication in Glassfish b.48. Edson is the most productive participant in Netcat program. I believe he will publish many J2EE hints in his new blog. Posted by pblaha ( Jun 05 2006, 04:47:04 PM CEST ) Permalink Comments [1]

20060602 Friday June 02, 2006

How to persist entity classes in XML file I described how to persists your objects in database using Toplink in previous prost. However, sometimes you might want to save your objects in XML. It's very simple with NetBeans 5.5 since we included JAXB library in IDE. Let's to create application that stores information about orders in xml instead of in DB. This approach might be used when you want to send your objects via network. Just create new project in Netbeans 5.5 and select Libraries node in project tab, righ-click and choose Add Library item and select JAXB 2.0 in list, don't forget to click OK button.
Now, create Item class that holds info related to one item. This class might have name and amount fields and appropriate set/get methods for them. Since, I would like to have XML schema type with itemOrder name in generated schema then put following annotation above class definition:

    @XmlType(name="orderItem")
    public class Item implements Serializable {
    ........
  
Order class is similar as Item. Order has reference to items and also order number and due date. This class is top level class and should be annotated with the @XmlRootElement annotation. See code snapshot:
   @XmlRootElement()
    public class Order implements Serializable {    
    
    private String orderNumber;
    private Calendar dueDate;
    private Collection  items;
 
Working with these objects is very simple and intuitive. I will show only storing objects in XML but creation of objects from XML or update them is similar. See storing order in XML file:
   JAXBContext ctx = JAXBContext.newInstance(Order.class);
        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);       
        File xml = new File("order.xml");
        
        Order order = new Order();
        order.setOrderNumber("FSX-2345-5454");
        order.addItem(new Item("book", 1));
        order.addItem(new Item("pen", 1));
        
        m.marshal(order, new FileOutputStream(xml));
 
When you run application, you can check order.xml. However, what's about creation schema from classes, it's easy as well. Only add following target in build.xml and rebuild project.
   <target name="-pre-compile">
        <taskdef name="schemagen"
        classname="com.sun.tools.jxc.SchemaGenTask"
        classpath="${javac.classpath}"/>
        <echo message= "Generating schemas..." />
        <mkdir dir="${build.dir}/schemas" />
        <schemagen destdir="${build.dir}/schemas" classpath="${javac.classpath}">
            <src path="${src.dir}" />
            <schema file="order.xsd"/>
        </schemagen>
    </target>
 
Working with JAXB is very easy in Netbeans 5.5. You don't need to download JWSDP pack since JAXB is bundled in IDE. In this post only java to XML binding is presented but you can simple create XML to Java as well. It means you will create objects from existing schema. Posted by pblaha ( Jun 02 2006, 01:50:28 PM CEST ) Permalink

Calendar

RSS Feeds

Search

Links

Navigation

Referers

Older blog entries