/************************************************************************* * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * Copyright 2000, 2010 Oracle and/or its affiliates. * * OpenOffice.org - a multi-platform office productivity suite * * This file is part of OpenOffice.org. * * OpenOffice.org is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 * only, as published by the Free Software Foundation. * * OpenOffice.org is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License version 3 for more details * (a copy is included in the LICENSE file that accompanied this code). * * You should have received a copy of the GNU Lesser General Public License * version 3 along with OpenOffice.org. If not, see * * for a copy of the LGPLv3 License. * ************************************************************************/ package stats; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.Statement; import java.util.Enumeration; import java.util.Hashtable; import java.util.StringTokenizer; import java.util.Vector; /** * */ public class SQLExecution { protected Connection mConnection = null; protected Statement mStatement = null; protected String mJdbcClass = null; protected String mDbURL = null; protected String mUser = null; protected String mPassword = null; protected boolean m_bConnectionOpen = false; protected boolean m_bDebug = false; /** Creates a new instance of SQLExecution * @param jdbcClass The jdbc class for the connection. * @param dbUrl The url of the database. * @param user The user for connecting the database. * @param password The password of throws user. */ public SQLExecution(String jdbcClass, String dbUrl, String user, String password) { mJdbcClass = jdbcClass; mUser = user; mPassword = password; mDbURL = dbUrl; } /** Creates a new instance of SQLExecution with additional debug output. * @param jdbcClass The jdbc class for the connection. * @param dbUrl The url of the database. * @param user The user for connecting the database. * @param password The password of throws user. * @param debug Write debug information, if true. */ public SQLExecution(String jdbcClass, String dbUrl, String user, String password, boolean debug) { mJdbcClass = jdbcClass; mUser = user; mPassword = password; mDbURL = dbUrl; m_bDebug = debug; } /** * Open a connection to the DataBase * @return True, if no error occured. */ public boolean openConnection() { if(m_bConnectionOpen) return true; try { Class.forName(mJdbcClass); } catch (ClassNotFoundException e) { System.err.println("Couldn't find jdbc driver : " + e.getMessage()); return false; } try { // establish database connection mConnection = DriverManager.getConnection( mDbURL, mUser, mPassword); mStatement = mConnection.createStatement(); } catch(java.sql.SQLException e) { System.err.println("Couldn't establish a connection: " + e.getMessage()); return false; } m_bConnectionOpen = true; return true; } /** * Close the connection to the DataBase * @return True, if no error occured. */ public boolean closeConnection() { if (!m_bConnectionOpen) return true; try { // close database connection mStatement.close(); mConnection.close(); } catch(java.sql.SQLException e) { System.err.println("Couldn't close the connection: " + e.getMessage()); return false; } m_bConnectionOpen = false; return true; } /** * Execute an sql command. * @param command The command to execute. * @param sqlInput Input values for the command. * @param sqlOutput The results of the command are put in this Hashtable. * @return True, if no error occured. */ public boolean executeSQLCommand(String command, Hashtable sqlInput, Hashtable sqlOutput) throws IllegalArgumentException { return executeSQLCommand(command, sqlInput, sqlOutput, false); } /** * Execute an sql command. * @param command The command to execute. * @param sqlInput Input values for the command. * @param sqlOutput The results of the command are put in this Hashtable. * @param mergeOutputIntoInput The output of the result is put into the * sqlInput Hashtable. * @return True, if no error occured. */ public boolean executeSQLCommand(String command, Hashtable sqlInput, Hashtable sqlOutput, boolean mergeOutputIntoInput) throws IllegalArgumentException { if (sqlOutput == null) { sqlOutput = new Hashtable(); // this has to be true, so the user of this method gets a return mergeOutputIntoInput = true; if (sqlInput == null) { System.out.println("sqlInput and sqlOutput are null: cannot return the results of the sql command."); return false; } } Vector sqlCommand = new Vector(); sqlCommand.add(""); boolean update = false; // synchronize all "$varname" occurences in the command string with // values from input StringTokenizer token = new StringTokenizer(command, " "); while (token.hasMoreTokens()) { String originalKey = token.nextToken(); // search for keys, beginning with "$" int index = originalKey.indexOf('$'); if (index != -1) { // found key String pre = ""; pre = originalKey.substring(0,index); // generate key: remove "$" String key = originalKey.substring(index+1); String post = ""; // remove any endings the key might have while (key.endsWith(",") || key.endsWith("\'") || key.endsWith(";") || key.endsWith(")") || key.endsWith("\"")) { post = key.substring(key.length()-1) + post; key = key.substring(0, key.length()-1); } // look for key in the Hashtable if (sqlInput.containsKey(key)) { // is there a value for the key? Object in = sqlInput.get(key); if (in instanceof String[]) { // value is a String[] String[]vals = (String[])in; if (vals.length != sqlCommand.size() && sqlCommand.size() > 1) { // size of this array and previous array(s) does not match throw new IllegalArgumentException("The key '" + key + "' in command \n'" + command + "'\n has not the same value count as the keys before."); } // build up the commands boolean addNewVals = (sqlCommand.size() == 1); for (int i=0; i with <''> in the value Strings, or the command will fail. * @param checkString The String that is checked: a part of the command * @return The String, cleared of all quotation marks. */ private String checkForQuotationMarks(String checkString) { String returnString = checkString; int quotIndex = 0; while ((quotIndex = returnString.indexOf('\"')) != -1) { String firstHalf = returnString.substring(0, quotIndex); String secondHalf = returnString.substring(quotIndex+1); returnString = firstHalf + "\'\'" + secondHalf; } return returnString; } }