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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 3 of 3

Thread: Help with JavaDB embedded

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with JavaDB embedded

    First time working with Databases, I'm trying to make an application with an embedded JavaDB. I'm looking in my book Big Java, and I came across this class which the book uses,
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;
     
    /**
       A simple data source for getting database connections. 
    */
    public class SimpleDataSource
    {
       /**
          Initializes the data source.
          @param fileName the name of the property file that 
          contains the database driver, URL, username, and password
       */
       public static void init(String fileName)
             throws IOException, ClassNotFoundException
       {  
          Properties props = new Properties();
          FileInputStream in = new FileInputStream(fileName);
          props.load(in);
     
          String driver = props.getProperty("jdbc.driver");
          url = props.getProperty("jdbc.url");
          username = props.getProperty("jdbc.username");
          password = props.getProperty("jdbc.password");
     
          Class.forName(driver);
       }
     
       /**
          Gets a connection to the database.
          @return the database connection
       */
       public static Connection getConnection() throws SQLException
       {
          return DriverManager.getConnection(url, username, password);
       }
     
       private static String url;
       private static String username;
       private static String password;
    }
    I created a properties file to give url, name, and password.

    In another class, the code contains a line that says :
    SimpledataSource.init(args[0])

    I'm confused what this means. Does args[0] somehow point to the properties file?

    And since this is the first time I'm using databases, am I heading in the right approach with this project? I plan on creating connections throughout another class to communicate with the database.


  2. #2
    Junior Member
    Join Date
    Mar 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with JavaDB embedded

    I don't know if this is what you want, but why don't you try to create an application using netbeans. It will do this job for you. Connect to the database, create GUI etc.

    Read this tutorial: http://http://netbeans.org/kb/docs/java/gui-db.html

    Just to mentioned something that it's not written in that tutorial. Bydefault, netbeans create a database using Java Db Network Driver. But you want to create an application which is going to use Embeded Driver. For this, you have to follow this: http://http://forums.netbeans.org/ptopic36401.html

    and that in the last post user ni62 suggests.

    Now you will be able to create a project, but (at least for me) can't run, because of a problem at Persistence unit. To solve this, you have to change Persistence library from TopLink to EclipseLink.
    This can be done after opening persistence.xml from netbeans, and congiguring it properly. (you have to choose EclipseLink from a drop-down menu)
    Then you have to add EclipseLink to project's libraries.


    Now you are allright!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with JavaDB embedded

    nevermind: I found a topic that relates to my problem
    Last edited by jstn455; March 17th, 2011 at 08:02 PM.

Similar Threads

  1. JavaDB characterstics
    By jacinto in forum JDBC & Databases
    Replies: 4
    Last Post: August 5th, 2009, 03:11 PM