From derby-commits-return-3152-apmail-db-derby-commits-archive=db.apache.org@db.apache.org Fri Apr 28 20:37:53 2006 Return-Path: Delivered-To: apmail-db-derby-commits-archive@www.apache.org Received: (qmail 87074 invoked from network); 28 Apr 2006 20:37:52 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 28 Apr 2006 20:37:52 -0000 Received: (qmail 96572 invoked by uid 500); 28 Apr 2006 20:37:47 -0000 Delivered-To: apmail-db-derby-commits-archive@db.apache.org Received: (qmail 96504 invoked by uid 500); 28 Apr 2006 20:37:47 -0000 Mailing-List: contact derby-commits-help@db.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: "Derby Development" List-Id: Delivered-To: mailing list derby-commits@db.apache.org Received: (qmail 96454 invoked by uid 99); 28 Apr 2006 20:37:47 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 Apr 2006 13:37:47 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS X-Spam-Check-By: apache.org Received: from [192.87.106.226] (HELO ajax.apache.org) (192.87.106.226) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 28 Apr 2006 13:37:46 -0700 Received: from ajax.apache.org (localhost.localdomain [127.0.0.1]) by ajax.apache.org (Postfix) with ESMTP id 5EC18D49FE for ; Fri, 28 Apr 2006 21:37:25 +0100 (BST) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Apache Wiki To: derby-commits@db.apache.org Date: Fri, 28 Apr 2006 20:37:25 -0000 Message-ID: <20060428203725.19441.15040@ajax.apache.org> Subject: [Db-derby Wiki] Update of "DatabaseManagerSource" by DonaldMcLean X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Dear Wiki user, You have subscribed to a wiki page or wiki category on "Db-derby Wiki" for change notification. The following page has been changed by DonaldMcLean: http://wiki.apache.org/db-derby/DatabaseManagerSource New page: ## Please edit system and help pages ONLY in the moinmaster wiki! For more ## information, please see MoinMaster:MoinPagesEditorGroup. ##master-page:Unknown-Page ##master-date:Unknown-Date ##acl MoinPagesEditorGroup:read,write,delete,revert All:read #format wiki #language en == Template for Help Pages == {{{ package your.package.here; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.jdom.Document; import org.jdom.Element; import java.sql.Driver; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.io.File; import java.io.IOException; import java.util.HashSet; import java.util.List; import java.util.Properties; /** * Created: * Date: Sep 5, 2005 * Time: 1:57:13 AM */ public class DatabaseManager { public DatabaseManager(File dbFile) { connectToDatabase(dbFile); Document document = BotUtilities.getXMLResource(getClass()); Element root = document.getRootElement(); if (createFlag) { createTables(root); } createHibernateSessionFactory(root); instance = this; } public static DatabaseManager getInstance() { return instance; } public Connection getConnection() { return connection; } public Session getMainSession() { if (mainSession == null) { mainSession = (Session)sessions.openSession(connection); } return mainSession; } public void close() { sessions.close(); try { connection.close(); } catch (SQLException e) { System.out.println("[DatabaseManager.close] Unable to close connection."); } } private Connection connection; private SessionFactory sessions; private Session mainSession = null; private boolean createFlag = false; private void createTables(Element root) { Element createElement = root.getChild("createSql"); List createElements = createElement.getChildren(); String sql = null; try { Statement statement = connection.createStatement(); for (int i = 0; i < createElements.size(); i++) { Element element = (Element) createElements.get(i); sql = element.getText(); statement.execute(sql); } } catch (SQLException e) { System.out.println("[DatabaseManager.createTables] Failed to execute: '" + sql + "'."); e.printStackTrace(); } } private void connectToDatabase(File dbFile) { try { String url = createURL(dbFile); connection = driver.connect(url, null); } catch (Exception e) { System.out.println("[ApplicationDatabase : ApplicationDatabase]:" + " unable to create database."); e.printStackTrace(); } } /** * @param dbFile * @throws IOException */ private String createURL(File dbFile) throws IOException { String dbURL = "jdbc:derby:" + dbFile.getCanonicalPath(); if (!dbFile.exists()) { createFlag = true; dbURL += ";create=true"; } System.out.println("[ApplicationDatabase.createURL] URL is '" + dbURL + "'.cn"); return dbURL; } private void createHibernateSessionFactory(Element root) { HashSet classes = collectFeatureClasses(root); Properties properties = new Properties(); properties.put("hibernate.dialect", "org.hibernate.dialect.DerbyDialect"); properties.put("hibernate.show_sql", "true"); Configuration cfg = new Configuration(); cfg.addProperties(properties); for(Class aClass : classes) { System.out.println("[ApplicationDatabase.createHibernateSessionFactory]" + " adding class '" + aClass.getName() + "'."); cfg.addClass(aClass); } sessions = cfg.buildSessionFactory(); } private HashSet collectFeatureClasses(Element root) { HashSet result = new HashSet(); Element classesElement = root.getChild("Classes"); List classElements = classesElement.getChildren(); for (int i = 0; i < classElements.size(); i++) { Element element = (Element) classElements.get(i); result.add(BotUtilities.loadClass(element.getText())); } return result; } static private Driver driver; static private DatabaseManager instance = null; static { try { driver = (Driver) Class.forName("org.apache.derby.jdbc.EmbeddedDriver") .newInstance(); System.out.println("[ApplicationDatabase : static]: driver loaded successfuly."); } catch (Exception e) { System.out.println("[ApplicationDatabase : static]: unable to load driver."); e.printStackTrace(); } } } }}}