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
.SQLException
;
26 import java
.util
.StringTokenizer
;
30 private Connection m_aConnection
= null;
32 public Connection
getConnection()
34 if (m_aConnection
== null)
38 m_aConnection
= DBHelper
.getMySQLConnection();
40 catch(java
.sql
.SQLException e
)
42 GlobalLogWriter
.get().println("DB: ERROR: can't connect to DB.");
50 class MySQLThread
extends Thread
52 private final Connection m_aCon
;
53 private final String m_sSQL
;
54 public MySQLThread(Connection _aCon
, String _sSQL
)
63 Statement oStmt
= null;
66 GlobalLogWriter
.get().println("DB: ERROR: in ExecSQL, connection not established.");
74 oStmt
= m_aCon
.createStatement();
76 GlobalLogWriter
.get().println("DB: " + m_sSQL
);
77 /* ResultSet oResult = */
78 oStmt
.executeUpdate(m_sSQL
);
88 GlobalLogWriter
.get().println("DB: Couldn't execute sql string '" + m_sSQL
+ "'");
89 GlobalLogWriter
.get().println("DB: Reason: " + e
.getMessage());
96 public void SQLupdateValue(Connection _aCon
, String _sTableName
, String _sSet
, String _sWhere
)
100 GlobalLogWriter
.get().println("DB: ERROR: in SQLinsertValues, connection not established.");
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
);
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.");
173 private synchronized void ExecSQL(Connection _aCon
, String _sSQL
)
175 MySQLThread aSQLThread
= new MySQLThread(_aCon
, _sSQL
);
181 public String
Quote(String _sToQuote
)
185 int nQuote
= _sToQuote
.indexOf(ts
);
188 return ds
+ _sToQuote
+ ds
;
190 return ts
+ _sToQuote
+ ts
;
193 public static final String sEqual
= "=";
194 public static final String sAND
= " AND ";