tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / qadevOOo / runner / convwatch / DBHelper.java
blob9bcf4765350ebac2d95aadac44c20e5485217449
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 final Object settingsLock = new Object();
113 private static String m_sDBServerName;
114 private static String m_sDBName;
115 private static String m_sDBUser;
116 private static String m_sDBPasswd;
118 protected void fillDBConnection(String _sInfo)
120 synchronized (settingsLock)
122 StringTokenizer aTokenizer = new StringTokenizer(_sInfo,",",false);
123 while (aTokenizer.hasMoreTokens())
125 String sPart = aTokenizer.nextToken();
126 if (sPart.startsWith("db:"))
128 m_sDBName = sPart.substring(3);
130 else if (sPart.startsWith("user:"))
132 m_sDBUser = sPart.substring(5);
134 else if (sPart.startsWith("passwd:"))
136 m_sDBPasswd = sPart.substring(7);
138 else if (sPart.startsWith("server:"))
140 m_sDBServerName = sPart.substring(7);
147 * This method establishes a Connection<br>
148 * with the database 'module_unit' on jakobus
151 public static Connection getMySQLConnection() throws SQLException
153 synchronized (settingsLock)
157 Class.forName("org.gjt.mm.mysql.Driver");
158 String sConnection = "jdbc:mysql://" + m_sDBServerName + ":3306/" + m_sDBName;
159 // Connection mysql = DriverManager.getConnection(
160 // "jdbc:mysql://jakobus:3306/jobs_convwatch","admin","admin");
161 Connection mysql = DriverManager.getConnection(sConnection, m_sDBUser, m_sDBPasswd);
162 return mysql;
164 catch (ClassNotFoundException e)
166 GlobalLogWriter.get().println("DB: Class not found exception caught: " + e.getMessage());
167 GlobalLogWriter.get().println("DB: Maybe mysql.jar is not added to the classpath.");
169 return null;
173 private synchronized void ExecSQL(Connection _aCon, String _sSQL)
175 MySQLThread aSQLThread = new MySQLThread(_aCon, _sSQL);
176 aSQLThread.start();
181 public String Quote(String _sToQuote)
183 char ts = '\'';
184 char ds = '"';
185 int nQuote = _sToQuote.indexOf(ts);
186 if (nQuote >= 0)
188 return ds + _sToQuote + ds;
190 return ts + _sToQuote + ts;
193 public static final String sEqual = "=";
194 public static final String sAND = " AND ";