Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2006

Tip: Looking for answers? Try searching our database.

xml property file...for say db connections?

Thread view: 
timasmith@hotmail.com - 26 Apr 2006 23:29 GMT
I have code that connects to MS Access *and* code which connects to an
Oracle database.

Does anyone have a code snippet which reads an XML file to extract
properties I can use to dynamically determine my configuration.

This is less of a db problem - more of extracting properties from an
XML config file problem.  Ideally my file is something like this

<db>
<environment name="default">
 <driver>MSAccess</driver>
 <datasource>C:\temp\my.mdb</datasource>
</environment>
<environment name="production">
 <driver>Oracle</driver>
 <datasource>tnsnamesname</datasource>
 <node>mynode</node>
 <port>1521</port>
 <instance>prod1</instance>
</environment>
</db>

Is there any existing java framework class that simplifies this task?

thanks

Tim
Bjorn Abelli - 26 Apr 2006 23:59 GMT
<timasmith@hotmail.com> wrote...

> I have code that connects to MS Access *and* code
> which connects to an Oracle database.
>
> Does anyone have a code snippet which reads an
> XML file to extract properties I can use to
> dynamically determine my configuration.

I guess you have these properties in a special object?

Then you can simply use java.beans.XMLCoder and XMLDecoder to serialize that
object in XML.

I found a hack I made several years ago for an app, where I easily could
shift between different connections, not only Oracle and MS Access... ;-)

It's not pretty, but I don't have the time to fix it.

Hopefully you can look at it and see if it's of any use to you.

======================================

 private void saveConnections ()
 {
   try
   {
     XMLEncoder e =
       new XMLEncoder(
         new BufferedOutputStream(
           new FileOutputStream("Connections.xml")));

     Vector cbm = new Vector(connections.values());

     int antal = cbm.size();

     for (int i = 0; i < antal; i++)
     {
       ConnectionProperty c =
         (ConnectionProperty)
            cbm.get(i);
       e.writeObject(c);
     }

     e.close();
   }
   catch (FileNotFoundException fex)
   {
     fex.printStackTrace();
   }
 }

 private void loadConnections ()
 {
   java.beans.XMLDecoder d = null;

   try
   {
     d = new XMLDecoder(
         new BufferedInputStream(
           new FileInputStream("Connections.xml")));

     while (true)
     {
       ConnectionProperty ci = (ConnectionProperty) d.readObject();
       connections.put(ci.getLabel(), ci);
     }
   }
   catch (FileNotFoundException fex)
   {
     fex.printStackTrace();
   }
   catch (ArrayIndexOutOfBoundsException aix)
   {
     d.close();
   }
 }

======================================

And the class I used for storing the
Connection properties...

======================================

import java.io.*;

public class ConnectionProperty implements Serializable
{
 private String label;
 private String driver;
 private String jar;
 private String connectionString;

 public ConnectionProperty (String l, String d, String c, String j)
 {
   label = l;
   driver = d;
   connectionString = c;
   jar = j;
 }

 public ConnectionProperty ()
 {
   // For the "Serializability"
 }

 public String toString()
 {
   return label;
 }

 public String getDriver()
 {
   return driver;
 }

 public void setDriver(String d)
 {
   driver = d;
 }

 public String getJar()
 {
   return jar;
 }

 public void setJar(String d)
 {
   jar = d;
 }

 public String getLabel()
 {
   return label;
 }

 public void setLabel(String s)
 {
   label = s;
 }

 public String getConnectionString()
 {
   return connectionString;
 }

 public void setConnectionString(String c)
 {
   connectionString = c;
 }
}

======================================

// Bjorn A
Arvind - 26 Apr 2006 23:59 GMT
> I have code that connects to MS Access *and* code which connects to an
> Oracle database.
[quoted text clipped - 20 lines]
>
> Is there any existing java framework class that simplifies this task?

There are many frameworks that let you map XML - Java and gets you
going...like Digester, Castor XML etc....

You can even consider storing such environment specific thingies in a
database too or different property files !

No matter what route you choose, make sure, the load of such
information happens once and remains a singleton since that time

--
Arvind
Venkatesh - 27 Apr 2006 05:01 GMT
Java has an in-built class called "Properties (java.util.Properties)".
U can load properties into this object from XML file also (there is a
method called readFromXML). For more details, see the API documentation
of Properties

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Properties.html

-Venkatesh
RC - 27 Apr 2006 13:40 GMT
> Does anyone have a code snippet which reads an XML file to extract
> properties I can use to dynamically determine my configuration.

Do more research on DOM and/or SAX XML parsers.
They are existed for many years, they are standard.

Go to the link below click
Xalan and/or Xerces
I think O'Reilly book(s) Java XML will teach you how to use these
DOM/SAX XML parsers

 http://xml.apache.org/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.