version 1.10, 2004/03/12 11:48:57
|
version 1.30, 2004/08/04 11:53:08
|
Line 1
|
Line 1
|
|
/* |
|
* 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.sql.*; |
|
import java.text.DateFormat; |
|
import java.text.ParseException; |
import java.util.*; |
import java.util.*; |
|
|
import com.fmi.jdbc.*; |
import com.fmi.jdbc.*; |
|
|
/** |
/** |
Line 49 import com.fmi.jdbc.*;
|
Line 65 import com.fmi.jdbc.*;
|
*/ |
*/ |
public class DBBean |
public class DBBean |
{ |
{ |
|
private boolean useNormanToUnicodeMapper = false; |
Connection connection; |
Connection connection; |
String url = ""; |
String url = ""; |
DatabaseMetaData dbMetaData; |
DatabaseMetaData dbMetaData; |
Line 69 public class DBBean
|
Line 86 public class DBBean
|
DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance()); |
DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance()); |
DriverManager.registerDriver((Driver) Class.forName("com.mysql.jdbc.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("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 |
// wait a maximum of 10 seconds when attempting to establish a connection |
DriverManager.setLoginTimeout(10); |
DriverManager.setLoginTimeout(10); |
Line 136 public class DBBean
|
Line 154 public class DBBean
|
ConnectionPool conPool = (ConnectionPool) connectionPool.get(url); |
ConnectionPool conPool = (ConnectionPool) connectionPool.get(url); |
if (conPool == null) |
if (conPool == null) |
{ |
{ |
connection = DriverManager.getConnection(url, user, passwd); |
createConnection(); |
connectionPool.put(url, new ConnectionPool(url, user, passwd, connection)); |
|
} else |
} else |
{ |
{ |
if (!conPool.user.equals(user) || !conPool.passwd.equals(passwd)) |
if (!conPool.user.equals(user) || !conPool.passwd.equals(passwd)) |
Line 151 public class DBBean
|
Line 169 public class DBBean
|
if (connection.isClosed()) |
if (connection.isClosed()) |
{ |
{ |
System.out.println("Made new connection!!!"); |
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; |
conPool.con = connection; |
} |
} |
} |
} |
if (url != "" && connection == null) |
if (url != "" && connection == null) |
connection = DriverManager.getConnection(url, user, passwd); |
createConnection(); |
|
//connection = DriverManager.getConnection(url, user, passwd); |
dbMetaData = connection.getMetaData(); |
dbMetaData = connection.getMetaData(); |
quoteChar = dbMetaData.getIdentifierQuoteString(); |
quoteChar = dbMetaData.getIdentifierQuoteString(); |
if (quoteChar == null) |
if (quoteChar == null) |
Line 164 public class DBBean
|
Line 185 public class DBBean
|
|
|
return connection; |
return connection; |
} |
} |
|
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) |
|
{ |
|
// 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 |
* sets the connection of this DBBean to the database specified in the url |
* property |
* property |
Line 172 public class DBBean
|
Line 211 public class DBBean
|
{ |
{ |
this.url = url; |
this.url = url; |
if (url != "") |
if (url != "") |
connection = DriverManager.getConnection(url, user, passwd); |
//connection = DriverManager.getConnection(url, user, passwd); |
|
createConnection(); |
dbMetaData = connection.getMetaData(); |
dbMetaData = connection.getMetaData(); |
quoteChar = dbMetaData.getIdentifierQuoteString(); |
quoteChar = dbMetaData.getIdentifierQuoteString(); |
if (quoteChar == null) |
if (quoteChar == null) |
Line 188 public class DBBean
|
Line 228 public class DBBean
|
this.passwd = passwd; |
this.passwd = passwd; |
this.url = url; |
this.url = url; |
if (url != "") |
if (url != "") |
connection = DriverManager.getConnection(url, user, passwd); |
createConnection(); |
|
// connection = DriverManager.getConnection(url, user, passwd); |
dbMetaData = connection.getMetaData(); |
dbMetaData = connection.getMetaData(); |
quoteChar = dbMetaData.getIdentifierQuoteString(); |
quoteChar = dbMetaData.getIdentifierQuoteString(); |
if (quoteChar == null) |
if (quoteChar == null) |
Line 336 public class DBBean
|
Line 377 public class DBBean
|
* Returns the result for select * from table |
* Returns the result for select * from table |
* with maxHits = 500 default value |
* 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); |
return getQueryData("SELECT * from " + quoteChar + table + quoteChar, maxHits); |
Line 416 public class DBBean
|
Line 457 public class DBBean
|
* Returns the result of the query |
* Returns the result of the query |
* or an Vector array of Vectors containing error messages |
* 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(); |
long timeStart = System.currentTimeMillis(); |
ResultSet resultSet = null; |
ResultSet resultSet = null; |
Line 545 public class DBBean
|
Line 586 public class DBBean
|
// method returns a java.awt.Image object for FileMaker Pro |
// method returns a java.awt.Image object for FileMaker Pro |
// container fields |
// container fields |
|
|
|
try |
|
{ |
tableRow.addElement(resultSet.getDate(i)); |
tableRow.addElement(resultSet.getDate(i)); |
m_columnClasses.addElement(java.sql.Date.class); |
|
|
} 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") |
} else if (metaData.getColumnTypeName(i) == "NUMBER") |
{ |
{ |
// use the ResultSet.getObject method for retieving images |
// use the ResultSet.getObject method for retieving images |
Line 555 public class DBBean
|
Line 612 public class DBBean
|
// container fields |
// container fields |
try |
try |
{ |
{ |
tableRow.addElement(new Integer(resultSet.getInt(i))); |
Double value =new Double(resultSet.getDouble(i)); |
m_columnClasses.addElement(Integer.class); |
|
|
|
|
// tableRow.addElement(new Double(resultSet.getDouble(i))); |
|
String tVal =value.toString(); |
|
tVal = tVal.substring(tVal.indexOf('.')+1); |
|
boolean checkMe = tVal.length()==1&&tVal.equals("0"); |
|
// System.out.println("check was"+checkMe+" "+tVal); |
|
if(checkMe) |
|
tableRow.addElement(new Integer(value.intValue())); |
|
else |
|
tableRow.addElement(value); |
|
|
} catch (Exception e) |
} catch (Exception e) |
{ |
{ |
Line 574 public class DBBean
|
Line 641 public class DBBean
|
if (number.length() > 0) |
if (number.length() > 0) |
{ |
{ |
tableRow.addElement(null); |
tableRow.addElement(null); |
m_columnClasses.addElement(Integer.class); |
m_columnClasses.addElement(Double.class); |
} else |
} else |
tableRow.addElement(null); |
tableRow.addElement(null); |
} |
} |
Line 651 public class DBBean
|
Line 718 public class DBBean
|
|
|
if (!connection.isClosed()) |
if (!connection.isClosed()) |
stm = connection.createStatement(); |
stm = connection.createStatement(); |
|
else |
|
{ |
|
|
|
try |
|
{ |
|
connection = getConnection(); |
|
stm = connection.createStatement(); |
|
} catch (Exception e) |
|
{ |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} |
|
} |
stm.setMaxRows(numberOfHits); |
stm.setMaxRows(numberOfHits); |
long time = System.currentTimeMillis(); |
long time = System.currentTimeMillis(); |
|
try |
|
{ |
stm.execute(query); |
stm.execute(query); |
long time2 = System.currentTimeMillis(); |
long time2 = System.currentTimeMillis(); |
|
|
Line 662 public class DBBean
|
Line 744 public class DBBean
|
result = stm.getResultSet(); |
result = stm.getResultSet(); |
// System.out.println(result+" "+stm.getUpdateCount()); |
// System.out.println(result+" "+stm.getUpdateCount()); |
metaData = result.getMetaData(); |
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; |
return result; |
} |
} |
/** |
/** |
Line 1241 public class DBBean
|
Line 1330 public class DBBean
|
// from FileMaker Pro container fields; the ResultSet.getObject |
// from FileMaker Pro container fields; the ResultSet.getObject |
// method returns a java.awt.Image object for FileMaker Pro |
// method returns a java.awt.Image object for FileMaker Pro |
// container fields |
// 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); |
// m_columnClasses.addElement(java.sql.Date.class); |
} else if (metaData.getColumnTypeName(i) == "NUMBER") |
} else if (metaData.getColumnTypeName(i) == "NUMBER") |
{ |
{ |
Line 1252 public class DBBean
|
Line 1357 public class DBBean
|
// container fields |
// container fields |
try |
try |
{ |
{ |
tableRow.addElement(new Integer(resultSet.getInt(i))); |
Double value =new Double(resultSet.getDouble(i)); |
|
|
|
|
|
// tableRow.addElement(new Double(resultSet.getDouble(i))); |
|
String tVal =value.toString(); |
|
tVal = tVal.substring(tVal.indexOf('.')+1); |
|
boolean checkMe = tVal.length()==1&&tVal.equals("0"); |
|
// System.out.println("check was"+checkMe+" "+tVal); |
|
if(checkMe) |
|
tableRow.addElement(new Integer(value.intValue())); |
|
else |
|
tableRow.addElement(value); |
|
|
// m_columnClasses.addElement(Integer.class); |
// m_columnClasses.addElement(Integer.class); |
|
|
} catch (Exception e) |
} catch (Exception e) |
Line 1285 public class DBBean
|
Line 1402 public class DBBean
|
byte[] b = null; |
byte[] b = null; |
if (metaData instanceof ResultSetMetaDataExt) |
if (metaData instanceof ResultSetMetaDataExt) |
b = resultSet.getBytes(i); |
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; |
String utf8 = null; |
utf8 = (b == null) ? null : new String(b); |
utf8 = (b == null) ? null : new String(b); |
if (metaData instanceof ResultSetMetaDataExt) |
if (metaData instanceof ResultSetMetaDataExt) |
tableRow.addElement((b != null) ? new String(utf8.getBytes()) : null); |
|
else |
|
{ |
{ |
|
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) |
|
{ |
|
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); |
// byte[] val = resultSet.getBytes(i); |
tableRow.add(resultSet.getString(i)); |
tableRow.add(resultSet.getString(i)); |
//tableRow.addElement((val==null) ? null:new String(val,"UTF-8")); |
//tableRow.addElement((val==null) ? null:new String(val,"UTF-8")); |
} |
} |
} catch (Exception e) |
} catch (Exception e) |
{ |
{ |
System.out.println("Hey I Got an error" + e); |
System.out.println("Hey I got an error" + e); |
e.printStackTrace(); |
e.printStackTrace(); |
} |
} |
// m_columnClasses.addElement(java.lang.String.class); |
// m_columnClasses.addElement(java.lang.String.class); |
Line 1372 public class DBBean
|
Line 1528 public class DBBean
|
ResultSet result = this.result; |
ResultSet result = this.result; |
String subQuery = query.substring(query.lastIndexOf(table)+table.length()+1); |
String subQuery = query.substring(query.lastIndexOf(table)+table.length()+1); |
System.out.println("subQuery "+subQuery); |
System.out.println("subQuery "+subQuery); |
makeQuery("select "+id+" from "+getQC()+table+getQC()+subQuery,numHits ); |
makeQuery("select " + id + " from " + getQC() + table + getQC() + subQuery+"order by "+id, numHits); |
while(true) |
while(true) |
{ |
{ |
Vector vec = getNextRow(); |
Vector vec = getNextRow(); |
Line 1384 public class DBBean
|
Line 1540 public class DBBean
|
metaData = (this.result==null) ?null:this.result.getMetaData(); |
metaData = (this.result==null) ?null:this.result.getMetaData(); |
return t; |
return t; |
} |
} |
|
/** |
|
* @return |
|
*/ |
|
public boolean isUseNormanToUnicodeMapper() |
|
{ |
|
return useNormanToUnicodeMapper; |
|
} |
|
|
|
/** |
|
* @param b |
|
*/ |
|
public void setUseNormanToUnicodeMapper(boolean b) |
|
{ |
|
useNormanToUnicodeMapper = b; |
|
} |
|
/** |
|
* |
|
*/ |
|
protected void closeAllConnections() |
|
{ |
|
Enumeration enumeration = connectionPool.elements(); |
|
while(enumeration.hasMoreElements()) |
|
{ |
|
ConnectionPool conPol =(ConnectionPool)enumeration.nextElement(); |
|
try |
|
{ |
|
System.out.println(conPol); |
|
conPol.con.close(); |
|
} catch (SQLException e) |
|
{ |
|
// TODO Auto-generated catch block |
|
e.printStackTrace(); |
|
} |
|
} |
|
|
|
} |
|
|
} |
} |
|
|