View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  package com.finalist.util.genelv.db;
19  
20  import java.sql.*;
21  import java.util.*;
22  import java.io.*;
23  import java.net.*;
24  
25  import org.apache.log4j.Category;
26  
27  /***
28   * This class does the lookup for the database table to locate the error code / error type mapping
29   *
30   * @author  Gerard van de Weerd
31   * @created 19 april 2002
32   */
33  public class GenElvConnectionManager implements Serializable {
34     static Category log = Category.getInstance(GenElvConnectionManager.class);
35  
36     static private GenElvConnectionManager instance;
37  
38     static String MY_SQL_DRIVER_CLASS = "org.gjt.mm.mysql.Driver";
39     static String DB_URL = "jdbc:mysql://192.168.0.60/genelv";
40     static String DB_USER = "genelv";
41     static String DB_PASSWORD = "genelv";
42  
43  
44     /***
45      * Default constructor. Loads my sql driver class;
46      */
47     protected GenElvConnectionManager() throws ClassNotFoundException {
48        Class.forName(MY_SQL_DRIVER_CLASS);
49     }
50  
51  
52     /***
53      *
54      */
55     static public GenElvConnectionManager getInstance() throws SQLException {
56        System.out.println("GenElvConnectionManager getInstance()");
57        if (instance == null) {
58           try {
59              instance = new GenElvConnectionManager();
60           }
61           catch (ClassNotFoundException e) {
62              e.printStackTrace();
63              instance = null;
64              throw new SQLException("Class " + MY_SQL_DRIVER_CLASS + " cannot be found.");
65           }
66        }
67        return instance;
68     }
69  
70  
71     /***
72      *
73      */
74     protected Connection getConnection() throws SQLException {
75        try {
76           Class.forName(MY_SQL_DRIVER_CLASS);
77        }
78        catch (Exception e) {
79        }
80        return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
81     }
82  
83  
84     /***
85      * Create return connection calling methode is responsable for closing
86      * this connection
87      * @throws SQLException When the lookup failed or if there is no database
88      * @return The Connection
89      */
90     static public Connection connect() throws SQLException {
91        return getInstance().getConnection();
92     }
93  
94  }