lok: avoid expensive fetching of a property.
[LibreOffice.git] / qadevOOo / runner / convwatch / DBHelper.java
blob34e1e1cfdeee1ec0f4ebfc9624904493fdddfdfa
1 /*
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 .
19 package convwatch;
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.Statement;
24 import java.sql.SQLException;
26 import java.util.StringTokenizer;
28 class ShareConnection
30 private Connection m_aConnection = null;
32 public Connection getConnection()
34 if (m_aConnection == null)
36 try
38 m_aConnection = DBHelper.getMySQLConnection();
40 catch(java.sql.SQLException e)
42 GlobalLogWriter.get().println("DB: ERROR: can't connect to DB.");
43 m_aConnection = null;
46 return m_aConnection;
50 class MySQLThread extends Thread
52 private final Connection m_aCon;
53 private final String m_sSQL;
54 public MySQLThread(Connection _aCon, String _sSQL)
56 m_aCon = _aCon;
57 m_sSQL = _sSQL;
60 @Override
61 public void run()
63 Statement oStmt = null;
64 if (m_aCon == null)
66 GlobalLogWriter.get().println("DB: ERROR: in ExecSQL, connection not established.");
67 return;
70 try
72 try
74 oStmt = m_aCon.createStatement();
76 GlobalLogWriter.get().println("DB: " + m_sSQL);
77 /* ResultSet oResult = */
78 oStmt.executeUpdate(m_sSQL);
80 finally
82 if (oStmt != null)
83 oStmt.close();
86 catch(Exception e)
88 GlobalLogWriter.get().println("DB: Couldn't execute sql string '" + m_sSQL + "'");
89 GlobalLogWriter.get().println("DB: Reason: " + e.getMessage());
94 public class DBHelper
96 public void SQLupdateValue(Connection _aCon, String _sTableName, String _sSet, String _sWhere)
98 if (_aCon == null)
100 GlobalLogWriter.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
101 return;
104 StringBuffer aUpdateStr = new StringBuffer();
106 aUpdateStr.append( "UPDATE " ).append( _sTableName )
107 .append( " SET " ).append( _sSet )
108 .append( " WHERE " ).append( _sWhere );
109 ExecSQL( _aCon, aUpdateStr.toString() );
112 private static String m_sDBServerName;
113 private static String m_sDBName;
114 private static String m_sDBUser;
115 private static String m_sDBPasswd;
117 protected synchronized void fillDBConnection(String _sInfo)
119 StringTokenizer aTokenizer = new StringTokenizer(_sInfo,",",false);
120 while (aTokenizer.hasMoreTokens())
122 String sPart = aTokenizer.nextToken();
123 if (sPart.startsWith("db:"))
125 m_sDBName = sPart.substring(3);
127 else if (sPart.startsWith("user:"))
129 m_sDBUser = sPart.substring(5);
131 else if (sPart.startsWith("passwd:"))
133 m_sDBPasswd = sPart.substring(7);
135 else if (sPart.startsWith("server:"))
137 m_sDBServerName = sPart.substring(7);
143 * This method establishes a Connection<br>
144 * with the database 'module_unit' on jakobus
147 public static Connection getMySQLConnection() throws SQLException
151 Class.forName("org.gjt.mm.mysql.Driver");
152 String sConnection = "jdbc:mysql://" + m_sDBServerName + ":3306/" + m_sDBName;
153 // Connection mysql = DriverManager.getConnection(
154 // "jdbc:mysql://jakobus:3306/jobs_convwatch","admin","admin");
155 Connection mysql = DriverManager.getConnection(sConnection, m_sDBUser, m_sDBPasswd);
156 return mysql;
158 catch (ClassNotFoundException e)
160 GlobalLogWriter.get().println("DB: Class not found exception caught: " + e.getMessage());
161 GlobalLogWriter.get().println("DB: Maybe mysql.jar is not added to the classpath.");
163 return null;
166 private synchronized void ExecSQL(Connection _aCon, String _sSQL)
168 MySQLThread aSQLThread = new MySQLThread(_aCon, _sSQL);
169 aSQLThread.start();
174 public String Quote(String _sToQuote)
176 char ts = '\'';
177 char ds = '"';
178 int nQuote = _sToQuote.indexOf(ts);
179 if (nQuote >= 0)
181 return ds + _sToQuote + ds;
183 return ts + _sToQuote + ts;
186 public static final String sEqual = "=";
187 public static final String sAND = " AND ";