--- FM2SQL/Attic/DBBean.java 2003/12/05 11:46:39 1.1 +++ FM2SQL/Attic/DBBean.java 2004/05/06 11:39:42 1.18 @@ -1,7 +1,23 @@ -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; +/* + * DBBean.java -- Class that ecapsulates all database actions + * Filemake to SQL Converter + * Copyright (C) 2004 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. Please read license.txt for the full details. A copy of + * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html You should + * have received a copy of the GNU General Public License along with this + * program; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA Created on 15.09.2003 by + * rogo + */ + + import java.sql.*; +import java.text.DateFormat; import java.util.*; + import com.fmi.jdbc.*; /** @@ -53,6 +69,7 @@ public class DBBean String url = ""; DatabaseMetaData dbMetaData; Vector columnNames; + Vector ids = new Vector(); String user = (System.getProperty("user.name") == null) ? "" : System.getProperty("user.name"); //"postgres"; String passwd = ""; //"3333";//"rogo"; public int maxHits = 10; @@ -66,9 +83,9 @@ public class DBBean { DriverManager.registerDriver(new com.fmi.jdbc.JdbcDriver()); DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance()); - DriverManager.registerDriver((Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance()); DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.Driver").newInstance()); - + DriverManager.registerDriver((Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance()); + // wait a maximum of 10 seconds when attempting to establish a connection DriverManager.setLoginTimeout(10); } catch (Exception e) @@ -135,8 +152,8 @@ public class DBBean ConnectionPool conPool = (ConnectionPool) connectionPool.get(url); if (conPool == null) { - connection = DriverManager.getConnection(url, user, passwd); - connectionPool.put(url, new ConnectionPool(url, user, passwd, connection)); + createConnection(); + } else { if (!conPool.user.equals(user) || !conPool.passwd.equals(passwd)) @@ -150,12 +167,15 @@ public class DBBean if (connection.isClosed()) { System.out.println("Made new connection!!!"); - connection = DriverManager.getConnection(conPool.url, conPool.user, conPool.passwd); + createConnection(); + + // connection = DriverManager.getConnection(conPool.url, conPool.user, conPool.passwd); conPool.con = connection; } } if (url != "" && connection == null) - connection = DriverManager.getConnection(url, user, passwd); + createConnection(); + //connection = DriverManager.getConnection(url, user, passwd); dbMetaData = connection.getMetaData(); quoteChar = dbMetaData.getIdentifierQuoteString(); if (quoteChar == null) @@ -163,6 +183,25 @@ public class DBBean return connection; } + private void createConnection() throws SQLException + { + // setup the properties + java.util.Properties prop = new java.util.Properties(); + prop.put("charSet", "Cp1256"); + prop.put("user", user); + prop.put("password", passwd); + System.out.println("url "+url); + if(url.indexOf("odbc")>=0) + { + // Connect to the database + connection = DriverManager.getConnection(url, prop); + System.out.println("odbc with properties inited"); + } + else + connection = DriverManager.getConnection(url, user, passwd); + connectionPool.put(url, new ConnectionPool(url, user, passwd, connection)); + + } /** * sets the connection of this DBBean to the database specified in the url * property @@ -171,7 +210,8 @@ public class DBBean { this.url = url; if (url != "") - connection = DriverManager.getConnection(url, user, passwd); + //connection = DriverManager.getConnection(url, user, passwd); + createConnection(); dbMetaData = connection.getMetaData(); quoteChar = dbMetaData.getIdentifierQuoteString(); if (quoteChar == null) @@ -187,13 +227,26 @@ public class DBBean this.passwd = passwd; this.url = url; if (url != "") - connection = DriverManager.getConnection(url, user, passwd); + createConnection(); + // connection = DriverManager.getConnection(url, user, passwd); dbMetaData = connection.getMetaData(); quoteChar = dbMetaData.getIdentifierQuoteString(); if (quoteChar == null) quoteChar = "\""; // needed for postgres } + public void setIDVector(Vector ids) + { + this.ids = ids; + } + + /** + * returns a Vector containing the ID Row Name + **/ + public Vector getIDVector() + { + return ids; + } /** * returns a Vector containing the Tablenames or an error message in the Vector */ @@ -542,8 +595,8 @@ public class DBBean // container fields try { - tableRow.addElement(new Integer(resultSet.getInt(i))); - m_columnClasses.addElement(Integer.class); + tableRow.addElement(new Double(resultSet.getDouble(i))); + m_columnClasses.addElement(Double.class); } catch (Exception e) { @@ -561,7 +614,7 @@ public class DBBean if (number.length() > 0) { tableRow.addElement(null); - m_columnClasses.addElement(Integer.class); + m_columnClasses.addElement(Double.class); } else tableRow.addElement(null); } @@ -633,13 +686,40 @@ public class DBBean { result = null; Statement stm = null; + // System.out.println("Query " + query); if (!connection.isClosed()) stm = connection.createStatement(); + else { + + + try + { + connection = getConnection(); + stm= connection.createStatement(); + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } stm.setMaxRows(numberOfHits); - result = stm.executeQuery(query); - // System.out.println(result+" "+stm.getMoreResults()); + long time = System.currentTimeMillis(); + try { + stm.execute(query); + } catch(Exception e) { + // TODO remove + // if(FM2SQL.fmInstance!=null) + // FM2SQL.showErrorDialog("Error caught!! \n Query was "+query+" \n","Debug Info"); + } + long time2 = System.currentTimeMillis(); + + System.out.println("time to execute "+(time2-time)); + // stm.setMaxRows(numberOfHits); + + result = stm.getResultSet(); + // System.out.println(result+" "+stm.getUpdateCount()); metaData = result.getMetaData(); return result; } @@ -1205,14 +1285,39 @@ public class DBBean // m_columnClasses.addElement(String.class); } // to catch - } else if (metaData.getColumnType(i) == Types.DATE) - { + } else if (metaData.getColumnType(i) == Types.INTEGER) + { + // use the ResultSet.getObject method for retieving images + // from FileMaker Pro container fields; the ResultSet.getObject + // method returns a java.awt.Image object for FileMaker Pro + // container fields + + tableRow.addElement(new Integer(resultSet.getInt(i))); + // m_columnClasses.addElement(java.sql.Date.class); + } else if (metaData.getColumnType(i) == Types.DATE) + { // use the ResultSet.getObject method for retieving images // from FileMaker Pro container fields; the ResultSet.getObject // method returns a java.awt.Image object for FileMaker Pro // container fields + try + { + tableRow.addElement(resultSet.getDate(i)); - tableRow.addElement(resultSet.getDate(i)); + } catch (Exception e) + { + // work around for parse bug in FM JDBC Driver + // for dates of format dd-mm-yyyy + String date=resultSet.getString(i); + date=date.replace('-','.'); + java.text.DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT,Locale.GERMAN); + java.util.Date d= dateFormat.parse(date); + // Calendar cal=Calendar.getInstance(Locale.GERMAN); + // cal.setTime(d); + // date=(cal.get(Calendar.YEAR))+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DATE); + tableRow.addElement(new java.sql.Date(d.getTime())); + System.out.println("Date "+date); + } // m_columnClasses.addElement(java.sql.Date.class); } else if (metaData.getColumnTypeName(i) == "NUMBER") { @@ -1222,7 +1327,7 @@ public class DBBean // container fields try { - tableRow.addElement(new Integer(resultSet.getInt(i))); + tableRow.addElement(new Double(resultSet.getDouble(i))); // m_columnClasses.addElement(Integer.class); } catch (Exception e) @@ -1261,6 +1366,14 @@ public class DBBean tableRow.addElement((b != null) ? new String(utf8.getBytes()) : null); else { + if(url.toLowerCase().indexOf("odbc")>=0) + { + byte[] val = resultSet.getBytes(i); + for(int j=0;j