xref: /trunk/main/connectivity/qa/complex/connectivity/JdbcLongVarCharTest.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package complex.connectivity;
28 
29 import com.sun.star.beans.PropertyState;
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sdbc.XResultSet;
33 import com.sun.star.sdbc.XClob;
34 import com.sun.star.sdbc.XDriverAccess;
35 import com.sun.star.sdbc.XParameters;
36 import com.sun.star.sdbc.XPreparedStatement;
37 import com.sun.star.sdbc.XResultSetMetaData;
38 import com.sun.star.sdbc.XResultSetMetaDataSupplier;
39 import com.sun.star.sdbc.XRow;
40 import com.sun.star.uno.UnoRuntime;
41 import complexlib.ComplexTestCase;
42 
43 public class JdbcLongVarCharTest extends ComplexTestCase
44 {
45 
46     public String[] getTestMethodNames()
47     {
48         return new String[]
49                 {
50                     "testLongVarChar"
51                 };
52     }
53 
54     @Override
55     public String getTestObjectName()
56     {
57         return "LongVarCharTest";
58     }
59 
60     public void testLongVarChar() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
61     {
62 
63         try
64         {
65             System.out.println("== Start testing ==");
66 
67             String url = "jdbc:mysql://localhost:3306/mysql?user=root";
68             //String url = "jdbc:ingres://localhost:II7/demodb;AUTO=multi";
69             com.sun.star.sdbc.XConnection xConnection = null;
70             com.sun.star.beans.PropertyValue prop[] = new PropertyValue[1];
71             prop[0] = new PropertyValue("JavaDriverClass", 0, "com.mysql.jdbc.Driver", PropertyState.DIRECT_VALUE);
72             //prop[0] = new PropertyValue("JavaDriverClass", 0, "com.ingres.jdbc.IngresDriver", PropertyState.DIRECT_VALUE);
73 
74             // get the remote office component context
75             XMultiServiceFactory xServiceManager = (XMultiServiceFactory) param.getMSF();
76             Object x = xServiceManager.createInstance("com.sun.star.sdbc.DriverManager");
77             com.sun.star.sdbc.XDriverAccess xDriverAccess = (XDriverAccess) UnoRuntime.queryInterface(XDriverAccess.class, x);
78             com.sun.star.sdbc.XDriver xDriver = xDriverAccess.getDriverByURL(url);
79             xConnection = xDriver.connect(url, prop);
80 
81             //Object prepStmnt = xConnection.prepareStatement("SELECT * FROM t1 WHERE t1.c1 = ?");
82             Object prepStmnt = xConnection.prepareStatement("SELECT * FROM i90114 WHERE i90114.c1 = ?");
83             ((XParameters) UnoRuntime.queryInterface(XParameters.class, prepStmnt)).clearParameters();
84             ((XParameters) UnoRuntime.queryInterface(XParameters.class, prepStmnt)).setInt(1, 1);
85             XResultSet xResultSet = ((XPreparedStatement) prepStmnt).executeQuery();
86             XRow xRow = (XRow) UnoRuntime.queryInterface(XRow.class, xResultSet);
87 
88             XResultSetMetaDataSupplier xRsMetaSup = (XResultSetMetaDataSupplier) UnoRuntime.queryInterface(XResultSetMetaDataSupplier.class, xResultSet);
89             XResultSetMetaData xRsMetaData = xRsMetaSup.getMetaData();
90             int nColumnCount = xRsMetaData.getColumnCount();
91 
92             System.out.println("== MetaData ==");
93             for (int i = 1; i <= nColumnCount; ++i)
94             {
95                 System.out.println("Name: " + xRsMetaData.getColumnName(i) + " Type: " +
96                         xRsMetaData.getColumnType(i));
97             }
98 
99             System.out.println("== Result ==");
100             while (xResultSet.next())
101             {
102                 String str = "not set";
103 
104                 XClob xClob = null;
105                 xClob = xRow.getClob(2);
106                 if (xClob != null)
107                 {
108                     System.out.println("xClob != null");
109                     int len = (int) xClob.length();
110                     str = xClob.getSubString(1, len);
111                 }
112                 else
113                 {
114                     System.out.println("xClob == null");
115                 }
116 
117                 System.out.println("c1 (Int): " + xRow.getInt(1) + " c2 (String): " + xRow.getString(2) + " c3 (Clob): " + str);
118             }
119 
120             xConnection.close();
121         }
122         catch (java.lang.Exception e)
123         {
124             System.out.println("== Exception occured while testing ==");
125             e.printStackTrace();
126         } finally
127         {
128             System.out.println("== End testing ==");
129             System.exit(0);
130         }
131     }
132 }