Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jmmv/db/Database.java


1   /*
2    * DiskCat - Disk Cataloguer
3    * Copyright (C) 2002 Julio Merino <slink@unixbsd.org>
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License as
7    * published by the Free Software Foundation; either version 2 of the
8    * License, or (at your option) any later version.
9   
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * General Public License for more details.
14  
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18   * USA
19   */
20  
21  package jmmv.db;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.sql.Connection;
26  import java.sql.Statement;
27  import java.sql.ResultSet;
28  import java.sql.DriverManager;
29  import java.sql.SQLException;
30  import java.util.LinkedList;
31  import java.util.Properties;
32  
33  /**
34   *
35   */
36  public class Database {
37      private static Database db = null;
38      private Properties props;
39      private Connection con;
40  
41      private Database() {
42    /* Read the configuration file */
43    File propsFile = new File("Database.properties");
44    props = new Properties();
45    try {
46        FileInputStream fis = new FileInputStream(propsFile);
47        props.load(fis);
48        fis.close();
49    } catch (Exception e) {
50        System.out.println("Error reading database properties");
51    }
52  
53    /* Look for the database driver class */
54    try {
55        Class.forName(props.getProperty("DATABASE_DRIVER_CLASS"));
56    } catch (Exception e) {
57        System.out.println("No database driver found");
58        return;
59    }
60  
61    /* Open a connection with the database server */
62    try {
63        con = DriverManager.getConnection
64      (props.getProperty("DATABASE_CONNECTION_STRING"),
65       props.getProperty("DATABASE_USERNAME"),
66       props.getProperty("DATABASE_PASSWORD"));
67    } catch (Exception e) {
68        System.out.println("Cannot open database");
69        return;
70    }
71    System.out.println("Connected to database engine");
72      }
73  
74      public static Database getInstance() {
75    if (db == null) db = new Database();
76    return db;
77      }
78  
79      public Connection getConnection() { return con; }
80  
81      public LinkedList tableToList(String table, RegisterFactory factory,
82            int regtype, String sortfield) {
83    LinkedList regs = new LinkedList();
84    try {
85        Statement st = con.createStatement();
86        ResultSet rs = st.executeQuery("SELECT * FROM " + table + " ORDER BY " + sortfield);
87        while (rs.next()) {
88      Register reg = factory.getRegister(regtype);
89      reg.parseResultSet(rs);
90      regs.add((Register) reg);
91        }
92        st.close();
93    } catch (SQLException e) {
94        System.out.println("FIXME: Unhandled exception: " + e);
95    }
96  
97    return regs;
98      }
99  
100     public void finalize() {
101   try {
102       con.close();
103       System.out.println("Database has shutted down");
104   } catch (Exception e) {
105       System.out.println("An error ocurred while closing connection");
106   }
107     }
108 }