1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DBHelper.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.sql
.Connection
;
34 import java
.sql
.DriverManager
;
35 import java
.sql
.Statement
;
36 import java
.sql
.ResultSet
;
37 import java
.sql
.SQLException
;
39 import java
.lang
.Thread
;
40 import java
.util
.StringTokenizer
;
44 private Connection m_aConnection
= null;
45 public ShareConnection()
48 public Connection
getConnection()
50 if (m_aConnection
== null)
54 m_aConnection
= DBHelper
.getMySQLConnection();
56 catch(java
.sql
.SQLException e
)
58 GlobalLogWriter
.get().println("DB: ERROR: can't connect to DB.");
66 class MySQLThread
extends Thread
68 Connection m_aCon
= null;
70 public MySQLThread(Connection _aCon
, String _sSQL
)
78 Statement oStmt
= null;
81 GlobalLogWriter
.get().println("DB: ERROR: in ExecSQL, connection not established.");
85 // Connection oCon = null;
88 // oCon = getMySQLConnection();
89 oStmt
= m_aCon
.createStatement();
91 GlobalLogWriter
.get().println("DB: " + m_sSQL
);
92 /* ResultSet oResult = */
93 oStmt
.executeUpdate(m_sSQL
);
97 GlobalLogWriter
.get().println("DB: Couldn't execute sql string '" + m_sSQL
+ "'");
98 GlobalLogWriter
.get().println("DB: Reason: " + e
.getMessage());
103 public class DBHelper
106 * This method inserts given values into<br>
108 * @param values a set of comma separated values to be inserted
111 public void SQLinsertValues(Connection _aCon
, String _sTableName
, String value_names
, String values
)
115 GlobalLogWriter
.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
119 // String aInsertStr = "";
121 // aInsertStr = "INSERT INTO " + _sTableName + " (" + value_names + " ) VALUES (" + values + ")";
122 // ExecSQL(_aCon, aInsertStr);
123 StringBuffer aInsertStr
= new StringBuffer();
125 aInsertStr
.append( "INSERT INTO " ) . append( _sTableName
);
126 aInsertStr
.append( " (").append( value_names
).append ( ")" );
127 aInsertStr
.append(" VALUES (" ).append( values
).append( ")" );
128 ExecSQL(_aCon
, aInsertStr
.toString() );
131 public void SQLupdateValue(Connection _aCon
, String _sTableName
, String _sSet
, String _sWhere
)
135 GlobalLogWriter
.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
139 // String aUpdateStr = "";
141 // aUpdateStr = "UPDATE " + _sTableName + " SET " + _sSet + " WHERE " + _sWhere;
142 // ExecSQL( _aCon, aUpdateStr );
143 StringBuffer aUpdateStr
= new StringBuffer();
145 aUpdateStr
.append( "UPDATE " ).append( _sTableName
)
146 .append( " SET " ).append( _sSet
)
147 .append( " WHERE " ).append( _sWhere
);
148 ExecSQL( _aCon
, aUpdateStr
.toString() );
151 private static String m_sDBServerName
;
152 private static String m_sDBName
;
153 private static String m_sDBUser
;
154 private static String m_sDBPasswd
;
156 protected synchronized void fillDBConnection(String _sInfo
)
158 StringTokenizer aTokenizer
= new StringTokenizer(_sInfo
,",",false);
159 while (aTokenizer
.hasMoreTokens())
161 String sPart
= aTokenizer
.nextToken();
162 if (sPart
.startsWith("db:"))
164 m_sDBName
= sPart
.substring(3);
165 // GlobalLogWriter.get().println("DB: source version: " + m_sSourceVersion);
167 else if (sPart
.startsWith("user:"))
169 m_sDBUser
= sPart
.substring(5);
171 else if (sPart
.startsWith("passwd:"))
173 m_sDBPasswd
= sPart
.substring(7);
175 else if (sPart
.startsWith("server:"))
177 m_sDBServerName
= sPart
.substring(7);
183 * This method establishes a Connection<br>
184 * with the database 'module_unit' on jakobus
187 public static Connection
getMySQLConnection() throws SQLException
191 Class
.forName("org.gjt.mm.mysql.Driver");
192 String sConnection
= "jdbc:mysql://" + m_sDBServerName
+ ":3306/" + m_sDBName
;
193 // Connection mysql = DriverManager.getConnection(
194 // "jdbc:mysql://jakobus:3306/jobs_convwatch","admin","admin");
195 Connection mysql
= DriverManager
.getConnection(sConnection
, m_sDBUser
, m_sDBPasswd
);
198 catch (ClassNotFoundException e
)
200 GlobalLogWriter
.get().println("DB: Class not found exception caught: " + e
.getMessage());
201 GlobalLogWriter
.get().println("DB: Maybe mysql.jar is not added to the classpath.");
208 * This method removes all entries of the given<br>
209 * module/platform combination
210 * @param mdl the name of the module, e.g. sal
211 * @param os the name of the platform, e.g. unxsols
213 // LLA: public static void SQLdeleteValues(Connection _aCon, String _sEnvironment, String _sUnitName, String _sMethodName, String _sCWS, String _sDate)
215 // LLA: String sSQL =
216 // LLA: "DELETE FROM states WHERE " +
217 // LLA: " unit=" + DatabaseEntry.Quote(_sUnitName) +
218 // LLA: " AND pf=" + DatabaseEntry.Quote (_sEnvironment) +
219 // LLA: " AND meth=" + DatabaseEntry.Quote (_sMethodName) +
220 // LLA: " AND cws=" + DatabaseEntry.Quote(_sCWS) +
221 // LLA: " AND dt=" + DatabaseEntry.Quote(_sDate);
223 // LLA: // ExecSQL(_aCon, sSQL);
226 protected synchronized void ExecSQL(Connection _aCon
, String _sSQL
)
228 MySQLThread aSQLThread
= new MySQLThread(_aCon
, _sSQL
);
234 // public static int QueryIntFromSQL(String _sSQL, String _sColumnName, String _sValue)
236 // boolean bNeedSecondTry = false;
242 // nValue = QueryIntFromSQL(_sSQL, _sColumnName, _sValue);
244 // catch (ValueNotFoundException e)
246 // bNeedSecondTry = true;
247 // String sSQL = "INSERT INTO " + _sTable + "(" + _sColumnName + ") VALUES (" + _sValue + ")";
250 // } while (bNeedSecondTry);
254 public int QueryIntFromSQL(Connection _aCon
, String _sSQL
, String _sColumnName
)
255 throws ValueNotFoundException
257 Statement oStmt
= null;
258 Connection oCon
= null;
262 // oCon = getMySQLConnection();
263 oStmt
= _aCon
.createStatement();
265 ResultSet oResult
= oStmt
.executeQuery(_sSQL
);
270 if (_sColumnName
.length() == 0)
272 // take the first row value (started with 1)
273 nValue
= oResult
.getInt(1);
277 nValue
= oResult
.getInt(_sColumnName
);
279 // System.out.println("value: " + String.valueOf(nValue));
281 catch (SQLException e
)
283 String sError
= e
.getMessage();
284 GlobalLogWriter
.get().println("DB: Original SQL error: " + sError
);
285 throw new ValueNotFoundException("Cant execute SQL: " + _sSQL
);
288 catch(SQLException e
)
290 String sError
= e
.getMessage();
291 GlobalLogWriter
.get().println("DB: Couldn't execute sql string " + _sSQL
+ "\n" + sError
);
296 public String
Quote(String _sToQuote
)
300 int nQuote
= _sToQuote
.indexOf(ts
);
303 return ds
+ _sToQuote
+ ds
;
305 return ts
+ _sToQuote
+ ts
;
308 /* default date format in the MySQL DB yyyy-MM-dd */
309 public static String
today()
311 return DateHelper
.getDateString("yyyy-MM-dd");
314 public static final String sComma
= ",";
315 public static final String sEqual
= "=";
316 public static final String sAND
= " AND ";