2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.sql
.Connection
;
22 import java
.sql
.DriverManager
;
23 import java
.sql
.Statement
;
24 import java
.sql
.ResultSet
;
25 import java
.sql
.SQLException
;
27 import java
.lang
.Thread
;
28 import java
.util
.StringTokenizer
;
32 private Connection m_aConnection
= null;
33 public ShareConnection()
36 public Connection
getConnection()
38 if (m_aConnection
== null)
42 m_aConnection
= DBHelper
.getMySQLConnection();
44 catch(java
.sql
.SQLException e
)
46 GlobalLogWriter
.get().println("DB: ERROR: can't connect to DB.");
54 class MySQLThread
extends Thread
56 Connection m_aCon
= null;
58 public MySQLThread(Connection _aCon
, String _sSQL
)
66 Statement oStmt
= null;
69 GlobalLogWriter
.get().println("DB: ERROR: in ExecSQL, connection not established.");
73 // Connection oCon = null;
76 // oCon = getMySQLConnection();
77 oStmt
= m_aCon
.createStatement();
79 GlobalLogWriter
.get().println("DB: " + m_sSQL
);
80 /* ResultSet oResult = */
81 oStmt
.executeUpdate(m_sSQL
);
85 GlobalLogWriter
.get().println("DB: Couldn't execute sql string '" + m_sSQL
+ "'");
86 GlobalLogWriter
.get().println("DB: Reason: " + e
.getMessage());
94 * This method inserts given values into<br>
96 * @param values a set of comma separated values to be inserted
99 public void SQLinsertValues(Connection _aCon
, String _sTableName
, String value_names
, String values
)
103 GlobalLogWriter
.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
107 // String aInsertStr = "";
109 // aInsertStr = "INSERT INTO " + _sTableName + " (" + value_names + " ) VALUES (" + values + ")";
110 // ExecSQL(_aCon, aInsertStr);
111 StringBuffer aInsertStr
= new StringBuffer();
113 aInsertStr
.append( "INSERT INTO " ) . append( _sTableName
);
114 aInsertStr
.append( " (").append( value_names
).append ( ")" );
115 aInsertStr
.append(" VALUES (" ).append( values
).append( ")" );
116 ExecSQL(_aCon
, aInsertStr
.toString() );
119 public void SQLupdateValue(Connection _aCon
, String _sTableName
, String _sSet
, String _sWhere
)
123 GlobalLogWriter
.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
127 // String aUpdateStr = "";
129 // aUpdateStr = "UPDATE " + _sTableName + " SET " + _sSet + " WHERE " + _sWhere;
130 // ExecSQL( _aCon, aUpdateStr );
131 StringBuffer aUpdateStr
= new StringBuffer();
133 aUpdateStr
.append( "UPDATE " ).append( _sTableName
)
134 .append( " SET " ).append( _sSet
)
135 .append( " WHERE " ).append( _sWhere
);
136 ExecSQL( _aCon
, aUpdateStr
.toString() );
139 private static String m_sDBServerName
;
140 private static String m_sDBName
;
141 private static String m_sDBUser
;
142 private static String m_sDBPasswd
;
144 protected synchronized void fillDBConnection(String _sInfo
)
146 StringTokenizer aTokenizer
= new StringTokenizer(_sInfo
,",",false);
147 while (aTokenizer
.hasMoreTokens())
149 String sPart
= aTokenizer
.nextToken();
150 if (sPart
.startsWith("db:"))
152 m_sDBName
= sPart
.substring(3);
153 // GlobalLogWriter.get().println("DB: source version: " + m_sSourceVersion);
155 else if (sPart
.startsWith("user:"))
157 m_sDBUser
= sPart
.substring(5);
159 else if (sPart
.startsWith("passwd:"))
161 m_sDBPasswd
= sPart
.substring(7);
163 else if (sPart
.startsWith("server:"))
165 m_sDBServerName
= sPart
.substring(7);
171 * This method establishes a Connection<br>
172 * with the database 'module_unit' on jakobus
175 public static Connection
getMySQLConnection() throws SQLException
179 Class
.forName("org.gjt.mm.mysql.Driver");
180 String sConnection
= "jdbc:mysql://" + m_sDBServerName
+ ":3306/" + m_sDBName
;
181 // Connection mysql = DriverManager.getConnection(
182 // "jdbc:mysql://jakobus:3306/jobs_convwatch","admin","admin");
183 Connection mysql
= DriverManager
.getConnection(sConnection
, m_sDBUser
, m_sDBPasswd
);
186 catch (ClassNotFoundException e
)
188 GlobalLogWriter
.get().println("DB: Class not found exception caught: " + e
.getMessage());
189 GlobalLogWriter
.get().println("DB: Maybe mysql.jar is not added to the classpath.");
194 protected synchronized void ExecSQL(Connection _aCon
, String _sSQL
)
196 MySQLThread aSQLThread
= new MySQLThread(_aCon
, _sSQL
);
200 public int QueryIntFromSQL(Connection _aCon
, String _sSQL
, String _sColumnName
)
201 throws ValueNotFoundException
203 Statement oStmt
= null;
207 // oCon = getMySQLConnection();
208 oStmt
= _aCon
.createStatement();
210 ResultSet oResult
= oStmt
.executeQuery(_sSQL
);
215 if (_sColumnName
.length() == 0)
217 // take the first row value (started with 1)
218 nValue
= oResult
.getInt(1);
222 nValue
= oResult
.getInt(_sColumnName
);
224 // System.out.println("value: " + String.valueOf(nValue));
226 catch (SQLException e
)
228 String sError
= e
.getMessage();
229 GlobalLogWriter
.get().println("DB: Original SQL error: " + sError
);
230 throw new ValueNotFoundException("Cant execute SQL: " + _sSQL
);
233 catch(SQLException e
)
235 String sError
= e
.getMessage();
236 GlobalLogWriter
.get().println("DB: Couldn't execute sql string " + _sSQL
+ "\n" + sError
);
241 public String
Quote(String _sToQuote
)
245 int nQuote
= _sToQuote
.indexOf(ts
);
248 return ds
+ _sToQuote
+ ds
;
250 return ts
+ _sToQuote
+ ts
;
253 /* default date format in the MySQL DB yyyy-MM-dd */
254 public static String
today()
256 return DateHelper
.getDateString("yyyy-MM-dd");
259 public static final String sComma
= ",";
260 public static final String sEqual
= "=";
261 public static final String sAND
= " AND ";