Java tutorial
/* * Copyright 2009 Martin Palma * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package it.unibz.inf.xmlssd.metadator.sqlite; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * Class for interfacing the SQLite database. * @author Martin Palma <martin@palma.bz> * */ public class SQLiteManager { /** Logger **/ private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SQLiteManager.class); private Connection aConnection; /** * Constructs the object. */ public SQLiteManager() { try { Class.forName("org.sqlite.JDBC"); // Establish connection aConnection = DriverManager.getConnection("jdbc:sqlite:" + System.getProperty("metadator.db")); // Create the Database Schema createSchema(); } catch (ClassNotFoundException e) { log.fatal("Class Not Found: " + e.getMessage()); } catch (SQLException e) { log.error("SQL Error: " + e.getMessage()); } } /** * Method which parses the given XML documents and inserts it's data * into the Database. * @param doc */ public void put(Document doc) { List list = doc.selectNodes("//metadator/*"); for (Iterator i = list.iterator(); i.hasNext();) { Element element = (Element) i.next(); try { Camera camera = new Camera(aConnection); int camera_id = camera.put(element); element.addAttribute("camera_id", Integer.toString(camera_id)); Photo photo = new Photo(aConnection); photo.put(element); } catch (SQLException e) { log.error("SQL Error: " + e.getMessage()); } } } /** * Method which delete the item with the given id from the database. * @param id * @return */ public Boolean delete(int id) { try { Photo photo = new Photo(aConnection); if (photo.delete(id) == 1) return true; } catch (SQLException e) { log.error("SQL Error: " + e.getMessage()); } return false; } /** * Method which constructs a XML document from the data stored in the * Database. * @return */ public Document get() { Document document = DocumentHelper.createDocument(); try { Photo photo = new Photo(aConnection); document = photo.get(); } catch (SQLException e) { log.error("SQL Error in getting XML: " + e.getMessage()); } return document; } /** * Method which creates the DB schema. * @throws SQLException */ private void createSchema() { try { Statement stmt = aConnection.createStatement(); stmt.executeUpdate(Photo.CREATE); stmt.executeUpdate(Camera.CREATE); stmt.close(); } catch (SQLException e) { log.fatal("Could not create Database schema:" + e.getMessage()); } } }