File:  [Repository] / FM2SQL / Attic / DBBean.java
Revision 1.19: download - view: text, annotated - select for diffs - revision graph
Tue May 11 09:06:38 2004 UTC (20 years, 1 month ago) by rogo
Branches: MAIN
CVS tags: HEAD
new driver added atinav

    1: /*
    2:  * DBBean.java -- Class that ecapsulates all database actions 
    3:  * Filemake to SQL Converter 
    4:  * Copyright (C) 2004 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) 
    5:  * This program is free software; you can redistribute it and/or modify it
    6:  * under the terms of the GNU General Public License as published by the Free
    7:  * Software Foundation; either version 2 of the License, or (at your option)
    8:  * any later version.  Please read license.txt for the full details. A copy of
    9:  * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html  You should
   10:  * have received a copy of the GNU General Public License along with this
   11:  * program; if not, write to the Free Software Foundation, Inc., 59 Temple
   12:  * Place, Suite 330, Boston, MA 02111-1307 USA  Created on 15.09.2003 by
   13:  * rogo  
   14:  */
   15: 
   16: 
   17: import java.sql.*;
   18: import java.text.DateFormat;
   19: import java.util.*;
   20: 
   21: import com.fmi.jdbc.*;
   22: 
   23: /**
   24:  *
   25:  *
   26:  * DBBean - Database bean
   27:  *
   28:  *<p> a Javabean  to perform queries on a JDBC Database,
   29:  * or excute any other SQL statement
   30:  * </p>
   31:  * <p>
   32:  * Usage:
   33:  * <pre>
   34:  *   DBBean bean = new DBBean();
   35:  * // setting user and passwd
   36:  *  bean.setUserAndPasswd("bla","bla");
   37:  *  try
   38:  *  {
   39:  *    bean.setConnection("jdbc:fmpro:http://localhost");
   40:  *    Vector names=bean.getTableNames();
   41:  *    Vector[] result=bean.getQueryData(names.get(0).toString());
   42:  *  // print results to screen
   43:  *     for(int i=0;i&lt;result[1].size();++i)
   44:  *     {
   45:  *       //print Header
   46:  *       System.out.print(" "+result[1].get(i));
   47:  *     }
   48:  *  System.out.println();
   49:  *  for(int j=0;j&lt;result[0].size();++j)
   50:  *  {
   51:  *     Vector row=(Vector)result[0].get(j);
   52:  *     //print rows
   53:  *     for(int k=0;k&lt;row.size();++k)
   54:  *     System.out.print(" "+row.get(k));
   55:  *     System.out.println();
   56:  *  }
   57:  * } catch(Exception e)
   58:  *   {
   59:  *     System.out.println("Error while connecting to database"+ e);
   60:  *   }
   61:  * </pre>
   62:  *
   63:  * </p>
   64:  * @author rogo
   65:  */
   66: public class DBBean
   67: {
   68:   Connection connection;
   69:   String url = "";
   70:   DatabaseMetaData dbMetaData;
   71:   Vector columnNames;
   72:   Vector ids = new Vector();
   73:   String user = (System.getProperty("user.name") == null) ? "" : System.getProperty("user.name"); //"postgres";
   74:   String passwd = ""; //"3333";//"rogo";
   75:   public int maxHits = 10;
   76:   ResultSet result;
   77:   String quoteChar = "";
   78:   Hashtable connectionPool = new Hashtable();
   79:   public ResultSetMetaData metaData;
   80:   // register DataBase Drivers
   81:   static {
   82:     try
   83:     {
   84:       DriverManager.registerDriver(new com.fmi.jdbc.JdbcDriver());
   85:       DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance());
   86:       DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.Driver").newInstance());
   87:       DriverManager.registerDriver((Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance());
   88:       DriverManager.registerDriver((Driver) Class.forName("acs.jdbc.Driver").newInstance());
   89:      
   90:       // wait a maximum of 10 seconds when attempting to establish a connection
   91:       DriverManager.setLoginTimeout(10);
   92:     } catch (Exception e)
   93:     {
   94:       System.out.println(e);
   95:     }
   96:   }
   97:   /**
   98:    * Constructs a database bean
   99:    */
  100:   public DBBean()
  101:   {
  102: 
  103:   }
  104:   /**
  105:    *Constructs a database bean
  106:    * and tries to connect to database
  107:    *specified in the jdbcConnectionURL
  108:    * @param jdbcConnectionURL url to connect to database
  109:     */
  110: 
  111:   public DBBean(String jdbcConnectionURL) throws Exception
  112:   {
  113: 
  114:     this.url = jdbcConnectionURL;
  115: 
  116:     connection = getConnection();
  117:     if (connection == null)
  118:       return;
  119:     // get the meta data for the current connection
  120:     DatabaseMetaData dbMetaData = (DatabaseMetaData) connection.getMetaData();
  121:     quoteChar = dbMetaData.getIdentifierQuoteString();
  122:     if (quoteChar == null)
  123:       quoteChar = "\""; // needed for postgres
  124: 
  125:     // create the root node of the tree
  126:     // get the open tables from the current connection; the FileMaker Pro
  127:     // JDBC driver ignores all of the parameters to this method
  128: 
  129:     // iterate over the table names and add them to the root node of the tree
  130: 
  131:   }
  132:   /**
  133:    * sets the maximum number of hits
  134:    */
  135:   public void setMaxHits(int maxHits)
  136:   {
  137:     this.maxHits = maxHits;
  138:   }
  139:   /**
  140:     * gets the maximum number of hits
  141:     */
  142:   public int getMaxHits()
  143:   {
  144:     return maxHits;
  145:   }
  146: 
  147:   /**
  148:    * returns the Connection if null creates a new one from the url property.
  149:    *
  150:    */
  151:   public Connection getConnection() throws Exception
  152:   {
  153:     ConnectionPool conPool = (ConnectionPool) connectionPool.get(url);
  154:     if (conPool == null)
  155:     {
  156:       createConnection();  
  157:       
  158:     } else
  159:     {
  160:       if (!conPool.user.equals(user) || !conPool.passwd.equals(passwd))
  161:       {
  162:         conPool.con.close();
  163:         conPool.user = user;
  164:         conPool.passwd = passwd;
  165: 
  166:       }
  167:       connection = conPool.con;
  168:       if (connection.isClosed())
  169:       {
  170:         System.out.println("Made new connection!!!");
  171:         createConnection();  
  172:    
  173:        // connection = DriverManager.getConnection(conPool.url, conPool.user, conPool.passwd);
  174:         conPool.con = connection;
  175:       }
  176:     }
  177:     if (url != "" && connection == null)
  178:       createConnection();
  179:       //connection = DriverManager.getConnection(url, user, passwd);
  180:     dbMetaData = connection.getMetaData();
  181:     quoteChar = dbMetaData.getIdentifierQuoteString();
  182:     if (quoteChar == null)
  183:       quoteChar = "\""; // needed for postgres
  184: 
  185:     return connection;
  186:   }
  187:   private void createConnection() throws SQLException
  188:   {
  189:       // setup the properties 
  190:        java.util.Properties prop = new java.util.Properties();
  191:        prop.put("charSet", "Cp1256");
  192:        prop.put("user", user);
  193:        prop.put("password", passwd);
  194:     System.out.println("url "+url);
  195:     if(url.indexOf("odbc")>=0)
  196:     {
  197:     // Connect to the database
  198:       connection = DriverManager.getConnection(url, prop);
  199:       System.out.println("odbc with properties inited");
  200:     }
  201:     else
  202:     connection = DriverManager.getConnection(url, user, passwd);
  203:     connectionPool.put(url, new ConnectionPool(url, user, passwd, connection));
  204:     
  205:   }
  206:   /**
  207:    * sets the connection of this DBBean to the database specified in the url
  208:    *  property
  209:    */
  210:   public void setConnection(String url) throws Exception
  211:   {
  212:     this.url = url;
  213:     if (url != "")
  214:       //connection = DriverManager.getConnection(url, user, passwd);
  215:     createConnection();
  216:     dbMetaData = connection.getMetaData();
  217:     quoteChar = dbMetaData.getIdentifierQuoteString();
  218:     if (quoteChar == null)
  219:       quoteChar = "\""; // needed for postgres
  220:   }
  221:   /**
  222:    * sets the connection of this DBBean to the database specified in the url
  223:    * and the url,user and passwd property of this DBBean instance
  224:    */
  225:   public void setConnection(String url, String user, String passwd) throws Exception
  226:   {
  227:     this.user = user;
  228:     this.passwd = passwd;
  229:     this.url = url;
  230:     if (url != "")
  231:      createConnection();
  232:      // connection = DriverManager.getConnection(url, user, passwd);
  233:     dbMetaData = connection.getMetaData();
  234:     quoteChar = dbMetaData.getIdentifierQuoteString();
  235:     if (quoteChar == null)
  236:       quoteChar = "\""; // needed for postgres
  237:   }
  238: 
  239:   public void setIDVector(Vector ids)
  240:   {
  241:     this.ids = ids;
  242:   }
  243: 
  244:   /** 
  245:    * returns a Vector containing the ID Row Name 
  246:    **/
  247:   public Vector getIDVector()
  248:   {
  249:     return ids;
  250:   }
  251:   /**
  252:    * returns a Vector containing the Tablenames or an error message in the Vector
  253:    */
  254:   public Vector getTableNames()
  255:   {
  256:     Vector tableNameVec = new Vector();
  257:     try
  258:     {
  259:       if (connection == null)
  260:       {
  261:         Vector vec = new Vector();
  262:         vec.add("no database connection");
  263:         return vec;
  264:       }
  265:       if (dbMetaData == null)
  266:         dbMetaData = connection.getMetaData();
  267:       ResultSet tableNames = dbMetaData.getTables(null, null, null, null);
  268:       // System.out.println(dbMetaData.supportsAlterTableWithAddColumn());
  269:       // iterate over the table names and add them to the root node of the tree
  270: 
  271:       while (tableNames.next())
  272:       {
  273:         String tableName = tableNames.getString("TABLE_NAME");
  274:         tableNameVec.add(tableName);
  275: 
  276:       }
  277:     } catch (Exception e)
  278:     {
  279:       e.printStackTrace();
  280:     }
  281:     return tableNameVec;
  282:   }
  283:   /**
  284:    * returns a Vector containing the Tablenames or an error message in the Vector
  285:    */
  286:   public Vector getTableNames(String catalog)
  287:   {
  288:     Vector tableNameVec = new Vector();
  289:     try
  290:     {
  291:       if (connection == null)
  292:       {
  293:         Vector vec = new Vector();
  294:         vec.add("no database connection");
  295:         return vec;
  296:       }
  297:       setConnection(url.substring(0, url.lastIndexOf("/") + 1) + catalog);
  298:       if (dbMetaData == null)
  299:         dbMetaData = connection.getMetaData();
  300:       System.out.println("catalog " + catalog + " " + dbMetaData.getCatalogSeparator() + " " + url.substring(0, url.lastIndexOf("/") + 1));
  301:       ResultSet tableNames = dbMetaData.getTables(null, null, null, null);
  302:       // System.out.println(dbMetaData.supportsAlterTableWithAddColumn());
  303:       // iterate over the table names and add them to the root node of the tree
  304: 
  305:       while (tableNames.next())
  306:       {
  307:         String tableName = tableNames.getString("TABLE_NAME");
  308:         tableNameVec.add(tableName);
  309: 
  310:       }
  311:     } catch (Exception e)
  312:     {
  313:       e.printStackTrace();
  314:     }
  315:     return tableNameVec;
  316:   }
  317: 
  318:   /**
  319:    * returns a Vector containing the Catalog or an error message in the Vector
  320:    */
  321:   public Vector getCatalogs()
  322:   {
  323:     Vector tableNameVec = new Vector();
  324:     try
  325:     {
  326:       if (connection == null)
  327:       {
  328:         Vector vec = new Vector();
  329:         vec.add("no database connection");
  330:         return vec;
  331:       }
  332:       if (dbMetaData == null)
  333:         dbMetaData = connection.getMetaData();
  334:       ResultSet tableNames = dbMetaData.getCatalogs();
  335:       // System.out.println(dbMetaData.supportsAlterTableWithAddColumn());
  336:       // iterate over the table names and add them to the root node of the tree
  337:       System.out.println(tableNames.getMetaData().getColumnName(1));
  338:       while (tableNames.next())
  339:       {
  340:         String tableName = tableNames.getString(1);
  341:         tableNameVec.add(tableName);
  342:         //tableName = tableNames.getString(1);
  343:         //tableNameVec.add(tableName);
  344: 
  345:       }
  346:     } catch (Exception e)
  347:     {
  348:       e.printStackTrace();
  349:     }
  350:     return tableNameVec;
  351:   }
  352: 
  353:   /**
  354:   * returns a Vector containing the layoutNames for the specified Table
  355:   * if the database supports this otherwise Vector containing an empty String
  356:   */
  357: 
  358:   public Vector getLayoutNames(String tableName) throws SQLException
  359:   {
  360:     Vector layouts = new Vector();
  361:     if (dbMetaData instanceof DatabaseMetaDataExt)
  362:       layouts.add("");
  363:     if (dbMetaData == null)
  364:       dbMetaData = connection.getMetaData();
  365: 
  366:     if (dbMetaData instanceof DatabaseMetaDataExt)
  367:     {
  368:       ResultSet layoutNames = ((DatabaseMetaDataExt) dbMetaData).getLayouts(null, null, tableName, null);
  369: 
  370:       // iterate over the layout names and add them to the "Layouts" node
  371:       while (layoutNames.next())
  372:         layouts.add(layoutNames.getString("LAYOUT_NAME"));
  373:     }
  374:     return layouts;
  375:   }
  376:   /**
  377:    *   Returns the result for select * from table
  378:    *   with maxHits = 500 default value
  379:    */
  380:   public Vector[] getQueryData(String table) throws SQLException
  381:   {
  382: 
  383:     return getQueryData("SELECT * from " + quoteChar + table + quoteChar, maxHits);
  384: 
  385:   }
  386: 
  387:   /**
  388:    *    Returns the result of the query
  389:    *    or an Vector array of Vectors containing error messages
  390:    */
  391:   public Vector[] getQueryData(String query, FM2SQL.ProgressDialog dialog, int maxHits) throws SQLException
  392:   {
  393:     long timeStart = System.currentTimeMillis();
  394:     ResultSet resultSet = null;
  395:     if (connection == null)
  396:     {
  397:       Vector[] noData = new Vector[2];
  398:       //System.out.println("Exception occured");
  399:       noData[1] = new Vector();
  400:       Vector vec2 = new Vector();
  401:       noData[0] = new Vector();
  402:       vec2.add("no Connection available");
  403:       noData[0].add(vec2);
  404:       noData[1].add("Exception occured! No results available");
  405:       //noData[1].add("no Results were produced");
  406: 
  407:       return noData;
  408:     }
  409:     if (dialog != null)
  410:       dialog.progress.setValue(0);
  411: 
  412:     resultSet = makeQuery(query, maxHits);
  413: 
  414:     metaData = resultSet.getMetaData();
  415:     int columnCount = metaData.getColumnCount();
  416:     int rowCount = 0;
  417:     if (maxHits == 0)
  418:       rowCount = (metaData instanceof ResultSetMetaDataExt) ? 1000 : getRowCount(query);
  419:     else
  420:       rowCount = maxHits;
  421:     int counter = 0;
  422:     Vector tableData = new Vector();
  423:     Vector tableRow = new Vector();
  424:     //  System.out.println("rowCount "+rowCount+" "+maxHits);
  425:     try
  426:     {
  427:       while ((tableRow = getNextRow()) != null)
  428:       {
  429:         counter++;
  430:         if (dialog != null)
  431:           dialog.progress.setValue((int) ((double) counter / (double) rowCount * 100.0));
  432: 
  433:         tableData.add(tableRow);
  434: 
  435:       }
  436:     } catch (Exception e)
  437:     {
  438:       // TODO Auto-generated catch block
  439:       e.printStackTrace();
  440:     }
  441: 
  442:     // retrieve the column names from the result set; the column names
  443:     // are used for the table header
  444:     columnNames = new Vector();
  445: 
  446:     for (int i = 1; i <= columnCount; i++)
  447:       columnNames.addElement(metaData.getColumnName(i));
  448:     Vector data[] = new Vector[2];
  449:     data[0] = tableData;
  450:     data[1] = columnNames;
  451:     System.out.println("Rows " + tableData.size() + " " + ((Vector) tableData.get(0)).size());
  452:     long timeEnd = System.currentTimeMillis();
  453:     System.out.println("Time needed for query and data retrieval " + (timeEnd - timeStart) + " ms");
  454:     return data;
  455:   }
  456:   /**
  457:    *    Returns the result of the query
  458:    *    or an Vector array of Vectors containing error messages
  459:    */
  460:   public Vector[] getQueryData(String query, int maxHits) throws SQLException
  461:   {
  462:     long timeStart = System.currentTimeMillis();
  463:     ResultSet resultSet = null;
  464:     if (connection == null)
  465:     {
  466:       Vector[] noData = new Vector[2];
  467:       //System.out.println("Exception occured");
  468:       noData[1] = new Vector();
  469:       Vector vec2 = new Vector();
  470:       noData[0] = new Vector();
  471:       vec2.add("no Connection available");
  472:       noData[0].add(vec2);
  473:       noData[1].add("Exception occured! No results available");
  474:       //noData[1].add("no Results were produced");
  475: 
  476:       return noData;
  477:     }
  478:     resultSet = makeQuery(query, maxHits);
  479:     metaData = resultSet.getMetaData();
  480:     int columnCount = metaData.getColumnCount();
  481: 
  482:     Vector tableData = new Vector();
  483:     while (resultSet.next())
  484:     {
  485:       //System.out.println("datatype "+(Types.LONGVARCHAR ==metaData.getColumnType(3)));
  486:       Vector tableRow = new Vector(), m_columnClasses = new Vector();
  487:       for (int i = 1; i <= columnCount; i++)
  488:       {
  489:         // repeating fields and fields from related databases may contain
  490:         // multliple data values; the data values are stored using
  491:         // a Vector which is then added to the tableRow
  492:         //      if (metaData instanceof ResultSetMetaDataExt)
  493:         if ((metaData instanceof ResultSetMetaDataExt) && (((ResultSetMetaDataExt) metaData).isRelated(i) || ((ResultSetMetaDataExt) metaData).isRepeating(i)))
  494:         {
  495:           //System.out.println("Related fields");
  496:           // retrieve the repeating or related field contents as a
  497:           // com.fmi.jdbc.Array via the ResultSet.getObject method
  498:           com.fmi.jdbc.Array array = (com.fmi.jdbc.Array) resultSet.getObject(i);
  499:           //            create a Vector for storing all of the data values
  500:           ArrayList columnData = new ArrayList();
  501: 
  502:           try
  503:           {
  504: 
  505:             // call the Array.getStringArray method since the data will
  506:             // only be displayed
  507:             Object[] fieldData = (Object[]) array.getArray();
  508: 
  509:             if (fieldData != null)
  510:             {
  511:               // add each value to the Vector
  512:               for (int j = 0; j < fieldData.length; j++)
  513:               {
  514:                 if (fieldData[j] != null)
  515:                   columnData.add(fieldData[j]);
  516:               }
  517: 
  518:             }
  519:           } catch (Exception e)
  520:           {
  521:             //System.out.println(e);
  522:           }
  523:           if (columnData.isEmpty())
  524:             tableRow.add(null);
  525:           else
  526:             tableRow.addElement(columnData);
  527:           //System.out.println(columnData);
  528:           //System.out.println("Related fields"+columnData.size()+" "+tableRow.size());
  529: 
  530:           m_columnClasses.addElement(java.util.Vector.class);
  531:         } else if (metaData.getColumnType(i) == Types.LONGVARBINARY)
  532:         {
  533:           // use the ResultSet.getObject method for retrieving images
  534:           // from FileMaker Pro container fields; the ResultSet.getObject
  535:           // method returns a java.awt.Image object for FileMaker Pro
  536:           // container fields
  537:           try
  538:           {
  539: 
  540:             tableRow.addElement(resultSet.getObject(i));
  541:           } catch (Exception e)
  542:           {
  543:             // TODO Auto-generated catch block
  544:             //e.printStackTrace();
  545:             tableRow.addElement(null);
  546:           }
  547:           //tableRow.addElement("Picture ignored");
  548:           m_columnClasses.addElement(java.awt.Image.class);
  549:         } else if (metaData.getColumnType(i) == Types.TIME)
  550:         {
  551:           // use the ResultSet.getObject method for retieving images
  552:           // from FileMaker Pro container fields; the ResultSet.getObject
  553:           // method returns a java.awt.Image object for FileMaker Pro
  554:           // container fields
  555:           try
  556:           {
  557:             tableRow.addElement(resultSet.getTime(i).toString());
  558:             m_columnClasses.addElement(java.sql.Time.class);
  559:           } catch (Exception e)
  560:           {
  561: 
  562:             String value = resultSet.getString(i);
  563:             if (value != null)
  564:             {
  565:               //System.out.println("SQLTime new "+Time.valueOf("17:00:00").toString());
  566:               int index = 0;
  567:               for (int j = 0; j < value.length(); ++j)
  568:               {
  569:                 if (!Character.isLetter(value.charAt(j)))
  570:                   index = j + 1;
  571:                 else
  572:                   break;
  573:               }
  574: 
  575:               tableRow.addElement(value.substring(0, index));
  576:               //m_columnClasses.addElement(java.sql.Time.class);
  577:             } else
  578:               tableRow.add(null);
  579:             m_columnClasses.addElement(String.class);
  580:           } // to catch
  581: 
  582:         } else if (metaData.getColumnType(i) == Types.DATE)
  583:         {
  584:           // use the ResultSet.getObject method for retieving images
  585:           // from FileMaker Pro container fields; the ResultSet.getObject
  586:           // method returns a java.awt.Image object for FileMaker Pro
  587:           // container fields
  588: 
  589:           tableRow.addElement(resultSet.getDate(i));
  590:           m_columnClasses.addElement(java.sql.Date.class);
  591:         } else if (metaData.getColumnTypeName(i) == "NUMBER")
  592:         {
  593:           // use the ResultSet.getObject method for retieving images
  594:           // from FileMaker Pro container fields; the ResultSet.getObject
  595:           // method returns a java.awt.Image object for FileMaker Pro
  596:           // container fields
  597:           try
  598:           {
  599:             tableRow.addElement(new Double(resultSet.getDouble(i)));
  600:             m_columnClasses.addElement(Double.class);
  601: 
  602:           } catch (Exception e)
  603:           {
  604: 
  605:             StringBuffer number = new StringBuffer();
  606:             String value = resultSet.getString(i);
  607:             System.out.println(value);
  608:             for (int c = 0; c < value.length(); ++c)
  609:             {
  610:               if (Character.isDigit(value.charAt(c)))
  611:               {
  612:                 number.append(value.charAt(c));
  613:               }
  614:             }
  615:             if (number.length() > 0)
  616:             {
  617:               tableRow.addElement(null);
  618:               m_columnClasses.addElement(Double.class);
  619:             } else
  620:               tableRow.addElement(null);
  621:           }
  622:         } else
  623:         {
  624:           // all other field values are retrieved as strings and
  625:           // added to the tableRow Vector
  626:           // if(resultSet.getObject(i)!=null) System.out.println(resultSet.getObject(i));
  627:           try
  628:           {
  629:             byte[] b = resultSet.getBytes(i);
  630:             String utf8 = null;
  631:             if (metaData instanceof ResultSetMetaDataExt)
  632:               utf8 = (b == null) ? null : new String(b);
  633:             else
  634:               utf8 = (b == null) ? null : new String(b, "UTF-8");
  635:             utf8 = (utf8 == null) ? null : new String(utf8.getBytes("UTF-8"), "UTF-8");
  636:             tableRow.addElement(utf8);
  637:           } catch (Exception e)
  638:           {
  639:             System.out.println("Hey I Got an error" + e);
  640:           }
  641:           m_columnClasses.addElement(java.lang.String.class);
  642:         }
  643:       }
  644: 
  645:       // add the tableRow Vector to the tableData Vector
  646:       tableData.addElement(tableRow);
  647:     }
  648: 
  649:     // retrieve the column names from the result set; the column names
  650:     // are used for the table header
  651:     columnNames = new Vector();
  652: 
  653:     for (int i = 1; i <= columnCount; i++)
  654:       columnNames.addElement(metaData.getColumnName(i));
  655:     Vector data[] = new Vector[2];
  656:     data[0] = tableData;
  657:     data[1] = columnNames;
  658:     System.out.println("Rows " + tableData.size() + " " + ((Vector) tableData.get(0)).size());
  659:     long timeEnd = System.currentTimeMillis();
  660:     System.out.println("Time needed for query and data retrieval " + (timeEnd - timeStart) + " ms");
  661:     return data;
  662:   }
  663: 
  664:   public Vector getColumnNames()
  665:   {
  666:     if (result == null)
  667:       return null;
  668:     try
  669:     {
  670:       ResultSetMetaData metaData = result.getMetaData();
  671:       int columnCount = metaData.getColumnCount();
  672:       columnNames = new Vector();
  673: 
  674:       for (int i = 1; i <= columnCount; i++)
  675:         columnNames.addElement(metaData.getColumnName(i));
  676:     } catch (Exception e)
  677:     {
  678:     }
  679:     return columnNames;
  680:   }
  681:   /**
  682:    * makes the database Query
  683:    *   with the numberOfHits as maximum
  684:    * @return the result as an ResultSet object
  685:   */
  686:   public ResultSet makeQuery(String query, int numberOfHits) throws SQLException
  687:   {
  688:     result = null;
  689:     Statement stm = null;
  690:    
  691:     //  System.out.println("Query " + query);
  692: 
  693:     if (!connection.isClosed())
  694:       stm = connection.createStatement();
  695:     else {
  696:       
  697:       
  698:       try
  699:       {
  700:         connection = getConnection();
  701:         stm= connection.createStatement();
  702:       } catch (Exception e)
  703:       {
  704:         // TODO Auto-generated catch block
  705:         e.printStackTrace();
  706:       }
  707:     }
  708:     stm.setMaxRows(numberOfHits);
  709:     long time = System.currentTimeMillis();
  710:     try {
  711:     stm.execute(query);
  712:     long time2 = System.currentTimeMillis();
  713:    
  714:     System.out.println("time to execute "+(time2-time));
  715:    // stm.setMaxRows(numberOfHits);
  716:  
  717:     result = stm.getResultSet();
  718:    // System.out.println(result+" "+stm.getUpdateCount());
  719:     metaData = result.getMetaData();
  720:     } catch(Exception e) {
  721:       // TODO remove
  722:       if(FM2SQL.fmInstance!=null)
  723:       FM2SQL.showErrorDialog("Error caught!! \n Query was  "+query+" \n","Debug Info");
  724:      }
  725:  
  726:     return result;
  727:   }
  728:   /**
  729:    *  sets the database user
  730:    */
  731:   public void setUser(String user)
  732:   {
  733:     this.user = user;
  734:   }
  735:   /**
  736:    *     sets the database passwd
  737:    */
  738:   public void setPasswd(String passwd)
  739:   {
  740:     this.passwd = passwd;
  741:   }
  742: 
  743:   /**
  744:    * sets the database user and passwd
  745:    */
  746:   public void setUserAndPasswd(String user, String passwd)
  747:   {
  748:     this.user = user;
  749:     this.passwd = passwd;
  750: 
  751:   }
  752:   /**
  753:    *  just  sets the connection URL
  754:    */
  755:   public void setURL(String url)
  756:   {
  757:     this.url = url;
  758:   }
  759: 
  760:   /**
  761:    *    Test the database drivers features given by the DatabaseMetaData object
  762:    */
  763:   public Vector[] TestDB(DatabaseMetaData d) throws SQLException
  764:   {
  765: 
  766:     Vector data[] = new Vector[2];
  767:     Vector[] rows = new Vector[120];
  768:     for (int i = 0; i < rows.length; ++i)
  769:       rows[i] = new Vector();
  770:     Vector columnNames = new Vector();
  771:     columnNames.add("Feature");
  772:     columnNames.add("Supported");
  773: 
  774:     Vector cols = new Vector();
  775:     rows[0].add("allProceduresAreCallable");
  776:     rows[0].add(new Boolean(d.allProceduresAreCallable()));
  777:     //boolean allProceduresAreCallable() throws SQLException;
  778:     rows[1].add("allTablesAreSelectable");
  779:     rows[1].add(new Boolean(d.allTablesAreSelectable()));
  780:     //    boolean allTablesAreSelectable() throws SQLException;
  781:     rows[2].add("isReadOnly");
  782:     rows[2].add(new Boolean(d.isReadOnly()));
  783:     //    boolean isReadOnly() throws SQLException;
  784:     rows[3].add("nullsAreSortedHigh");
  785:     rows[3].add(new Boolean(d.nullsAreSortedHigh()));
  786:     //    boolean nullsAreSortedHigh() throws SQLException;
  787:     rows[4].add("nullsAreSortedLow");
  788:     rows[4].add(new Boolean(d.nullsAreSortedLow()));
  789:     // boolean nullsAreSortedLow() throws SQLException;
  790:     rows[5].add("nullsAreSortedAtStart");
  791:     rows[5].add(new Boolean(d.nullsAreSortedAtStart()));
  792:     //  boolean nullsAreSortedAtStart() throws SQLException;
  793:     rows[6].add("nullsAreSortedAtEnd");
  794:     rows[6].add(new Boolean(d.nullsAreSortedAtEnd()));
  795:     //  boolean nullsAreSortedAtEnd() throws SQLException;
  796:     rows[7].add("usesLocalFiles");
  797:     rows[7].add(new Boolean(d.usesLocalFiles()));
  798:     //    boolean usesLocalFiles() throws SQLException;
  799:     rows[8].add("usesLocalFilePerTable");
  800:     rows[8].add(new Boolean(d.usesLocalFilePerTable()));
  801:     // boolean usesLocalFilePerTable() throws SQLException;
  802:     rows[9].add("supportsMixedCaseIdentifiers");
  803:     rows[9].add(new Boolean(d.supportsMixedCaseIdentifiers()));
  804:     //boolean supportsMixedCaseIdentifiers() throws SQLException;
  805:     rows[10].add("storesUpperCaseIdentifiers");
  806:     rows[10].add(new Boolean(d.storesUpperCaseIdentifiers()));
  807:     // boolean storesUpperCaseIdentifiers() throws SQLException;
  808:     rows[11].add("storesLowerCaseIdentifiers");
  809:     rows[11].add(new Boolean(d.storesLowerCaseIdentifiers()));
  810:     //    boolean storesLowerCaseIdentifiers() throws SQLException;
  811:     rows[12].add("storesMixedCaseIdentifiers");
  812:     rows[12].add(new Boolean(d.storesMixedCaseIdentifiers()));
  813:     //    boolean storesMixedCaseIdentifiers() throws SQLException;
  814:     rows[13].add("supportsMixedCaseQuotedIdentifiers");
  815:     rows[13].add(new Boolean(d.supportsMixedCaseQuotedIdentifiers()));
  816:     //    boolean supportsMixedCaseQuotedIdentifiers() throws SQLException;
  817:     rows[14].add("storesUpperCaseQuotedIdentifiers");
  818:     rows[14].add(new Boolean(d.storesUpperCaseQuotedIdentifiers()));
  819:     //   boolean storesUpperCaseQuotedIdentifiers() throws SQLException;
  820:     rows[15].add("storesLowerCaseQuotedIdentifiers");
  821:     rows[15].add(new Boolean(d.storesLowerCaseQuotedIdentifiers()));
  822:     //boolean storesLowerCaseQuotedIdentifiers() throws SQLException;
  823:     rows[16].add("storesMixedCaseQuotedIdentifiers");
  824:     rows[16].add(new Boolean(d.storesMixedCaseQuotedIdentifiers()));
  825:     // boolean storesMixedCaseQuotedIdentifiers() throws SQLException;
  826:     rows[17].add("supportsAlterTableWithAddColumn");
  827:     rows[17].add(new Boolean(d.supportsAlterTableWithAddColumn()));
  828:     //    boolean supportsAlterTableWithAddColumn() throws SQLException;
  829:     rows[18].add("supportsAlterTableWithDropColumn");
  830:     rows[18].add(new Boolean(d.supportsAlterTableWithDropColumn()));
  831:     //  boolean supportsAlterTableWithDropColumn() throws SQLException;
  832:     rows[19].add("nullPlusNonNullIsNull");
  833:     rows[19].add(new Boolean(d.nullPlusNonNullIsNull()));
  834:     //   boolean nullPlusNonNullIsNull() throws SQLException;
  835:     rows[20].add("supportsConvert");
  836:     rows[20].add(new Boolean(d.supportsConvert()));
  837:     // boolean supportsConvert() throws SQLException;
  838: 
  839:     // boolean supportsConvert(int fromType, int toType) throws SQLException;
  840:     rows[21].add("supportsTableCorrelationNames");
  841:     rows[21].add(new Boolean(d.supportsTableCorrelationNames()));
  842:     //  boolean supportsTableCorrelationNames() throws SQLException;
  843:     rows[22].add("supportsDifferentTableCorrelationNames");
  844:     rows[22].add(new Boolean(d.supportsDifferentTableCorrelationNames()));
  845:     // boolean supportsDifferentTableCorrelationNames() throws SQLException;
  846:     rows[23].add("supportsExpressionsInOrderBy");
  847:     rows[23].add(new Boolean(d.supportsExpressionsInOrderBy()));
  848:     // boolean supportsExpressionsInOrderBy() throws SQLException;
  849:     rows[24].add("supportsOrderByUnrelated");
  850:     rows[24].add(new Boolean(d.supportsOrderByUnrelated()));
  851:     //   boolean supportsOrderByUnrelated() throws SQLException;
  852:     rows[25].add("supportsGroupBy");
  853:     rows[25].add(new Boolean(d.supportsGroupBy()));
  854:     //  boolean supportsGroupBy() throws SQLException;
  855:     rows[26].add("supportsGroupByUnrelated");
  856:     rows[26].add(new Boolean(d.supportsGroupByUnrelated()));
  857:     // boolean supportsGroupByUnrelated() throws SQLException;
  858:     rows[27].add("supportsGroupByBeyondSelect");
  859:     rows[27].add(new Boolean(d.supportsGroupByBeyondSelect()));
  860:     //  boolean supportsGroupByBeyondSelect() throws SQLException;
  861:     rows[28].add("supportsLikeEscapeClause");
  862:     rows[28].add(new Boolean(d.supportsLikeEscapeClause()));
  863:     // boolean supportsLikeEscapeClause() throws SQLException;
  864:     rows[29].add("supportsMultipleResultSets");
  865:     rows[29].add(new Boolean(d.supportsMultipleResultSets()));
  866:     // boolean supportsMultipleResultSets() throws SQLException;
  867:     rows[30].add("supportsMultipleTransactions");
  868:     rows[30].add(new Boolean(d.supportsMultipleTransactions()));
  869:     //  boolean supportsMultipleTransactions() throws SQLException;
  870:     rows[31].add("supportsNonNullableColumns");
  871:     rows[31].add(new Boolean(d.supportsNonNullableColumns()));
  872:     //    boolean supportsNonNullableColumns() throws SQLException;
  873:     rows[32].add("supportsMinimumSQLGrammar");
  874:     rows[32].add(new Boolean(d.supportsMinimumSQLGrammar()));
  875:     // boolean supportsMinimumSQLGrammar() throws SQLException;
  876:     rows[33].add("supportsCoreSQLGrammar");
  877:     rows[33].add(new Boolean(d.supportsCoreSQLGrammar()));
  878:     // boolean supportsCoreSQLGrammar() throws SQLException;
  879:     rows[34].add("supportsExtendedSQLGrammar");
  880:     rows[34].add(new Boolean(d.supportsExtendedSQLGrammar()));
  881:     // boolean supportsExtendedSQLGrammar() throws SQLException;
  882:     rows[35].add("supportsANSI92EntryLevelSQL");
  883:     rows[35].add(new Boolean(d.supportsANSI92EntryLevelSQL()));
  884:     // boolean supportsANSI92EntryLevelSQL() throws SQLException;
  885:     rows[36].add("supportsANSI92IntermediateSQL");
  886:     rows[36].add(new Boolean(d.supportsANSI92IntermediateSQL()));
  887:     //boolean supportsANSI92IntermediateSQL() throws SQLException;
  888:     rows[37].add("supportsANSI92FullSQL");
  889:     rows[37].add(new Boolean(d.supportsANSI92FullSQL()));
  890:     //boolean supportsANSI92FullSQL() throws SQLException;
  891:     rows[38].add("supportsIntegrityEnhancementFacility");
  892:     rows[38].add(new Boolean(d.supportsIntegrityEnhancementFacility()));
  893:     //boolean supportsIntegrityEnhancementFacility() throws SQLException;
  894:     rows[39].add("supportsOuterJoins");
  895:     rows[39].add(new Boolean(d.supportsOuterJoins()));
  896:     //boolean supportsOuterJoins() throws SQLException;
  897:     rows[40].add("supportsFullOuterJoins");
  898:     rows[40].add(new Boolean(d.supportsFullOuterJoins()));
  899:     //boolean supportsFullOuterJoins() throws SQLException;
  900:     rows[41].add("supportsLimitedOuterJoins");
  901:     rows[41].add(new Boolean(d.supportsLimitedOuterJoins()));
  902:     //boolean supportsLimitedOuterJoins() throws SQLException;
  903:     rows[42].add("isCatalogAtStart");
  904:     rows[42].add(new Boolean(d.isCatalogAtStart()));
  905:     //boolean isCatalogAtStart() throws SQLException;
  906:     rows[43].add("supportsSchemasInDataManipulation");
  907:     rows[43].add(new Boolean(d.supportsSchemasInDataManipulation()));
  908:     //boolean supportsSchemasInDataManipulation() throws SQLException;
  909:     rows[44].add("supportsSchemasInProcedureCalls");
  910:     rows[44].add(new Boolean(d.supportsSchemasInProcedureCalls()));
  911:     //boolean supportsSchemasInProcedureCalls() throws SQLException;
  912:     rows[45].add("supportsSchemasInTableDefinitions");
  913:     rows[45].add(new Boolean(d.supportsSchemasInTableDefinitions()));
  914:     //boolean supportsSchemasInTableDefinitions() throws SQLException;
  915:     rows[46].add("supportsSchemasInIndexDefinitions");
  916:     rows[46].add(new Boolean(d.supportsSchemasInIndexDefinitions()));
  917:     //boolean supportsSchemasInIndexDefinitions() throws SQLException;
  918:     rows[47].add("supportsSchemasInPrivilegeDefinitions");
  919:     rows[47].add(new Boolean(d.supportsSchemasInPrivilegeDefinitions()));
  920:     //boolean supportsSchemasInPrivilegeDefinitions() throws SQLException;
  921:     rows[48].add("supportsCatalogsInDataManipulation");
  922:     rows[48].add(new Boolean(d.supportsCatalogsInDataManipulation()));
  923:     //boolean supportsCatalogsInDataManipulation() throws SQLException;
  924:     rows[49].add("supportsCatalogsInProcedureCalls");
  925:     rows[49].add(new Boolean(d.supportsCatalogsInProcedureCalls()));
  926:     //boolean supportsCatalogsInProcedureCalls() throws SQLException;
  927:     rows[50].add("supportsCatalogsInTableDefinitions");
  928:     rows[50].add(new Boolean(d.supportsCatalogsInTableDefinitions()));
  929:     //boolean supportsCatalogsInTableDefinitions() throws SQLException;
  930:     rows[51].add("supportsCatalogsInIndexDefinitions");
  931:     rows[51].add(new Boolean(d.supportsCatalogsInIndexDefinitions()));
  932:     //boolean supportsCatalogsInIndexDefinitions() throws SQLException;
  933:     rows[52].add("supportsCatalogsInPrivilegeDefinitions");
  934:     rows[52].add(new Boolean(d.supportsCatalogsInPrivilegeDefinitions()));
  935:     //boolean supportsCatalogsInPrivilegeDefinitions() throws SQLException;
  936:     rows[53].add("supportsPositionedDelete");
  937:     rows[53].add(new Boolean(d.supportsPositionedDelete()));
  938:     //boolean supportsPositionedDelete() throws SQLException;
  939:     rows[54].add("supportsPositionedUpdate");
  940:     rows[54].add(new Boolean(d.supportsPositionedUpdate()));
  941:     //boolean supportsPositionedUpdate() throws SQLException;
  942:     rows[55].add("supportsSelectForUpdate");
  943:     rows[55].add(new Boolean(d.supportsSelectForUpdate()));
  944:     //boolean supportsSelectForUpdate() throws SQLException;
  945:     rows[56].add("supportsStoredProcedures");
  946:     rows[56].add(new Boolean(d.supportsStoredProcedures()));
  947:     //boolean supportsStoredProcedures() throws SQLException;
  948:     rows[57].add("supportsSubqueriesInComparisons");
  949:     rows[57].add(new Boolean(d.supportsSubqueriesInComparisons()));
  950:     //boolean supportsSubqueriesInComparisons() throws SQLException;
  951:     rows[58].add("supportsSubqueriesInExists");
  952:     rows[58].add(new Boolean(d.supportsSubqueriesInExists()));
  953:     //boolean supportsSubqueriesInExists() throws SQLException;
  954:     rows[59].add("supportsSubqueriesInIns");
  955:     rows[59].add(new Boolean(d.supportsSubqueriesInIns()));
  956:     //boolean supportsSubqueriesInIns() throws SQLException;
  957:     rows[60].add("supportsSubqueriesInQuantifieds");
  958:     rows[60].add(new Boolean(d.supportsSubqueriesInQuantifieds()));
  959:     //boolean supportsSubqueriesInQuantifieds() throws SQLException;
  960:     rows[61].add("supportsCorrelatedSubqueries");
  961:     rows[61].add(new Boolean(d.supportsCorrelatedSubqueries()));
  962:     //boolean supportsCorrelatedSubqueries() throws SQLException;
  963:     rows[62].add("supportsUnion");
  964:     rows[62].add(new Boolean(d.supportsUnion()));
  965:     //boolean supportsUnion() throws SQLException;
  966:     rows[63].add("supportsUnionAll");
  967:     rows[63].add(new Boolean(d.supportsUnionAll()));
  968:     //boolean supportsUnionAll() throws SQLException;
  969:     rows[64].add("supportsOpenCursorsAcrossCommit");
  970:     rows[64].add(new Boolean(d.supportsOpenCursorsAcrossCommit()));
  971:     //boolean supportsOpenCursorsAcrossCommit() throws SQLException;
  972:     rows[65].add("supportsOpenCursorsAcrossRollback");
  973:     rows[65].add(new Boolean(d.supportsOpenCursorsAcrossRollback()));
  974:     //boolean supportsOpenCursorsAcrossRollback() throws SQLException;
  975:     rows[66].add("supportsOpenStatementsAcrossCommit");
  976:     rows[66].add(new Boolean(d.supportsOpenStatementsAcrossCommit()));
  977:     //boolean supportsOpenStatementsAcrossCommit() throws SQLException;
  978:     rows[67].add("supportsOpenStatementsAcrossRollback");
  979:     rows[67].add(new Boolean(d.supportsOpenStatementsAcrossRollback()));
  980:     //boolean supportsOpenStatementsAcrossRollback() throws SQLException;
  981:     rows[68].add("doesMaxRowSizeIncludeBlobs");
  982:     rows[68].add(new Boolean(d.doesMaxRowSizeIncludeBlobs()));
  983:     //boolean doesMaxRowSizeIncludeBlobs() throws SQLException;
  984:     rows[69].add("supportsTransactions");
  985:     rows[69].add(new Boolean(d.supportsTransactions()));
  986:     //boolean supportsTransactions() throws SQLException;
  987:     rows[70].add("supportsTransactionIsolationLevel");
  988:     rows[70].add(new Boolean(d.supportsTransactionIsolationLevel(1)));
  989:     //boolean supportsTransactionIsolationLevel(int level) throws SQLException;
  990:     rows[71].add("supportsDataDefinitionAndDataManipulationTransactions");
  991:     rows[71].add(new Boolean(d.supportsDataDefinitionAndDataManipulationTransactions()));
  992:     //boolean supportsDataDefinitionAndDataManipulationTransactions() throws SQLException;
  993:     rows[72].add("supportsDataManipulationTransactionsOnly");
  994:     rows[72].add(new Boolean(d.supportsDataManipulationTransactionsOnly()));
  995:     //boolean supportsDataManipulationTransactionsOnly() throws SQLException;
  996:     rows[73].add("dataDefinitionCausesTransactionCommit");
  997:     rows[73].add(new Boolean(d.dataDefinitionCausesTransactionCommit()));
  998:     //boolean dataDefinitionCausesTransactionCommit() throws SQLException;
  999:     rows[74].add("dataDefinitionIgnoredInTransactions");
 1000:     rows[74].add(new Boolean(d.dataDefinitionIgnoredInTransactions()));
 1001:     //boolean dataDefinitionIgnoredInTransactions() throws SQLException;
 1002:     rows[75].add("getMaxBinaryLiteralLength");
 1003:     rows[75].add(new Integer(d.getMaxBinaryLiteralLength()));
 1004:     // int getMaxBinaryLiteralLength() throws SQLException;
 1005:     rows[76].add("getMaxCharLiteralLength");
 1006:     rows[76].add(new Integer(d.getMaxCharLiteralLength()));
 1007:     //int getMaxCharLiteralLength() throws SQLException;
 1008:     rows[77].add("getMaxColumnNameLength");
 1009:     rows[77].add(new Integer(d.getMaxColumnNameLength()));
 1010:     // int getMaxColumnNameLength() throws SQLException;
 1011:     rows[78].add("getMaxColumnsInGroupBy");
 1012:     rows[78].add(new Integer(d.getMaxColumnsInGroupBy()));
 1013:     //int getMaxColumnsInGroupBy() throws SQLException;
 1014:     rows[79].add("getMaxColumnsInIndex");
 1015:     rows[79].add(new Integer(d.getMaxColumnsInIndex()));
 1016:     //int getMaxColumnsInIndex() throws SQLException;
 1017:     rows[80].add("getMaxColumnsInOrderBy");
 1018:     rows[80].add(new Integer(d.getMaxColumnsInOrderBy()));
 1019:     //int getMaxColumnsInOrderBy() throws SQLException;
 1020:     rows[81].add("getMaxColumnsInSelect");
 1021:     rows[81].add(new Integer(d.getMaxColumnsInSelect()));
 1022:     //int getMaxColumnsInSelect() throws SQLException;
 1023:     rows[82].add("getMaxColumnsInTable");
 1024:     rows[82].add(new Integer(d.getMaxColumnsInTable()));
 1025:     //int getMaxColumnsInTable() throws SQLException;
 1026:     rows[83].add("getMaxConnections");
 1027:     rows[83].add(new Integer(d.getMaxConnections()));
 1028:     //int getMaxConnections() throws SQLException;
 1029:     rows[84].add("getMaxCursorNameLength");
 1030:     rows[84].add(new Integer(d.getMaxCursorNameLength()));
 1031:     //    int getMaxCursorNameLength() throws SQLException;
 1032:     rows[85].add("getMaxIndexLength");
 1033:     rows[85].add(new Integer(d.getMaxIndexLength()));
 1034:     //int getMaxIndexLength() throws SQLException;
 1035:     rows[86].add("getMaxSchemaNameLength");
 1036:     rows[86].add(new Integer(d.getMaxSchemaNameLength()));
 1037:     //int getMaxSchemaNameLength() throws SQLException;
 1038:     rows[87].add("getMaxProcedureNameLength");
 1039:     rows[87].add(new Integer(d.getMaxProcedureNameLength()));
 1040:     //int getMaxProcedureNameLength() throws SQLException;
 1041:     rows[88].add("getMaxCatalogNameLength");
 1042:     rows[88].add(new Integer(d.getMaxCatalogNameLength()));
 1043:     //int getMaxCatalogNameLength() throws SQLException;
 1044:     rows[89].add("getMaxRowSize");
 1045:     rows[89].add(new Integer(d.getMaxRowSize()));
 1046:     //int getMaxRowSize() throws SQLException;
 1047:     rows[90].add("getMaxStatementLength");
 1048:     rows[90].add(new Integer(d.getMaxStatementLength()));
 1049:     //int getMaxStatementLength() throws SQLException;
 1050:     rows[91].add("getMaxStatements");
 1051:     rows[91].add(new Integer(d.getMaxStatements()));
 1052:     //int getMaxStatements() throws SQLException;
 1053:     rows[92].add("getMaxTableNameLength");
 1054:     rows[92].add(new Integer(d.getMaxTableNameLength()));
 1055:     //int getMaxTableNameLength() throws SQLException;
 1056:     rows[93].add("getMaxTablesInSelect");
 1057:     rows[93].add(new Integer(d.getMaxTablesInSelect()));
 1058:     //int getMaxTablesInSelect() throws SQLException;
 1059:     rows[94].add("getMaxUserNameLength");
 1060:     rows[94].add(new Integer(d.getMaxUserNameLength()));
 1061:     // int getMaxUserNameLength() throws SQLException;
 1062:     rows[95].add("getDefaultTransactionIsolation");
 1063:     rows[95].add(new Integer(d.getDefaultTransactionIsolation()));
 1064:     //int getDefaultTransactionIsolation() throws SQLException;
 1065: 
 1066:     rows[96].add("getDatabaseProductName");
 1067:     rows[96].add(d.getDatabaseProductName());
 1068:     // String getDatabaseProductName() throws SQLException;
 1069:     rows[97].add("getDatabaseProductVersion");
 1070:     rows[97].add(d.getDatabaseProductVersion());
 1071:     //String getDatabaseProductVersion() throws SQLException;
 1072: 
 1073:     rows[98].add("getURL");
 1074:     rows[98].add(d.getURL());
 1075:     //String getURL() throws SQLException;
 1076:     rows[99].add("getUserName");
 1077:     rows[99].add(d.getUserName());
 1078:     //String getUserName() throws SQLException;
 1079:     rows[100].add("getDriverName");
 1080:     rows[100].add(d.getDriverName());
 1081:     //    String getDriverName() throws SQLException;
 1082:     rows[101].add("getIdentifierQuoteString");
 1083:     rows[101].add(d.getIdentifierQuoteString());
 1084:     //String getIdentifierQuoteString() throws SQLException;
 1085: 
 1086:     rows[102].add("getDriverVersion");
 1087:     rows[102].add(d.getDriverVersion());
 1088:     //String getDriverVersion() throws SQLException;
 1089:     rows[103].add("getDriverMajorVersion");
 1090:     rows[103].add(new Integer(d.getDriverMajorVersion()));
 1091:     //int getDriverMajorVersion();
 1092:     rows[104].add("getDriverMinorVersion");
 1093:     rows[104].add(new Integer(d.getDriverMinorVersion()));
 1094:     //int getDriverMinorVersion();
 1095:     rows[105].add("getSQLKeywords");
 1096:     rows[105].add(d.getSQLKeywords());
 1097:     //String getSQLKeywords() throws SQLException;
 1098:     rows[106].add("getNumericFunctions");
 1099:     rows[106].add(d.getNumericFunctions());
 1100:     //String getNumericFunctions() throws SQLException;
 1101:     rows[107].add("getStringFunctions");
 1102:     rows[107].add(d.getStringFunctions());
 1103:     // String getStringFunctions() throws SQLException;
 1104:     rows[108].add("getSystemFunctions");
 1105:     rows[108].add(d.getSystemFunctions());
 1106:     //String getSystemFunctions() throws SQLException;
 1107:     rows[109].add("getTimeDateFunctions");
 1108:     rows[109].add(d.getTimeDateFunctions());
 1109:     //String getTimeDateFunctions() throws SQLException;
 1110:     rows[110].add("getSearchStringEscape");
 1111:     rows[110].add(d.getSearchStringEscape());
 1112:     //String getSearchStringEscape() throws SQLException;
 1113:     rows[111].add("getExtraNameCharacters");
 1114:     rows[111].add(d.getExtraNameCharacters());
 1115:     //String getExtraNameCharacters() throws SQLException;
 1116:     rows[112].add("getSchemaTerm");
 1117:     rows[112].add(d.getSchemaTerm());
 1118:     //String getSchemaTerm() throws SQLException;
 1119:     rows[113].add("getProcedureTerm");
 1120:     rows[113].add(d.getProcedureTerm());
 1121:     //String getProcedureTerm() throws SQLException;
 1122:     rows[114].add("getCatalogTerm");
 1123:     rows[114].add(d.getCatalogTerm());
 1124:     // String getCatalogTerm() throws SQLException;
 1125:     rows[115].add("getCatalogSeparator");
 1126:     rows[115].add(d.getCatalogSeparator());
 1127:     //String getCatalogSeparator() throws SQLException;
 1128: 
 1129:     /*
 1130:      boolean supportsResultSetType(int type) throws SQLException;
 1131:     
 1132:      boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException;
 1133:     
 1134:      boolean ownUpdatesAreVisible(int type) throws SQLException;
 1135:     
 1136:      boolean ownDeletesAreVisible(int type) throws SQLException;
 1137:     
 1138:      boolean ownInsertsAreVisible(int type) throws SQLException;
 1139:     
 1140:      boolean othersUpdatesAreVisible(int type) throws SQLException;
 1141:     
 1142:      boolean othersDeletesAreVisible(int type) throws SQLException;
 1143:     
 1144:      boolean othersInsertsAreVisible(int type) throws SQLException;
 1145:      boolean updatesAreDetected(int type) throws SQLException;
 1146:      boolean deletesAreDetected(int type) throws SQLException;
 1147:     
 1148:      boolean insertsAreDetected(int type) throws SQLException;
 1149:     */
 1150:     // not in filemaker
 1151:     // rows[96].add("supportsBatchUpdates");
 1152:     // rows[96].add(new Boolean(d.supportsBatchUpdates()));
 1153:     //boolean supportsBatchUpdates() throws SQLException;
 1154: 
 1155:     /*
 1156:     ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException;
 1157:     
 1158:     ResultSet getProcedureColumns(String catalog, String schemaPattern, String procedureNamePattern, String columnNamePattern) throws SQLException;
 1159:     ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String types[]) throws SQLException;
 1160:     ResultSet getSchemas() throws SQLException;
 1161:     ResultSet getCatalogs() throws SQLException;
 1162:     ResultSet getTableTypes() throws SQLException;
 1163:     ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException;
 1164:     ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException;
 1165:     ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException;
 1166:     ResultSet getExportedKeys(String catalog, String schema, String table) throws SQLException;
 1167:     ResultSet getCrossReference(String primaryCatalog, String primarySchema, String primaryTable, String foreignCatalog, String foreignSchema, String foreignTable) throws SQLException;
 1168:     ResultSet getTypeInfo() throws SQLException;
 1169:     ResultSet getIndexInfo(String catalog, String schema, String table, boolean unique, boolean approximate) throws SQLException;
 1170:     
 1171:     ResultSet getUDTs(String catalog, String schemaPattern, String typeNamePattern, int[] types) throws SQLException;
 1172:     
 1173:     Connection getConnection() throws SQLException;
 1174:     
 1175:     */
 1176:     for (int i = 0; i < rows.length; ++i)
 1177:       cols.add(rows[i]);
 1178:     data[0] = cols;
 1179:     data[1] = columnNames;
 1180:     return data;
 1181:   }
 1182:   public Vector getNextRow() throws Exception
 1183:   {
 1184: 
 1185:     if (result == null)
 1186:       return null;
 1187:     boolean check = false;
 1188:     ResultSet resultSet = result;
 1189:     ResultSetMetaData metaData = resultSet.getMetaData();
 1190:     int columnCount = metaData.getColumnCount();
 1191:     Vector tableData = new Vector();
 1192:     check = resultSet.next();
 1193:     //  System.out.println("hallo check "+check);
 1194:     if (!check)
 1195:       return null;
 1196:     Vector tableRow = new Vector(), m_columnClasses = new Vector();
 1197:     for (int i = 1; i <= columnCount; i++)
 1198:     {
 1199:       // repeating fields and fields from related databases may contain
 1200:       // multliple data values; the data values are stored using
 1201:       // a Vector which is then added to the tableRow
 1202:       //      if (metaData instanceof ResultSetMetaDataExt)
 1203:       if ((metaData instanceof ResultSetMetaDataExt) && (((ResultSetMetaDataExt) metaData).isRelated(i) || ((ResultSetMetaDataExt) metaData).isRepeating(i)))
 1204:       {
 1205:         //System.out.println("Related fields");
 1206:         // retrieve the repeating or related field contents as a
 1207:         // com.fmi.jdbc.Array via the ResultSet.getObject method
 1208:         com.fmi.jdbc.Array array = (com.fmi.jdbc.Array) resultSet.getObject(i);
 1209:         //            create a Vector for storing all of the data values
 1210:         ArrayList columnData = new ArrayList();
 1211:         try
 1212:         {
 1213: 
 1214:           // call the Array.getStringArray method since the data will
 1215:           // only be displayed
 1216:           Object[] fieldData = (Object[]) array.getArray();
 1217: 
 1218:           if (fieldData != null)
 1219:           {
 1220:             // add each value to the Vector
 1221:             for (int j = 0; j < fieldData.length; j++)
 1222:             {
 1223:               if (fieldData[j] != null)
 1224:                 columnData.add(fieldData[j]);
 1225:             }
 1226:           }
 1227:         } catch (Exception e)
 1228:         {
 1229:           //System.out.println(e);
 1230:         }
 1231: 
 1232:         if (columnData.isEmpty())
 1233:           tableRow.add(null);
 1234:         else
 1235:           tableRow.addElement(columnData);
 1236:         //System.out.println(columnData);
 1237:         //System.out.println("Related fields"+columnData.size()+" "+tableRow.size());
 1238: 
 1239:         // m_columnClasses.addElement(java.util.Vector.class);
 1240:       } else if (metaData.getColumnType(i) == Types.LONGVARBINARY)
 1241:       {
 1242:         // use the ResultSet.getObject method for retrieving images
 1243:         // from FileMaker Pro container fields; the ResultSet.getObject
 1244:         // method returns a java.awt.Image object for FileMaker Pro
 1245:         // container fields
 1246: 
 1247:         try
 1248:         {
 1249:           tableRow.addElement(resultSet.getObject(i));
 1250:         } catch (Exception e)
 1251:         {
 1252:           // TODO Auto-generated catch block
 1253:           // e.printStackTrace();
 1254:           tableRow.addElement(null);
 1255:         }
 1256:         //    m_columnClasses.addElement(java.awt.Image.class);
 1257:       } else if (metaData.getColumnType(i) == Types.TIME)
 1258:       {
 1259:         // use the ResultSet.getObject method for retieving images
 1260:         // from FileMaker Pro container fields; the ResultSet.getObject
 1261:         // method returns a java.awt.Image object for FileMaker Pro
 1262:         // container fields
 1263:         try
 1264:         {
 1265:           tableRow.addElement(resultSet.getTime(i).toString());
 1266:           //    m_columnClasses.addElement(java.sql.Time.class);
 1267:         } catch (Exception e)
 1268:         {
 1269: 
 1270:           String value = resultSet.getString(i);
 1271:           if (value != null)
 1272:           {
 1273:             //System.out.println("SQLTime new "+Time.valueOf("17:00:00").toString());
 1274:             int index = 0;
 1275:             for (int j = 0; j < value.length(); ++j)
 1276:             {
 1277:               if (!Character.isLetter(value.charAt(j)))
 1278:                 index = j + 1;
 1279:               else
 1280:                 break;
 1281:             }
 1282: 
 1283:             tableRow.addElement(value.substring(0, index));
 1284:             //m_columnClasses.addElement(java.sql.Time.class);
 1285:           } else
 1286:             tableRow.add(null);
 1287:           //  m_columnClasses.addElement(String.class);
 1288:         } // to catch
 1289: 
 1290:       } else if (metaData.getColumnType(i) == Types.INTEGER)
 1291:         {
 1292:           // use the ResultSet.getObject method for retieving images
 1293:           // from FileMaker Pro container fields; the ResultSet.getObject
 1294:           // method returns a java.awt.Image object for FileMaker Pro
 1295:           // container fields
 1296: 
 1297:           tableRow.addElement(new Integer(resultSet.getInt(i)));
 1298:           //  m_columnClasses.addElement(java.sql.Date.class);
 1299:         } else if (metaData.getColumnType(i) == Types.DATE)
 1300:         {
 1301:         // use the ResultSet.getObject method for retieving images
 1302:         // from FileMaker Pro container fields; the ResultSet.getObject
 1303:         // method returns a java.awt.Image object for FileMaker Pro
 1304:         // container fields
 1305:           try
 1306:           {
 1307:             tableRow.addElement(resultSet.getDate(i));
 1308: 
 1309:           } catch (Exception e)
 1310:           {
 1311:             // work around for parse bug in FM JDBC Driver 
 1312:             // for dates of format dd-mm-yyyy
 1313:             String date=resultSet.getString(i);
 1314:             date=date.replace('-','.');
 1315:             java.text.DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,Locale.GERMAN);
 1316:             java.util.Date d= dateFormat.parse(date);
 1317:            // Calendar cal=Calendar.getInstance(Locale.GERMAN);
 1318:            // cal.setTime(d);
 1319:            // date=(cal.get(Calendar.YEAR))+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DATE);
 1320:             tableRow.addElement(new java.sql.Date(d.getTime()));
 1321:              System.out.println("Date "+date);      
 1322:           }
 1323:         //  m_columnClasses.addElement(java.sql.Date.class);
 1324:       } else if (metaData.getColumnTypeName(i) == "NUMBER")
 1325:       {
 1326:         // use the ResultSet.getObject method for retieving images
 1327:         // from FileMaker Pro container fields; the ResultSet.getObject
 1328:         // method returns a java.awt.Image object for FileMaker Pro
 1329:         // container fields
 1330:         try
 1331:         {
 1332:           tableRow.addElement(new Double(resultSet.getDouble(i)));
 1333:           // m_columnClasses.addElement(Integer.class);
 1334: 
 1335:         } catch (Exception e)
 1336:         {
 1337: 
 1338:           StringBuffer number = new StringBuffer();
 1339:           String value = resultSet.getString(i);
 1340:           System.out.println(value);
 1341:           for (int c = 0; c < value.length(); ++c)
 1342:           {
 1343:             if (Character.isDigit(value.charAt(c)))
 1344:             {
 1345:               number.append(value.charAt(c));
 1346:             }
 1347:           }
 1348:           if (number.length() > 0)
 1349:           {
 1350:             tableRow.addElement(null);
 1351:             //   m_columnClasses.addElement(Integer.class);
 1352:           } else
 1353:             tableRow.addElement(null);
 1354:         }
 1355:       } else
 1356:       {
 1357:         // all other field values are retrieved as strings and
 1358:         // added to the tableRow Vector
 1359:         //   System.out.println("row "+resultSet.getString(i));
 1360:         try
 1361:         {
 1362:           byte[] b = null;
 1363:           if (metaData instanceof ResultSetMetaDataExt)
 1364:             b = resultSet.getBytes(i);
 1365:           String utf8 = null;
 1366:           utf8 = (b == null) ? null : new String(b);
 1367:           if (metaData instanceof ResultSetMetaDataExt)
 1368:             tableRow.addElement((b != null) ? new String(utf8.getBytes()) : null);
 1369:           else
 1370:           {
 1371:             if(url.toLowerCase().indexOf("odbc")>=0)
 1372:            {
 1373:                byte[] val = resultSet.getBytes(i);
 1374:              for(int j=0;j<val.length;++j)
 1375:              System.out.println(Integer.toHexString(val[j]));
 1376:              tableRow.addElement((val==null) ? null:new String(val));
 1377:       
 1378:            } else
 1379:             //  byte[] val = resultSet.getBytes(i);
 1380:             tableRow.add(resultSet.getString(i));
 1381:             //tableRow.addElement((val==null) ? null:new String(val,"UTF-8"));
 1382:           }
 1383:         } catch (Exception e)
 1384:         {
 1385:           System.out.println("Hey I Got an error" + e);
 1386:           e.printStackTrace();
 1387:         }
 1388:         // m_columnClasses.addElement(java.lang.String.class);
 1389:       }
 1390:     }
 1391:     //  tableData.addElement(tableRow);
 1392:     if (check)
 1393:       return tableRow;
 1394:     else
 1395:       return null;
 1396:   }
 1397:   class ConnectionPool
 1398:   {
 1399:     String user = "", passwd = "", url = "";
 1400:     Connection con;
 1401:     public ConnectionPool(String url, String user, String passwd, Connection con)
 1402:     {
 1403:       this.con = con;
 1404:       this.user = user;
 1405:       this.passwd = passwd;
 1406:       this.url = url;
 1407:     }
 1408: 
 1409:   }
 1410:   public String getQC()
 1411:   {
 1412:     // if (connection == null)
 1413:     // return "";
 1414: 
 1415:     // check if connection null if null try to get one
 1416:     if (connection == null)
 1417:       try
 1418:       {
 1419:         getConnection();
 1420:       } catch (Exception e)
 1421:       {
 1422:         if (FM2SQL.debug)
 1423:           System.out.println("cannot get a connection");
 1424:       }
 1425:     if (connection == null)
 1426:     {
 1427:       if (url.toLowerCase().indexOf("fmpro") >= 0 || url.toLowerCase().indexOf("postgres") >= 0)
 1428:         quoteChar = "\"";
 1429:       else if (url.toLowerCase().indexOf("mysql") >= 0)
 1430:         quoteChar = "`";
 1431:     }
 1432:     if (quoteChar == null)
 1433:       quoteChar = "\""; // needed for postgres
 1434:     return quoteChar;
 1435:   }
 1436:   public int getRowCount(String query) throws SQLException
 1437:   {
 1438:     String table = query.substring(query.indexOf("from") + 4).trim();
 1439:     int index = table.indexOf(" ");
 1440:     table = table.substring(0, (index >= 0) ? index : table.length());
 1441:     System.out.println(table);
 1442:     Statement stm = null;
 1443: 
 1444:     if (metaData instanceof ResultSetMetaDataExt)
 1445:       return 1000;
 1446:     if (!connection.isClosed())
 1447:       stm = connection.createStatement();
 1448:     stm.setMaxRows(1);
 1449:     ResultSet resultSet = stm.executeQuery("select count(*) from " + table);
 1450:     resultSet.next();
 1451:     return resultSet.getInt(1);
 1452:   }
 1453: 	public TreeSet getIDVector(String id,String table,String query,int numHits) throws Exception
 1454: 	{
 1455: 	  TreeSet t= new TreeSet();
 1456: 	  getConnection();
 1457: 	  ResultSet result = this.result;
 1458: 	  String subQuery = query.substring(query.lastIndexOf(table)+table.length()+1);
 1459:     System.out.println("subQuery "+subQuery);
 1460:     makeQuery("select "+id+" from "+getQC()+table+getQC()+subQuery,numHits );
 1461: 	  while(true)
 1462: 	  {
 1463:       Vector vec = getNextRow();
 1464:       if (vec == null)
 1465:         break;
 1466:       t.add(vec.get(0));
 1467: 	  }
 1468: 		this.result=result;
 1469:     metaData = (this.result==null) ?null:this.result.getMetaData();
 1470:     return t;
 1471: 	}
 1472: 	
 1473: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>