Update ooo320-m1
[ooovba.git] / connectivity / workben / TT / StartTest.java
blob7ffecc539ba8bcf2f1a20e84925d92625a997288
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: StartTest.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package workben.tt;
32 import java.lang.Object;
33 import java.io.*;
34 import workben.odbc.*;
36 import com.sun.star.comp.servicemanager.ServiceManager;
37 // import java.io.IOException;
39 //import com.sun.star.beans.PropertyValue;
40 //import com.sun.star.beans.PropertyState;
41 import com.sun.star.connection.XConnector;
42 import com.sun.star.connection.XConnection;
43 import com.sun.star.bridge.XBridge;
45 // import com.sun.star.comp.bootstrap.Bootstrap;
47 // import com.sun.star.io.XInputStream;
48 // import com.sun.star.io.XOutputStream;
50 // import com.sun.star.lang.XComponent;
51 import com.sun.star.lang.XMultiServiceFactory;
52 import com.sun.star.lang.XServiceInfo;
54 import com.sun.star.uno.IBridge;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.uno.XInterface;
57 import com.sun.star.uno.XNamingService;
59 import com.sun.star.sdbc.*;
60 // import com.sun.star.sdbc.XConnection;
61 // import com.sun.star.uno.Enum;
63 // import com.sun.star.lib.uno.typeinfo.ParameterTypeInfo;
64 // import com.sun.star.lib.uno.typeinfo.TypeInfo;
66 public class StartTest
68 //###########################################################
69 //# allgemeine Variablen #
70 //###########################################################
71 private static java.util.Hashtable ConfigItems = new java.util.Hashtable();
72 //###########################################################
73 //# ResultSetToStringBuffer #
74 //###########################################################
75 static StringBuffer ResultSetToStringBuffer(com.sun.star.sdbc.XResultSet results)
77 com.sun.star.sdbc.XRow Row;
78 com.sun.star.sdbc.XResultSetMetaDataSupplier rsmds;
79 com.sun.star.sdbc.XResultSetMetaData rsmd;
81 StringBuffer buf = new StringBuffer();
83 try
85 rsmds = (com.sun.star.sdbc.XResultSetMetaDataSupplier)UnoRuntime.queryInterface(com.sun.star.sdbc.XResultSetMetaDataSupplier.class,results);
86 rsmd = rsmds.getMetaData();
87 int numCols = rsmd.getColumnCount();
88 int i;
90 // get column header info
91 for (i=1; i <= numCols; i++)
93 if (i == 1) buf.append("\"");
94 if (i > 1) buf.append("\"\t\"");
95 buf.append(rsmd.getColumnLabel(i));
97 buf.append("\"\r\n");
101 while (results.next())
103 Row = (com.sun.star.sdbc.XRow)UnoRuntime.queryInterface(com.sun.star.sdbc.XRow.class, results);
104 for (i=1; i <= numCols; i++)
106 if (i == 1) buf.append("\"");
107 if (i > 1) buf.append("\"\t\"");
108 buf.append(Row.getString(i));
110 buf.append("\"\r\n");
112 return(buf);
114 catch (Exception e)
116 System.out.println("Exception at ResultSetToStringBuffer : " + e);
117 return(buf);
121 //###########################################################
122 //# printStringBuffer #
123 //###########################################################
125 static void printStringBuffer(StringBuffer buf)
127 FileWriter f1;
130 f1 = new FileWriter("d:\\data\\sdbc\\test.log",true);
131 int maxStrings = buf.length() - 1;
132 //System.out.println(maxStrings);
133 for(int i=0 ; i<=maxStrings ; i++)
135 f1.write(buf.charAt(i));
137 f1.close();
139 catch( IOException e )
141 System.out.println("Error: can't create logfile");
145 //###########################################################
146 //# neededServices #
147 //###########################################################
148 static String neededServices[] = new String[] {
149 "com.sun.star.comp.servicemanager.ServiceManager",
150 "com.sun.star.comp.loader.JavaLoader",
151 "com.sun.star.comp.connections.Connector",
152 "com.sun.star.comp.connections.Acceptor"
155 //###########################################################
156 //# readConfigFile #
157 //###########################################################
158 static void readConfigFile(String sFileName)
160 BufferedReader confFile;
163 confFile = new BufferedReader(new FileReader(sFileName));
164 ConfigItems.put("ConnectString",confFile.readLine());
165 ConfigItems.put("user",confFile.readLine());
166 ConfigItems.put("password",confFile.readLine());
167 ConfigItems.put("ToTest",confFile.readLine());
168 ConfigItems.put("LogFile",confFile.readLine());
169 confFile.close();
171 catch(Exception e)
173 System.out.println(e);
177 //###########################################################
178 //# MAIN #
179 //###########################################################
181 public static void main(String argv[]) throws Exception
184 //Die Parameter auslesen
186 // if(argv.length == 0)
187 // {
188 // System.out.println("missing parameter");
189 // System.exit(0);
190 // }
192 //readConfigFile(argv[0]);
194 ////////////////////////
196 com.sun.star.comp.servicemanager.ServiceManager smgr = new com.sun.star.comp.servicemanager.ServiceManager();
197 smgr.addFactories(neededServices);
199 XConnector xConnector = (XConnector)smgr.createInstance("com.sun.star.connection.Connector");
200 if(xConnector == null) System.err.println("no connector!");
202 XConnection xConn = xConnector.connect("socket,host=localhost,port=6001");
203 if(xConn == null) System.err.println("no XConnection!");
205 IBridge iBridge = UnoRuntime.getBridgeByName("java", null, "remote", null, new Object[]{"iiop", xConn, null});
207 Object rInitialObject = iBridge.mapInterfaceFrom("classic_uno", XInterface.class);
209 if(rInitialObject != null)
211 System.err.println("got the remote object");
212 System.out.println("before naming service !");
215 XNamingService rName = (XNamingService)UnoRuntime.queryInterface(XNamingService.class, rInitialObject );
218 if(rName != null)
220 System.err.println("got the remote naming service !");
221 Object rXsmgr = rName.getRegisteredObject("StarOffice.ServiceManager");
222 XMultiServiceFactory rSmgr = (XMultiServiceFactory)UnoRuntime.queryInterface(XMultiServiceFactory.class, rXsmgr);
223 if(rSmgr != null)
225 System.out.println("got the remote service manager !");
226 Object rDriver = rSmgr.createInstance("com.sun.star.sdbc.ADriver");
227 if(rDriver != null)
229 System.out.println("got a com.sun.star.sdbc.Driver !");
230 com.sun.star.sdbc.XDriver xDriver = (XDriver)UnoRuntime.queryInterface(com.sun.star.sdbc.XDriver.class,rDriver);
231 if(xDriver != null)
233 com.sun.star.sdbc.XConnection xConnection = null;
236 com.sun.star.beans.PropertyValue [] ConInfo = new com.sun.star.beans.PropertyValue[]
238 new com.sun.star.beans.PropertyValue("user",0,"qsuser",com.sun.star.beans.PropertyState.DIRECT_VALUE),
239 new com.sun.star.beans.PropertyValue("password",0,"qsuser",com.sun.star.beans.PropertyState.DIRECT_VALUE)
241 xConnection = xDriver.connect("sdbc:ado:PROVIDER=SQLOLEDB;DATA SOURCE=sqllab2",ConInfo);
243 if(xConnection != null)
245 System.out.println("got a connection!");
246 com.sun.star.sdbc.XDatabaseMetaData dmd;
247 com.sun.star.sdbc.XStatement stmt;
248 com.sun.star.sdbc.XResultSet result;
249 com.sun.star.sdbc.XRow row;
252 stmt = xConnection.createStatement();
253 stmt.executeUpdate("insert into testtab values(4,'vier')");
254 result = stmt.executeQuery("select * from testtab where int = 4");
255 com.sun.star.sdbc.XResultSetMetaDataSupplier rsmds;
257 rsmds = (com.sun.star.sdbc.XResultSetMetaDataSupplier)UnoRuntime.queryInterface(com.sun.star.sdbc.XResultSetMetaDataSupplier.class,result);
258 com.sun.star.sdbc.XResultSetMetaData xMD = rsmds.getMetaData();
259 System.out.println("Anzahl Spalten = " + xMD.getColumnCount());
260 if(result.next())
262 row = (com.sun.star.sdbc.XRow)UnoRuntime.queryInterface(com.sun.star.sdbc.XRow.class,result);
263 String xx = row.getString(1);
266 catch (Exception e)
268 System.out.println("Exception beim Aufruf der Tests");
272 catch(Exception e)
274 System.out.println("exception while connecting!");
275 e.printStackTrace();
277 xConnection.close();
283 catch(Exception t)
287 catch(Exception e1)
289 System.err.println("exception from getRegisteredObject!");
292 System.out.println("FERTIG");
293 System.exit(0);
295 }// die Klammer der ganzen Klasse