1 package eu
.etaxonomy
.cdm
.io
.common
;
4 * Created on 14.05.2005
5 * @author Andreas Müller
13 import org
.apache
.log4j
.Logger
;
18 * Creates Cursors from extern relational DB.
19 * Used only for developpers convienence hence undocumented.
20 * You may create input cursors in any other way you want
21 * @author Andreas Müller
24 static Logger logger
= Logger
.getLogger(Source
.class);
26 /* ************ Constants **************************************/
28 private final static boolean DEBUG_MODE
= false;
29 private final static boolean DEBUG_LOG_WRITER
= false;
32 // public final static String SQL_SERVER_2000 = "SQLServer2000";
33 public final static String SQL_SERVER_2005
= "SQLServer2005";
34 public final static String ACCESS
= "Access";
35 public final static String EXCEL
= "Excel";
36 public final static String ODDBC
= "ODBC";
37 public final static String ORACLE
= "Oracle";
38 public final static String DB2
= "DB2";
41 public final static String SELECT_DIRECT
= "direct";
42 public final static String SELECT_CURSOR
= "cursor";
45 // private static String clsSQLServer2000 = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
46 private static String clsSQLServer2005
= "com.microsoft.sqlserver.jdbc.SQLServerDriver";
47 private static String clsODBC
= "sun.jdbc.odbc.JdbcOdbcDriver";
48 private static String clsOracle
= "oracle.jdbc.driver.OracleDriver";
49 private static String clsDB2
= "COM.ibm.db2.jdbc.net.DB2Driver";
50 private static String clsSQLServerDdtek
= "com.ddtek.jdbc.sqlserver.SQLServerDriver";
51 private static String clsDefault
= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
54 // private static String urlSQLServer = "jdbc:microsoft:sqlserver://";
55 private static String urlSQLServer2005
= "jdbc:sqlserver://";
56 private static String urlDB2
= "jdbc:db2://";
57 private static String urlOracle
= "jdbc:oracle:thin:@:1243:";
58 private static String urlDataDirectSQLServer
= "jdbc:datadirect:sqlserver://";
59 private static String urlODBC
= "jdbc:odbc:";
60 private static String urlDefault
= "jdbc:microsoft:sqlserver://LAPI:1433;DatabaseName=studienarbeit;SelectMethod=direct";
63 /* *************** VARIABLES *******************************/
64 private Connection mConn
;
65 private Statement mStmt
;
66 private String mQuery
;
67 private String mUrl
= null;
68 private String mDb
= null;
69 private int mPort
= 1433; //default port TODO 2 derzeit nur für SQLServer, müsste auch für andere Datenbanken umgesetzt werden
70 private String mDbms
= null;
71 private String mServer
= null;
72 private boolean isCursor
;
73 private boolean connExist
= false; //does a Connection exist?
74 private String mUserName
;
77 private static String userNameDefault
= "sa"; //default user
78 private static String pwdDefault
= "sa"; //default PWD
81 /* ******************** CONSTRUCTORS **********************************/
85 * Creates a source and sets the according variables
86 * @param dbms name of database management system (e.g. "SQLServer", "Access", "Excel", "Oracle"
87 * @param server name of server, if dbms is ODBC, this must be the ODBC name
88 * @param db name of database
90 public Source(String dbms
, String server
, String db
){
94 this.mPwd
= pwdDefault
;
95 this.mUserName
= userNameDefault
;
99 * Creates a source and sets the query string
100 * @param dbms name of database management system (e.g. "SQLServer", "Access", "Excel", "Oracle"
101 * @param server name of server, if dbms is ODBC, this must be the ODBC name
102 * @param db name of database
103 * @param query the query
105 public Source(String dbms
, String server
, String db
, String query
){
106 this(dbms
, server
, db
);
107 this.setQuery(query
);
111 * Creates a source and sets the parameter.
112 * @param cursor access mode, if true 'cursor', if false 'direct'
114 public Source(String dbms
, String server
, String db
, boolean cursor
) {
115 this(dbms
, server
, db
);
121 * Creates a source and sets the parameter. If port is -1 the databases default port is used.
122 * @param dbms name of database management system (e.g. "SQLServer", "Access", "Excel", "Oracle"
123 * @param server name of server, if dbms is ODBC, this must be the ODBC name
124 * @param db name of database
125 * @param port the databases port (-1: use default port)
127 public Source(String dbms
, String server
, String db
, int port
) {
128 this(dbms
, server
, db
);
134 //********************* METHODS *****************************************************/
136 //******* RESULTSETS *******************//
138 * Returns the Resultset for query 'query'.
139 * Does not change the Sources query-string!!
140 * @return Resultset for the query.
142 public ResultSet
getResultSet (String query
){
145 this.getConnection(); //establish connection
149 mStmt
= mConn
.createStatement();
150 rs
= mStmt
.executeQuery(query
);
152 }catch(SQLException e
){
153 logger
.error("Problems when creating Resultset for query \n " + query
+ " \n" + "Exception: " + e
);
160 * Returns the Resultset for Sources query.
161 * @return Resultset for the Sources query
163 public ResultSet
getResultSet (){
164 return getResultSet(mQuery
);
168 // ***************CONNECTION *************************************//
170 * Returns the connection.
171 * @return the Sources connection
173 public Connection
getConnection(){
175 if (this.connExist
== false){
179 this.makeConnection() ;
182 } catch (SourceConnectionException e
) {
183 //TODO error weitergabe des Fehlers nach oben?
184 logger
.error("Error when establishing Connection");
186 } catch (SQLException e
) {
187 logger
.error("SQLException in getConnection.");
194 * Makes the connection.
195 * @return true if connection created
196 * @throws SourceConnectionException
198 private boolean makeConnection()throws SourceConnectionException
{
200 if (makeConnectionString() == false){
201 throw new SourceConnectionException ("Error in Connection String");
203 mConn
= DriverManager
.getConnection(mUrl
, mUserName
, mPwd
);
204 mConn
.setCatalog(mDb
); //
205 logger
.info("Connected to " + mConn
.getCatalog());
206 mStmt
= mConn
.createStatement();
207 this.connExist
= true;
209 }catch (SQLException e
){
210 logger
.error("Probleme beim Öffnen der Datenbank !!!\n" +
211 "URL: " + mUrl
+ "\n" +
213 throw new SourceConnectionException ();
218 * Makes the connection string
219 * @return false if ClassNotFoundException, else true
221 private boolean makeConnectionString(){
226 selectMethod
= SELECT_CURSOR
;
228 selectMethod
= SELECT_DIRECT
;
230 if (DEBUG_LOG_WRITER
) DriverManager
.setLogWriter((new PrintWriter(System
.out
)));
231 // if (mDbms.equalsIgnoreCase(SQL_SERVER_2000)) {
232 // Class.forName(clsSQLServer2000);
233 // server = mServer + ":" + mPort;
234 // mUrl = urlSQLServer + server + ";DataBase=" + mDb + ";SelectMethod="+ selectMethod;
236 else if (mDbms
.equalsIgnoreCase(SQL_SERVER_2005
)) {
237 Class
.forName(clsSQLServer2005
);
238 server
= mServer
+ ":" + mPort
;
239 mUrl
= urlSQLServer2005
+ server
+ ";databaseName=" + mDb
+";SelectMethod="+ selectMethod
;
241 else if (mDbms
.equalsIgnoreCase(ACCESS
)) {
242 Class
.forName(clsODBC
);
243 mUrl
= urlODBC
+ mDb
;
245 else if (mDbms
.equalsIgnoreCase(EXCEL
)) {
246 Class
.forName(clsODBC
);
247 mUrl
= urlODBC
+ mDb
;
249 else if (mDbms
.equalsIgnoreCase(ODDBC
)) {
250 Class
.forName(clsODBC
);
251 mUrl
= urlODBC
+ mServer
;
253 else if (mDbms
.equalsIgnoreCase(ORACLE
)) {
254 Class
.forName(clsOracle
);
255 mUrl
= urlOracle
+ mDb
;
257 else if (mDbms
.equalsIgnoreCase(DB2
)) {
258 Class
.forName(clsDB2
);
261 else if (mDbms
.equalsIgnoreCase("SQLServerDdtek")) {
262 Class
.forName(clsSQLServerDdtek
);
263 mUrl
= urlDataDirectSQLServer
+ mServer
;
266 Class
.forName(clsSQLServer2005
);
269 logger
.debug("Connection String: " + mUrl
);
271 }catch (ClassNotFoundException e
){
272 logger
.error("Datenbank-Treiber-Klasse konnte nicht geladen werden\n" + "Exception: " + e
.toString());
278 /* ******************** SETTER *************************************/
283 public Source
setQuery (String query
){
292 public Source
setUsername (String userName
){
293 if (userName
== null){
294 this.mUserName
= userNameDefault
;
296 this.mUserName
= userName
;
298 this.connExist
= false;
306 public Source
setPassword (String pwd
){
308 this.mPwd
= pwdDefault
;
312 this.connExist
= false;
317 * Sets the username and password.
321 public Source
setUserAndPwd (String userName
, String pwd
){
322 setUsername(userName
);
332 public Source
setPort (int port
){
334 this.connExist
= false;
338 public String
getDatabase(){
342 public String
getServer(){