Ergebnis 1 bis 2 von 2
  1. #1
    Newbie Avatar von GamerForSale
    Registriert seit
    17.05.2007
    Beiträge
    21

    Standard Derby Database Tutorial

    Wer mit Java zu tun hat, kommt irgendwann natürlich auch mit Datenbanken in Berührung. Apache stellt mit Derby eine leichtgewichtige und leistungsfähige (aber manchmal auch etwas ... *eigensinnige*) Datenbank zur Verfügung. Zudem sind seit dem JDK 6 nativ Treiber für die Derby-Datenbank integriert, weshalb das Aufsetzen einer Derby-Datenbank wirklich recht unkompliziert ist. Ich hab in meinem (sich noch im Aufbau befindlichen) Wiki nun ein komplettes Tutorial zu dem Thema Derby & Java (mit Eclipse) erstellt, was ich der Einfachheit halber einfach hier noch einmal Abdrucke.

    Den Original Artikel findet ihr unter : http://wiki.gamerforsale.com/index.php/Java_Database

    Bei Fragen oder Anregungen meldet euch einfach hier im Thread oder schreibt mich per PN/Email an.

    Viel Spaß,
    Nico
    __________________________________________________ ______________

    Those of you, who are working in Java, will sooner or later need databases as well. Apache offers a very powerful, and flexible Java-Database called "Derby". The reason why Derby would be a good Database to start with is, that as of JDK 6, DerbyDrivers are natively integrated, which eases the overall setup of the Database especially for Java-newcomers. In the following I'll provide a brief but comprehensible Tutorial on how to setup up and use a Derby-Database in Eclipse.

    The article is also posted on my wikipage, which can be reached by following this link : http://wiki.gamerforsale.com/index.php/Java_Database

    In case you have any questions or suggestions, please don't hesitate to voice them either right here or by sending me a PN/Email.

    Enjoy,
    Nicolai
    __________________________________________________ ______________

    Und nun zum Tutorial :

    Derby and Java


    Useful Links

    Derby Homepage

    Derby Documentation

    Download Derby

    Getting Started

    While writing this article, the current Version of Derby is Version 10.2.2.0. Simply head over to the download-Section on the official Derby-Website (link given above) and download the current Release - mind, that actually all you need as of JDK 6.0 is the derby.jar (no drivers needed if you go for the embedded ones shipped with JDK 6), hence the "lib-Distribution-Package" will do the Job. After downloading the package, unpack it into a temporary folder and safely delete everything from it, except the derby.jar.
    Derby meets Eclipse

    Now its about time to integrate your Derby-Database into an Eclipse-Project. I'll assume, that you are already more or less familiar with the Eclipse-IDE. If that isn't the case, best check out the Eclipse Documentation. Open your Eclipse-IDE and create a New Project. Now open your Eclipse-Workspace (the Location where your newly created Projects are kept) either in a Terminal or a Window. Switch to the folder of your current Project (Top-Level) and create a Folder called "lib" - here we're going to store all ".jar"-Files that are used in our Project. Copy the derby.jar into this lib-Folder.
    Now switch back to Eclipse and refresh it by right-clicking on your Project and hitting the "Refresh-Button"). The lib-folder should now pop up and list display the derby.jar as well. Now we're going to add our derby.jar to the build-path of our Project. To accomplish this, simply right-click on the derby.jar and chose "Build->Add to Build-Path".
    As for the configuration we're pretty much done. Now comes the fun-part : Coding :-) ...
    Derby meets Java

    First of all I recommend you to create a class called something like "DatabaseManager", which offers some basic functionality for administrating and accessing the Database. Since only one instance of this DatabaseManager is needed, I would setup the class as a Singleton in order to provide instant access without creating thousands of instances. The DatabaseManager will provide Connections to the Database on request and contain some basic Database-Operations according to the CRUD-Pattern (Create, Read, Update, Delete). In the following I'll provide a class-template which will give you an idea of how to structure your DatabaseManager.


    Code:
    public class DatabaseManager
    {
        private static String dbURL    : "jdbc:derby:softcondb;create=true";
        private static String dbDriver : "org.apache.derby.jdbc.EmbeddedDriver";
        //Self-Reference
        private static DatabaseManager self;
        
        //Private Constructor which Sets up the Database-Driver
        private DatabaseManager()
        {
            Class.forName(dbDriver).newInstance();
        }
    
        //Method to acquire an instance of the DatabaseManager
        public static DatabaseManager getInstance()
        {
            if (self == null)
                self = new DatabaseManager();
            return self;
        }
        
        //This method returns a Connection to the Database
        public Connection getconnection() throws SQLException
        {
            Connection con = DriverManager.getConnection(dbURL);
            con.setAutoCommit(true);
            return con;
        }
        
        //Add some Methods for handling DB-Requests such as Inserts,Updates or Selects
        //Consider using static (String) class-variables for the DB-Commands in order
        //to make future-adjustments easier and maintainance overall
    }
    You may now call the above Manager from pretty much any class which needs access to the Database, in the following I'll demonstrate how to make a request and cleanup afterwards (Closing the Database-Connection and freeing the ResultSets).

    Code:
    public void getAllOrders() trows SQLException
    {
        //We set these variables to null, so that we can check for null in the finally-block
        //which will save us from running into a nullpointer-exception in the finally-block
        Connection con = null; 
        Statement getOrders = null;
        
        try
        {
            //Get a Connection to the Database by using the DatabaseManager
            con = DatabaseManager.getInstance().getConnection();
            //Create a Statement
            getOrders = con.createStatement();
            //Execute a Statement
            ResultSet resultGetOrders = getOrders.executeQuery("SELECT * FROM Orders");
            //Do somemthing with the Result Set
            
            //Free the ResultSet
            resultGetOrders.close();
        }
        finally
        {
            try
            {
                if (con != null)
                    con.close(); 
                if(getOrders != null)
                    getOrders.close();            
            }
            catch (SQLException e)
            {
                //Consider throwing a self-made exception here which specifies the error
            }    
        }
    }
    Geändert von GamerForSale (15.07.2007 um 13:54 Uhr) Grund: Tag misplaced

  2. #2
    Einmalposter
    Registriert seit
    30.10.2011
    Beiträge
    1

    Standard

    Hallo GamerForSale,
    gibt es das Wiki noch? Bei mir geht das Link ins Lehre.

    Vielen Dank.

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •