version 1.1.1.1, 2003/12/05 11:46:39
|
version 1.13, 2004/04/06 10:34:15
|
Line 1
|
Line 1
|
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.sql.*; |
import java.util.*; |
import java.util.*; |
import com.fmi.jdbc.*; |
import com.fmi.jdbc.*; |
Line 53 public class DBBean
|
Line 67 public class DBBean
|
String url = ""; |
String url = ""; |
DatabaseMetaData dbMetaData; |
DatabaseMetaData dbMetaData; |
Vector columnNames; |
Vector columnNames; |
|
Vector ids = new Vector(); |
String user = (System.getProperty("user.name") == null) ? "" : System.getProperty("user.name"); //"postgres"; |
String user = (System.getProperty("user.name") == null) ? "" : System.getProperty("user.name"); //"postgres"; |
String passwd = ""; //"3333";//"rogo"; |
String passwd = ""; //"3333";//"rogo"; |
public int maxHits = 10; |
public int maxHits = 10; |
Line 66 public class DBBean
|
Line 81 public class DBBean
|
{ |
{ |
DriverManager.registerDriver(new com.fmi.jdbc.JdbcDriver()); |
DriverManager.registerDriver(new com.fmi.jdbc.JdbcDriver()); |
DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance()); |
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("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 |
// wait a maximum of 10 seconds when attempting to establish a connection |
DriverManager.setLoginTimeout(10); |
DriverManager.setLoginTimeout(10); |
Line 194 public class DBBean
|
Line 209 public class DBBean
|
quoteChar = "\""; // needed for postgres |
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 |
* returns a Vector containing the Tablenames or an error message in the Vector |
*/ |
*/ |
Line 542 public class DBBean
|
Line 569 public class DBBean
|
// container fields |
// container fields |
try |
try |
{ |
{ |
tableRow.addElement(new Integer(resultSet.getInt(i))); |
tableRow.addElement(new Double(resultSet.getDouble(i))); |
m_columnClasses.addElement(Integer.class); |
m_columnClasses.addElement(Double.class); |
|
|
} catch (Exception e) |
} catch (Exception e) |
{ |
{ |
Line 561 public class DBBean
|
Line 588 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 633 public class DBBean
|
Line 660 public class DBBean
|
{ |
{ |
result = null; |
result = null; |
Statement stm = null; |
Statement stm = null; |
|
|
// System.out.println("Query " + query); |
// System.out.println("Query " + query); |
|
|
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); |
result = stm.executeQuery(query); |
long time = System.currentTimeMillis(); |
// System.out.println(result+" "+stm.getMoreResults()); |
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(); |
metaData = result.getMetaData(); |
return result; |
return result; |
} |
} |
Line 1205 public class DBBean
|
Line 1253 public class DBBean
|
// m_columnClasses.addElement(String.class); |
// m_columnClasses.addElement(String.class); |
} // to catch |
} // 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 |
|
|
|
tableRow.addElement(new Integer(resultSet.getInt(i))); |
|
// m_columnClasses.addElement(java.sql.Date.class); |
} else if (metaData.getColumnType(i) == Types.DATE) |
} else if (metaData.getColumnType(i) == Types.DATE) |
{ |
{ |
// use the ResultSet.getObject method for retieving images |
// use the ResultSet.getObject method for retieving images |
Line 1222 public class DBBean
|
Line 1279 public class DBBean
|
// container fields |
// container fields |
try |
try |
{ |
{ |
tableRow.addElement(new Integer(resultSet.getInt(i))); |
tableRow.addElement(new Double(resultSet.getDouble(i))); |
// m_columnClasses.addElement(Integer.class); |
// m_columnClasses.addElement(Integer.class); |
|
|
} catch (Exception e) |
} catch (Exception e) |
Line 1335 public class DBBean
|
Line 1392 public class DBBean
|
resultSet.next(); |
resultSet.next(); |
return resultSet.getInt(1); |
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) |
|
{ |
|
Vector vec = getNextRow(); |
|
if (vec == null) |
|
break; |
|
t.add(vec.get(0)); |
|
} |
|
this.result=result; |
|
metaData = (this.result==null) ?null:this.result.getMetaData(); |
|
return t; |
|
} |
|
|
} |
} |