001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/contrib/phpbb2mvnforum/src/org/mvnforum/util/DBUtils.java,v 1.6 2007/01/15 10:27:31 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.6 $
005: * $Date: 2007/01/15 10:27:31 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author:
039: */
040: package org.mvnforum.util;
041:
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.FileNotFoundException;
045: import java.io.FileOutputStream;
046: import java.io.IOException;
047: import java.sql.Connection;
048: import java.sql.DriverManager;
049: import java.sql.ResultSet;
050: import java.sql.SQLException;
051: import java.sql.Statement;
052:
053: import javax.xml.parsers.DocumentBuilder;
054: import javax.xml.parsers.DocumentBuilderFactory;
055: import javax.xml.parsers.ParserConfigurationException;
056: import javax.xml.transform.Result;
057: import javax.xml.transform.Source;
058: import javax.xml.transform.Transformer;
059: import javax.xml.transform.TransformerConfigurationException;
060: import javax.xml.transform.TransformerException;
061: import javax.xml.transform.TransformerFactory;
062: import javax.xml.transform.dom.DOMSource;
063: import javax.xml.transform.stream.StreamResult;
064:
065: import org.w3c.dom.Document;
066:
067: public class DBUtils {
068:
069: private static Document doc = null;
070:
071: static {
072: try {
073: DocumentBuilder builder = DocumentBuilderFactory
074: .newInstance().newDocumentBuilder();
075: doc = builder.newDocument();
076: doc.appendChild(doc.createElement("MvnForum"));
077: } catch (ParserConfigurationException e) {
078: e.printStackTrace();
079: }
080: }
081:
082: /**
083: * Get a connection from the connection pool. The returned connection
084: * must be closed by calling DBUtils.closeConnection()
085: * @return : a new connection from the pool if succeed
086: * @throws SQLException : if cannot get a connection from the pool
087: * @throws ClassNotFoundException
088: * @throws IOException
089: * @throws FileNotFoundException
090: */
091: public static Connection getPhpbbConnection() throws SQLException {
092:
093: String phpurl = "jdbc:mysql://" + Phpbb2MvnforumConfig.PHP_HOST
094: + "/" + Phpbb2MvnforumConfig.PHP_DB;
095:
096: Connection conection = DriverManager.getConnection(phpurl,
097: Phpbb2MvnforumConfig.PHP_USER,
098: Phpbb2MvnforumConfig.PHP_PASS);
099:
100: return conection;
101: }
102:
103: /**
104: * Get a connection from the connection pool. The returned connection
105: * must be closed by calling DBUtils.closeConnection()
106: * @return : a new connection from the pool if succeed
107: * @throws SQLException : if cannot get a connection from the pool
108: * @throws ClassNotFoundException
109: * @throws IOException
110: * @throws FileNotFoundException
111: */
112: public static Connection getMvnConnection() throws SQLException {
113:
114: String phpurl = "jdbc:mysql://" + Phpbb2MvnforumConfig.MVN_HOST
115: + "/" + Phpbb2MvnforumConfig.MVN_DB;
116:
117: Connection conection = DriverManager.getConnection(phpurl,
118: Phpbb2MvnforumConfig.MVN_USER,
119: Phpbb2MvnforumConfig.MVN_PASS);
120:
121: return conection;
122: }
123:
124: /**
125: * Use this method to close the ResultSet
126: * @param rs : the resultset that needs to be closed
127: */
128: public static void closeResultSet(ResultSet rs) {
129: if (rs != null) {
130: try {
131: rs.close();
132: } catch (SQLException e) {
133: e.printStackTrace();
134: }
135: }
136: }
137:
138: /**
139: * Use this method to close the Statement
140: * @param statement : the statement that needs to be closed
141: */
142: public static void closeStatement(Statement statement) {
143: if (statement != null) {
144: try {
145: statement.close();
146: } catch (SQLException e) {
147: e.printStackTrace();
148: }
149: }
150: }
151:
152: /**
153: * Use this method to return the connection to the connection pool
154: * Do not use this method to close connection that is not from
155: * the connection pool
156: * @param connection : the connection that needs to be returned to the pool
157: */
158: public static void closeConnection(Connection connection) {
159: if (connection != null) {
160: try {
161: connection.close();
162: } catch (SQLException e) {
163: e.printStackTrace();
164: }
165: }
166: }
167:
168: public static Document getDomDocument() {
169: return doc;
170: }
171:
172: public static void writeXmlFile(Document doc, String filename) {
173: try {
174: // Prepare the DOM document for writing
175: Source source = new DOMSource(doc);
176:
177: // Prepare the output file
178: File file = new File(filename);
179: Result result = new StreamResult(file);
180:
181: // Write the DOM document to the file
182: Transformer xformer = TransformerFactory.newInstance()
183: .newTransformer();
184: xformer.transform(source, result);
185: } catch (TransformerConfigurationException e) {
186: e.printStackTrace();
187: } catch (TransformerException e) {
188: e.printStackTrace();
189: }
190: }
191:
192: public static FileInputStream getInput() {
193: try {
194: FileInputStream file = new FileInputStream(
195: Phpbb2MvnforumConfig.EXPORT_XML);
196: return file;
197: } catch (FileNotFoundException e) {
198: e.printStackTrace();
199: }
200: return null;
201: }
202:
203: public static FileOutputStream getOutput() {
204: try {
205: FileOutputStream file = new FileOutputStream(
206: Phpbb2MvnforumConfig.EXPORT_XML);
207: return file;
208: } catch (FileNotFoundException e) {
209: e.printStackTrace();
210: }
211: return null;
212: }
213:
214: }
|