KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > rentacar > persistance > database > DatabaseManager


1 package org.objectweb.rentacar.persistance.database;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.sql.DriverManager JavaDoc;
6 import java.sql.SQLException JavaDoc;
7
8 import javax.xml.parsers.DocumentBuilder JavaDoc;
9 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
10 import javax.xml.parsers.ParserConfigurationException JavaDoc;
11
12 import org.dbunit.DatabaseUnitException;
13 import org.dbunit.database.DatabaseConnection;
14 import org.dbunit.database.IDatabaseConnection;
15 import org.dbunit.dataset.IDataSet;
16 import org.dbunit.dataset.xml.FlatXmlDataSet;
17 import org.dbunit.operation.DatabaseOperation;
18 import org.hibernate.HibernateException;
19 import org.hibernate.cfg.AnnotationConfiguration;
20 import org.hibernate.tool.hbm2ddl.SchemaExport;
21 import org.hsqldb.Server;
22 import org.w3c.dom.Document JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24
25 /**
26  * Database manager
27  */

28 public class DatabaseManager {
29
30     /**
31      * Instance of the server
32      */

33     private Server server = new Server();
34
35     /**
36      * Start the database server
37      * @param host host
38      * @param port port
39      * @param dbName name of the database
40      * @param dbPath path of the database
41      */

42     public void runServer(String JavaDoc host, String JavaDoc port, String JavaDoc dbName, String JavaDoc dbPath) {
43         server.setAddress(host);
44         server.setDatabaseName(0, dbName);
45         server.setDatabasePath(0, dbPath);
46         server.setPort(Integer.parseInt(port));
47         server.setSilent(true);
48         server.setTrace(false);
49         server.start();
50     }
51
52     /**
53      * Shutdown the hsqldb server
54      */

55     public void shutdownServer() {
56         server.stop();
57     }
58
59     /**
60      * Build database schema
61      * @param url
62      * @param driver
63      * @param dialect
64      * @param userName
65      * @param password
66      * @throws DatabaseException
67      */

68     public void buildDatabaseSchema(String JavaDoc url, String JavaDoc driver, String JavaDoc dialect, String JavaDoc userName, String JavaDoc password) throws DatabaseException {
69         AnnotationConfiguration conf = new AnnotationConfiguration();
70         try {
71             DocumentBuilderFactory JavaDoc fact = DocumentBuilderFactory.newInstance();
72             DocumentBuilder JavaDoc builder = fact.newDocumentBuilder();
73             Document JavaDoc document = builder.parse(DatabaseManager.class.getClassLoader().getResourceAsStream("hibernate.cfg.xml"));
74             conf.configure(document);
75             conf.setProperty("hibernate.connection.username", userName);
76             conf.setProperty("hibernate.connection.password", password);
77             conf.setProperty("hibernate.connection.url", url);
78             conf.setProperty("hibernate.dialect", dialect);
79             conf.setProperty("hibernate.connection.driver_class", driver);
80             SchemaExport export = new SchemaExport(conf);
81             export.create(false, true);
82         }
83         catch (HibernateException e) {
84             throw new DatabaseException("Error building the schema", e);
85         }
86         catch (SAXException JavaDoc e) {
87             throw new DatabaseException("Error building the schema", e);
88         }
89         catch (IOException JavaDoc e) {
90             throw new DatabaseException("Error building the schema", e);
91         }
92         catch (ParserConfigurationException JavaDoc e) {
93             throw new DatabaseException("Error building the schema", e);
94         }
95     }
96
97     /**
98      * @param host
99      * @param port
100      * @param dbName
101      * @param dsFileName
102      * @throws DatabaseException
103      */

104     public void fillDatabase(String JavaDoc host, String JavaDoc port, String JavaDoc dbName, String JavaDoc dsFileName) throws DatabaseException {
105         IDatabaseConnection connection = null;
106         try {
107             Class.forName("org.hsqldb.jdbcDriver");
108             connection = (IDatabaseConnection) new DatabaseConnection(DriverManager.getConnection("jdbc:hsqldb:hsql://" + host + " :" + port + "/" + dbName, "sa", ""));
109             IDataSet dataSet = new FlatXmlDataSet(DatabaseManager.class.getClassLoader().getResourceAsStream(dsFileName));
110             DatabaseOperation.DELETE.execute(connection, dataSet);
111             DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
112         }
113         catch (DatabaseUnitException e) {
114             throw new DatabaseException("Error filling the database", e);
115         }
116         catch (SQLException JavaDoc e) {
117             throw new DatabaseException("Error filling the database", e);
118         }
119         catch (FileNotFoundException JavaDoc e) {
120             throw new DatabaseException("Error filling the database", e);
121         }
122         catch (IOException JavaDoc e) {
123             throw new DatabaseException("Error filling the database", e);
124         }
125         catch (Exception JavaDoc e) {
126             throw new DatabaseException("Error filling the database", e);
127         }
128         finally {
129             try {
130                 if (connection != null) {
131                     connection.close();
132                 }
133             }
134             catch (SQLException JavaDoc e) {
135                 throw new DatabaseException("Error filling the database", e);
136             }
137         }
138     }
139
140 }
141
Popular Tags