1 | /*
|
---|
2 | * Created on Dec 1, 2004
|
---|
3 | * Copyright (C) Andrea Schweer, 2004
|
---|
4 | *
|
---|
5 | * This file is part of the Greenstone Alerting Service.
|
---|
6 | * Refer to the COPYING file in the base directory of this package
|
---|
7 | * for licensing information.
|
---|
8 | */
|
---|
9 | package org.greenstone.gsdlas.database;
|
---|
10 |
|
---|
11 | import java.sql.*;
|
---|
12 | import java.sql.Connection;
|
---|
13 | import java.sql.DriverManager;
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * @author andrea
|
---|
18 | *
|
---|
19 | * TODO To change the template for this generated type comment go to
|
---|
20 | * Window - Preferences - Java - Code Style - Code Templates
|
---|
21 | */
|
---|
22 | public class DatabaseManager {
|
---|
23 |
|
---|
24 | private static DatabaseManager instance;
|
---|
25 | private Connection database;
|
---|
26 |
|
---|
27 | private DatabaseManager() {
|
---|
28 | // hide constructor
|
---|
29 | }
|
---|
30 |
|
---|
31 | public static DatabaseManager getInstance() {
|
---|
32 | if (instance == null) {
|
---|
33 | instance = new DatabaseManager();
|
---|
34 | }
|
---|
35 | return instance;
|
---|
36 | }
|
---|
37 |
|
---|
38 | public Connection getDatabaseConnection() throws DatabaseException {
|
---|
39 | if(database == null) {
|
---|
40 | String connectionString = "jdbc:mysql://localhost/alerting?user=gsdl3admin";
|
---|
41 | try {
|
---|
42 | Class.forName("com.mysql.jdbc.Driver").newInstance();
|
---|
43 | database = DriverManager.getConnection(connectionString);
|
---|
44 | } catch (Exception e) {
|
---|
45 | if (e instanceof SQLException) {
|
---|
46 | SQLException sqle = (SQLException) e;
|
---|
47 | System.err.println("SQLState: " + sqle.getSQLState());
|
---|
48 | }
|
---|
49 | throw new DatabaseException("Could not establish connection to database (" + connectionString + ")", e);
|
---|
50 | }
|
---|
51 | }
|
---|
52 | return database;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | }
|
---|