view m-toolbox/classes/+utils/@jmysql/query.m @ 52:daf4eab1a51e database-connection-manager tip

Fix. Default password should be [] not an empty string
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 07 Dec 2011 17:29:47 +0100
parents 91f21a0aab35
children
line wrap: on
line source

function varargout = query(conn, query, varargin)
% QUERY  Query an LTPDA repository.
%
% DEPRECATED! Use utils.mysql.execute instead!
%
% CALL:                [results] = query(conn, q, varargin)
%              [results, cnames] = query(conn, q, varargin)
%
% INPUTS:
%
%      conn  - database connection object implementing java.sql.Connection
%     query  - a valid SQL query
%  varargin  - optional query parameters
%
% OUTPUTS:
%
%   results  - a cell-array of the results
%    cnames  - a cell-array of the column names in the query
%
% VERSION: $Id: query.m,v 1.1 2009/07/27 19:46:21 hewitson Exp $
%

  warning('!!! deprecated. use utils.mysql.execute instead');
  
  if ~isa(conn, 'java.sql.Connection')
    error('### first argument should be a java.sql.Connection object');
  end
  
  stmt = conn.prepareStatement(query);
  for kk = 1:numel(varargin)
    stmt.setObject(kk, varargin{kk});
  end
  
  % execute query
  resultSet = stmt.executeQuery();
  
  % set outputs
  if nargout > 0
    varargout{1} = resultSet;
    if nargout == 2
      % get column names from the meta data
      rsm = resultSet.getMetaData;
      Nc = rsm.getColumnCount;
      colnames = cell(1,Nc);
      for kk=1:Nc
        colnames{kk} = char(rsm.getColumnName(kk));
      end
      varargout{2} = colnames;
    end
  end
  
end