001: /*
002: * SalomeTMF is a Test Management Framework
003: * Copyright (C) 2005 France Telecom R&D
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * @author Faycal SOUGRATI
020: *
021: * Contact: mikael.marche@rd.francetelecom.com
022: */
023:
024: package org.objectweb.salome_tmf.api.api2db;
025:
026: import java.sql.ResultSet;
027: import java.sql.ResultSetMetaData;
028: import java.sql.SQLException;
029:
030: /**
031: * Classe encapsulant tous les traitements necessaires au formattage des resulats des requetes
032: * envoyees a la BdD SalomeTMF
033: */
034: public class DataSet {
035:
036: /**
037: * Objet contenant les informations sur le resultat d'une requete
038: */
039: ResultSet results;
040:
041: /**
042: * Objet contenant des informations sur le nom et le type des colonnes du resultat d'une requete
043: */
044: ResultSetMetaData rsmd;
045:
046: /**
047: * Nombre de colonnes du resultat d'une requete
048: */
049: int columnCount;
050:
051: /**
052: * Noms des colonnes du resultat d'une requete
053: */
054: String[] columnNames;
055:
056: /**
057: * Constructeur
058: */
059: public DataSet(ResultSet results) {
060: try {
061: this .results = results;
062: rsmd = results.getMetaData();
063: columnCount = rsmd.getColumnCount();
064: } catch (SQLException ex) {
065: System.out.println(ex.getMessage());
066: }
067: }
068:
069: /**
070: * Selecteur sur le champ "results"
071: */
072: public ResultSet getResults() {
073: return results;
074: }
075:
076: /**
077: * Selecteur sur le champ "columnCount"
078: */
079: public int getColumnCount() {
080: return columnCount;
081: }
082:
083: /**
084: * Fonction retournant "true" s'il y a plus de resultats, "false" sinon
085: */
086: public boolean hasMoreElements() {
087: try {
088: return results.next();
089: } catch (SQLException ex) {
090: System.out.println(ex.getMessage());
091: return false;
092: }
093: }
094:
095: /**
096: * Retourne tous les champs du resultat sous forme de chaines de caracteres
097: */
098: public String[] getMetaData() {
099: String[] s;
100: s = new String[columnCount + 1];
101: try {
102: results.next();
103: for (int i = 1; i <= columnCount; i++) {
104: s[i] = results.getString(i);
105: }
106: } catch (SQLException ex) {
107: System.out.println(ex.getMessage());
108: }
109: return s;
110: }
111:
112: /**
113: * Retourne tous les champs du resultat suivant sous forme de chaines de caracteres
114: */
115: public String[] nextElement() {
116: String[] line;
117: line = new String[columnCount + 1];
118: try {
119: for (int i = 1; i <= columnCount; i++) {
120: line[i] = results.getString(i);
121: }
122: } catch (SQLException ex) {
123: System.out.println(ex.getMessage());
124: }
125: return line;
126: }
127:
128: /**
129: * Donne la valeur d'un champ du resultat en indiquant le nom de la colonne
130: */
131: public String getColumnValue(String columnName) {
132: try {
133: return results.getString(columnName);
134: } catch (SQLException ex) {
135: System.out.println(ex.getMessage());
136: return "";
137: }
138: }
139:
140: /**
141: * Retourne le nom de la colonne numero i dans le resultat
142: */
143: public String getColumnName(int i) {
144: try {
145: return rsmd.getColumnName(i);
146: } catch (SQLException ex) {
147: System.out.println(ex.getMessage());
148: return "";
149: }
150: }
151:
152: /**
153: * Donne le maximum de caracteres pour la colonne i dans la resultat
154: */
155: public int getColumnDisplaySize(int i) throws SQLException {
156: //try {
157: return rsmd.getColumnDisplaySize(i);
158: /*}
159: catch (SQLException ex) {
160: System.out.println(ex.getMessage());
161: return 0;
162: }*/
163: }
164:
165: /**
166: * Donne le type SQL de la colonne i dans le resultat
167: */
168: public int getColumnType(int i) throws SQLException {
169: //try {
170: return rsmd.getColumnType(i);
171: /*}
172: catch (SQLException ex) {
173: System.out.println(ex.getMessage());
174: return 0;
175: }*/
176: }
177:
178: /**
179: * Retoune le type dans la BdD de la colonne i dans le resultat
180: */
181: public String getColumnTypeName(int i) throws SQLException {
182: //try {
183: return rsmd.getColumnTypeName(i);
184: /*}
185: catch (SQLException ex) {
186: System.out.println(ex.getMessage());
187: return "";
188: }*/
189: }
190:
191: /**
192: * Affiche le resultat sous forme d'une chaine de caracteres
193: */
194: public void display() {
195: String[] line = null;
196: try {
197: while (hasMoreElements()) {
198: line = nextElement();
199: for (int i = 1; i <= getColumnCount(); i++) {
200: System.out.print(line[i] + " | ");
201: }
202: System.out.println("");
203: }
204: } catch (Exception E) {
205: E.printStackTrace();
206: }
207: }
208:
209: }
|