--- FM2SQL/Attic/DBBean.java 2004/05/06 11:39:42 1.18 +++ FM2SQL/Attic/DBBean.java 2004/06/22 10:33:53 1.24 @@ -16,6 +16,7 @@ import java.sql.*; import java.text.DateFormat; +import java.text.ParseException; import java.util.*; import com.fmi.jdbc.*; @@ -65,6 +66,7 @@ import com.fmi.jdbc.*; */ public class DBBean { + private boolean useNormanToUnicodeMapper=false; Connection connection; String url = ""; DatabaseMetaData dbMetaData; @@ -85,6 +87,7 @@ public class DBBean DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance()); DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.Driver").newInstance()); DriverManager.registerDriver((Driver) Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance()); + DriverManager.registerDriver((Driver) Class.forName("acs.jdbc.Driver").newInstance()); // wait a maximum of 10 seconds when attempting to establish a connection DriverManager.setLoginTimeout(10); @@ -187,11 +190,11 @@ public class DBBean { // setup the properties java.util.Properties prop = new java.util.Properties(); - prop.put("charSet", "Cp1256"); + // prop.put("charSet", "MacCentralEurope"); prop.put("user", user); prop.put("password", passwd); System.out.println("url "+url); - if(url.indexOf("odbc")>=0) + if(url.indexOf("fmpro")>=0) { // Connect to the database connection = DriverManager.getConnection(url, prop); @@ -376,7 +379,7 @@ public class DBBean * Returns the result for select * from table * with maxHits = 500 default value */ - public Vector[] getQueryData(String table) throws SQLException + public Vector[] getQueryData(String table) throws SQLException,ParseException { return getQueryData("SELECT * from " + quoteChar + table + quoteChar, maxHits); @@ -456,7 +459,7 @@ public class DBBean * Returns the result of the query * or an Vector array of Vectors containing error messages */ - public Vector[] getQueryData(String query, int maxHits) throws SQLException + public Vector[] getQueryData(String query, int maxHits) throws SQLException, ParseException { long timeStart = System.currentTimeMillis(); ResultSet resultSet = null; @@ -585,9 +588,25 @@ public class DBBean // method returns a java.awt.Image object for FileMaker Pro // container fields - tableRow.addElement(resultSet.getDate(i)); - m_columnClasses.addElement(java.sql.Date.class); - } else if (metaData.getColumnTypeName(i) == "NUMBER") + try + { + 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); + } + } else if (metaData.getColumnTypeName(i) == "NUMBER") { // use the ResultSet.getObject method for retieving images // from FileMaker Pro container fields; the ResultSet.getObject @@ -708,11 +727,6 @@ public class DBBean 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)); @@ -721,6 +735,12 @@ public class DBBean result = stm.getResultSet(); // System.out.println(result+" "+stm.getUpdateCount()); metaData = result.getMetaData(); + } catch(Exception e) { + // TODO remove + if(FM2SQL.fmInstance!=null) + FM2SQL.showErrorDialog("Error caught!! \n Query was "+query+" \n","Debug Info"); + } + return result; } /** @@ -1360,10 +1380,42 @@ public class DBBean byte[] b = null; if (metaData instanceof ResultSetMetaDataExt) b = resultSet.getBytes(i); + /* if (b != null) + { + java.io.ByteArrayInputStream stream = (java.io.ByteArrayInputStream) resultSet.getBinaryStream(i); + // System.out.println(" stream "+resultSet.getBinaryStream(i)); + byte[] c = new byte[stream.available()]; + int length = stream.read(c, 0, c.length); + int count = 0; + b = new byte[c.length]; + for (int n = 0; n < length; ++n) + { + + if (c[n] != 0) + { + // System.out.println(c[n]+" "+(int)'?'+" "+(char)c[n]+" "+count+" "+b.length); + b[count++] = c[n]; + } + } + byte[] bCopy = new byte[count]; + System.arraycopy(b, 0, bCopy, 0, count); + b = bCopy; + }*/ String utf8 = null; utf8 = (b == null) ? null : new String(b); if (metaData instanceof ResultSetMetaDataExt) - tableRow.addElement((b != null) ? new String(utf8.getBytes()) : null); + { + String rowElement = ""; + if (b != null) + { + rowElement = resultSet.getString(i); + if(useNormanToUnicodeMapper) + rowElement = Convert.normanToUnicode(rowElement); + tableRow.addElement(rowElement); + + } else + tableRow.addElement(null); + } else { if(url.toLowerCase().indexOf("odbc")>=0) @@ -1380,7 +1432,7 @@ public class DBBean } } catch (Exception e) { - System.out.println("Hey I Got an error" + e); + System.out.println("Hey I got an error" + e); e.printStackTrace(); } // m_columnClasses.addElement(java.lang.String.class); @@ -1467,5 +1519,20 @@ public class DBBean metaData = (this.result==null) ?null:this.result.getMetaData(); return t; } - + /** + * @return + */ + public boolean isUseNormanToUnicodeMapper() + { + return useNormanToUnicodeMapper; + } + + /** + * @param b + */ + public void setUseNormanToUnicodeMapper(boolean b) + { + useNormanToUnicodeMapper = b; + } + } \ No newline at end of file