Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JDBC with XML files.

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Thanked 0 Times in 0 Posts

    Question JDBC with XML files.

    Hello guys !

    I have a bit of situation on which i really can't find any answer on google : so i'm turning to you guys... so here it is:

    I would like to open XML files (.xml) on computer with a JDBC driver (instead of new File() function). So actually i want same functionality as Derby Enbedded Driver does, but instead of opening .db files i want to open .xml files. I'm using XML files for saving data like databases are - with tables, schemas and stuff. I can write something like my own JDBC driver, or extend from some driver that already does something like that.. I would like to make something, so i could connect like this:

    String url = "jdbc:mydriver:D:\\username\nameOfFile.xml";
    Class.forName ("com.mydriver.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url);
    System.out.println ("Database connection established");

    Once i'm connected with driver, i would also like to use SQL statements for my XML file database.
    I hope i was clear enough so you have idea what i'm trying to achieve here.

    Any tips, help would be much appreciated !


  2. #2
    Junior Member
    Join Date
    Aug 2012
    Thanked 0 Times in 0 Posts

    Default Re: JDBC with XML files.

    I am not sure if this will help you at all... but this is how I loaded my XML file.

    I created this method:
    	public static FileInputStream getFis() throws Exception {
    		FileInputStream fis = null;
    		// Use file input stream to connect to the properties XML file.
    		try {
    			fis = new FileInputStream("META-INF/test-config.xml");
    			System.out.println("XML file was loaded successfully.");
    			// FileInputStream error handling
    			} catch (FileNotFoundException e1) {
    				System.err.println("FileInputStream failed to " +
    						"load XML file. SQLException: " + e1.getMessage());
    		System.out.println("Data loaded: " + fis);
    		return fis;

    And then in my main method I called it like so:
    public static void main(String args[]){
    		// Call method to load XML file
    		try {
    			System.out.println("Trying to load the XML file.");
    		} catch (Exception e) {
    			System.out.println("Failure to successfully load the XML file.");

    Not entirely sure if this is what you meant, and if not I apologize You might not have a DaoFactory class.. but I imagine you could tweak the above code to work for you.

    As for the rest of the stuff you mentioned in your post (using SQl statements for your database, etc.. I cannot help but hopefully someone else will come along quickly )
    Last edited by Noob_Ichigo; August 29th, 2012 at 10:28 AM.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    North Wales
    My Mood
    Thanked 115 Times in 106 Posts
    Blog Entries

    Default Re: JDBC with XML files.

    That post lost me entirely.

    Why opt for this XML idea when it is an ideal situation to use an Embedded database?
    You don't really "load" your XML file in that code segment, but rather connect to it.

    Assuming the code format inside the XML file is syntactically correct, then you would need to use a DOM or SAX parser to extract the data in an orderly manner.
    But why not use an Embedded DB?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. The Following User Says Thank You to newbie For This Useful Post:

    curmudgeon (August 29th, 2012)

Similar Threads

  1. Replies: 3
    Last Post: August 3rd, 2012, 10:40 AM
  2. [SOLVED] files size for files inside the JAR file
    By Natherul in forum Java Theory & Questions
    Replies: 3
    Last Post: February 9th, 2012, 03:17 PM
  3. JDBC Problem - com.mysql.jdbc.Driver
    By icu222much in forum Java Servlet
    Replies: 2
    Last Post: November 21st, 2011, 11:54 PM
  4. Seraching through files in a folder for a pattern match inside the files.
    By dazzabiggs in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:35 AM
  5. Replies: 1
    Last Post: March 22nd, 2011, 06:59 PM