--- FM2SQL/Attic/DBBean.java 2004/06/22 10:33:53 1.24 +++ FM2SQL/Attic/DBBean.java 2004/06/23 09:59:53 1.25 @@ -13,7 +13,6 @@ * rogo */ - import java.sql.*; import java.text.DateFormat; import java.text.ParseException; @@ -66,7 +65,7 @@ import com.fmi.jdbc.*; */ public class DBBean { - private boolean useNormanToUnicodeMapper=false; + private boolean useNormanToUnicodeMapper = false; Connection connection; String url = ""; DatabaseMetaData dbMetaData; @@ -88,7 +87,7 @@ public class DBBean 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); } catch (Exception e) @@ -155,8 +154,8 @@ public class DBBean ConnectionPool conPool = (ConnectionPool) connectionPool.get(url); if (conPool == null) { - createConnection(); - + createConnection(); + } else { if (!conPool.user.equals(user) || !conPool.passwd.equals(passwd)) @@ -170,15 +169,15 @@ public class DBBean if (connection.isClosed()) { System.out.println("Made new connection!!!"); - createConnection(); - - // 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) createConnection(); - //connection = DriverManager.getConnection(url, user, passwd); + //connection = DriverManager.getConnection(url, user, passwd); dbMetaData = connection.getMetaData(); quoteChar = dbMetaData.getIdentifierQuoteString(); if (quoteChar == null) @@ -188,22 +187,21 @@ public class DBBean } private void createConnection() throws SQLException { - // setup the properties - java.util.Properties prop = new java.util.Properties(); - // prop.put("charSet", "MacCentralEurope"); - prop.put("user", user); - prop.put("password", passwd); - System.out.println("url "+url); - if(url.indexOf("fmpro")>=0) + // setup the properties + java.util.Properties prop = new java.util.Properties(); + // prop.put("charSet", "MacCentralEurope"); + prop.put("user", user); + prop.put("password", passwd); + System.out.println("url " + url); + if (url.indexOf("fmpro") >= 0) { - // Connect to the database + // Connect to the database connection = DriverManager.getConnection(url, prop); System.out.println("odbc with properties inited"); - } - else - connection = DriverManager.getConnection(url, user, passwd); + } 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 @@ -214,7 +212,7 @@ public class DBBean this.url = url; if (url != "") //connection = DriverManager.getConnection(url, user, passwd); - createConnection(); + createConnection(); dbMetaData = connection.getMetaData(); quoteChar = dbMetaData.getIdentifierQuoteString(); if (quoteChar == null) @@ -230,8 +228,8 @@ public class DBBean this.passwd = passwd; this.url = url; if (url != "") - createConnection(); - // connection = DriverManager.getConnection(url, user, passwd); + createConnection(); + // connection = DriverManager.getConnection(url, user, passwd); dbMetaData = connection.getMetaData(); quoteChar = dbMetaData.getIdentifierQuoteString(); if (quoteChar == null) @@ -379,7 +377,7 @@ public class DBBean * Returns the result for select * from table * with maxHits = 500 default value */ - public Vector[] getQueryData(String table) throws SQLException,ParseException + public Vector[] getQueryData(String table) throws SQLException, ParseException { return getQueryData("SELECT * from " + quoteChar + table + quoteChar, maxHits); @@ -588,25 +586,25 @@ public class DBBean // method returns a java.awt.Image object for FileMaker Pro // container fields - 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") + 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 @@ -705,18 +703,18 @@ public class DBBean { result = null; Statement stm = null; - + // System.out.println("Query " + query); if (!connection.isClosed()) stm = connection.createStatement(); - else { - - + else + { + try { connection = getConnection(); - stm= connection.createStatement(); + stm = connection.createStatement(); } catch (Exception e) { // TODO Auto-generated catch block @@ -725,22 +723,24 @@ public class DBBean } stm.setMaxRows(numberOfHits); long time = System.currentTimeMillis(); - try { - stm.execute(query); - 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(); - } catch(Exception e) { + try + { + stm.execute(query); + 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(); + } catch (Exception e) + { // TODO remove - if(FM2SQL.fmInstance!=null) - FM2SQL.showErrorDialog("Error caught!! \n Query was "+query+" \n","Debug Info"); - } - + if (FM2SQL.fmInstance != null) + FM2SQL.showErrorDialog("Error caught!! \n Query was " + query + " \n", "Debug Info"); + } + return result; } /** @@ -1306,38 +1306,38 @@ public class DBBean } // to catch } 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 + { + // 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) - { + 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)); + 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); - } + } 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") { @@ -1380,27 +1380,27 @@ 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; - }*/ + /* 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) @@ -1409,25 +1409,24 @@ public class DBBean if (b != null) { rowElement = resultSet.getString(i); - if(useNormanToUnicodeMapper) - rowElement = Convert.normanToUnicode(rowElement); + if (useNormanToUnicodeMapper) + rowElement = Convert.normanToUnicode(rowElement); tableRow.addElement(rowElement); } else tableRow.addElement(null); - } - else + } else { - if(url.toLowerCase().indexOf("odbc")>=0) - { - byte[] val = resultSet.getBytes(i); - for(int j=0;j= 0) + { + byte[] val = resultSet.getBytes(i); + for (int j = 0; j < val.length; ++j) + System.out.println(Integer.toHexString(val[j])); + tableRow.addElement((val == null) ? null : new String(val)); + + } else + // byte[] val = resultSet.getBytes(i); + tableRow.add(resultSet.getString(i)); //tableRow.addElement((val==null) ? null:new String(val,"UTF-8")); } } catch (Exception e) @@ -1500,25 +1499,25 @@ public class DBBean resultSet.next(); return resultSet.getInt(1); } - public TreeSet getIDVector(String id,String table,String query,int numHits) throws Exception - { - TreeSet t= new TreeSet(); - getConnection(); - ResultSet result = this.result; - String subQuery = query.substring(query.lastIndexOf(table)+table.length()+1); - System.out.println("subQuery "+subQuery); - makeQuery("select "+id+" from "+getQC()+table+getQC()+subQuery,numHits ); - while(true) - { + public TreeSet getIDVector(String id, String table, String query, int numHits) throws Exception + { + TreeSet t = new TreeSet(); + getConnection(); + ResultSet result = this.result; + String subQuery = query.substring(query.lastIndexOf(table) + table.length() + 1); + System.out.println("subQuery " + subQuery); + makeQuery("select " + id + " from " + getQC() + table + getQC() + subQuery, numHits); + while (true) + { Vector vec = getNextRow(); if (vec == null) break; t.add(vec.get(0)); - } - this.result=result; - metaData = (this.result==null) ?null:this.result.getMetaData(); + } + this.result = result; + metaData = (this.result == null) ? null : this.result.getMetaData(); return t; - } + } /** * @return */