1
package org.hisp.dhis.dashboard.util;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.io.BufferedReader;
32
import java.io.FileNotFoundException;
33
import java.io.FileReader;
34
import java.io.IOException;
35
import java.sql.Connection;
36
import java.sql.DriverManager;
37
import java.util.ArrayList;
38
import java.util.List;
40
import javax.xml.parsers.DocumentBuilder;
41
import javax.xml.parsers.DocumentBuilderFactory;
43
import org.w3c.dom.Document;
44
import org.w3c.dom.Element;
45
import org.w3c.dom.Node;
46
import org.w3c.dom.NodeList;
47
import org.xml.sax.SAXException;
48
import org.xml.sax.SAXParseException;
50
public class DBConnection
53
Connection con = null;
55
String dbConnectionXMLFileName = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator
56
+ "db" + File.separator + "DBConnections.xml";
59
* To retrieve the db details from xml file
61
public List getDBDeatilsFromXML()
63
List<String> li = null;
66
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
67
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
68
Document doc = docBuilder.parse( new File( dbConnectionXMLFileName ) );
70
NodeList listOfDBConnections = doc.getElementsByTagName( "db-connection" );
72
Node dbConnectionsNode = listOfDBConnections.item( 0 );
73
li = new ArrayList<String>();
74
if ( dbConnectionsNode.getNodeType() == Node.ELEMENT_NODE )
76
Element dbConnectionElement = (Element) dbConnectionsNode;
78
NodeList dbUserNameList = dbConnectionElement.getElementsByTagName( "uname" );
79
Element dbUserNameElement = (Element) dbUserNameList.item( 0 );
80
NodeList textDBUNList = dbUserNameElement.getChildNodes();
81
li.add( 0, ( textDBUNList.item( 0 )).getNodeValue().trim() );
83
NodeList dbUserPwdList = dbConnectionElement.getElementsByTagName( "upwd" );
84
Element dbUserPwdElement = (Element) dbUserPwdList.item( 0 );
85
NodeList textDUPwdList = dbUserPwdElement.getChildNodes();
86
li.add( 1, ( textDUPwdList.item( 0 )).getNodeValue().trim() );
88
NodeList dbURLList = dbConnectionElement.getElementsByTagName( "dburl" );
89
Element dbURLElement = (Element) dbURLList.item( 0 );
90
NodeList textDBURLList = dbURLElement.getChildNodes();
91
li.add( 2, ( textDBURLList.item( 0 )).getNodeValue().trim() );
93
NodeList dbStateNameList = dbConnectionElement.getElementsByTagName( "state-name" );
94
Element dbStateNameElement = (Element) dbStateNameList.item( 0 );
95
NodeList textDBSNameList = dbStateNameElement.getChildNodes();
96
li.add( 3, ( textDBSNameList.item( 0 )).getNodeValue().trim() );
99
catch ( SAXParseException err )
101
System.out.println( "** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId() );
102
System.out.println( " " + err.getMessage() );
104
catch ( SAXException e )
106
Exception x = e.getException();
107
((x == null) ? e : x).printStackTrace();
109
catch ( Throwable t )
116
public List getDBDeatilsFromHibernate()
118
String path = System.getProperty( "user.home" ) + File.separator + "dhis" + File.separator
119
+ "hibernate.properties";
120
FileReader fr = null;
121
BufferedReader input = null;
123
List<String> li = new ArrayList<String>();
126
fr = new FileReader( path );
127
input = new BufferedReader( fr );
129
String s = input.readLine();
130
while ( s instanceof String )
132
if ( s.contains( "jdbc:mysql:" ) )
135
* String tempS2[] = s.split("/"); String dbName =
136
* "jdbc:mysql://localhost/"+tempS2[tempS2.length-1].substring(0,
137
* tempS2[tempS2.length-1].indexOf('?'));
138
* System.out.println("DBName : "+dbName); li.add(0,dbName);
140
String tempS2[] = s.split( "=" );
141
String dbName = tempS2[1].substring( 0, tempS2[1].indexOf( '?' ) ).trim();
142
//System.out.println( "DBName : " + dbName );
145
if ( s.contains( "hibernate.connection.username" ) )
147
String tempS2[] = s.split( "=" );
148
//System.out.println( "UserName : " + tempS2[tempS2.length - 1].trim() );
149
li.add( 1, tempS2[tempS2.length - 1].trim() );
151
if ( s.contains( "hibernate.connection.password" ) )
153
String tempS2[] = s.split( "=" );
154
//System.out.println( "PassWord : " + tempS2[tempS2.length - 1].trim() );
155
li.add( 2, tempS2[tempS2.length - 1].trim() );
157
// System.out.println(s);
158
s = input.readLine();
161
catch ( FileNotFoundException e )
163
System.out.println( e.getMessage() );
165
catch ( IOException e )
167
System.out.println( e.getMessage() );
178
catch ( Exception e )
180
System.out.println( e.getMessage() );
185
}// getDBDeatilsFromHibernate end
187
public Connection openConnection()
193
// To get From XML File
194
// List li = (ArrayList)getDBDeatilsFromXML();
195
// String userName = (String) li.get(0);
196
// String userPass = (String) li.get(1);
197
// String urlForConnection = (String) li.get(2);
199
// To get From Hibernate.Properties File
200
List li = (ArrayList) getDBDeatilsFromHibernate();
201
String urlForConnection = (String) li.get( 0 );
202
String userName = (String) li.get( 1 );
203
String userPass = (String) li.get( 2 );
205
// Direct DBConnection
206
// String userName = "dhis";
207
// String userPass = "";
208
// String urlForConnection = "jdbc:mysql://localhost/jh_dhis2";
210
Class.forName( "com.mysql.jdbc.Driver" ).newInstance();
211
con = DriverManager.getConnection( urlForConnection, userName, userPass );
213
catch ( Exception e )
215
System.out.println( "Exception while opening connection : " + e.getMessage() );
219
} // openConnection end
221
public void closeConnection()
233
catch ( Exception e )
235
System.out.println( e.getMessage() );
238
} // closeConnection end