1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package complex.connectivity;
24 
25 import com.sun.star.beans.PropertyState;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.sdbc.XResultSet;
29 import com.sun.star.sdbc.XClob;
30 import com.sun.star.sdbc.XDriverAccess;
31 import com.sun.star.sdbc.XParameters;
32 import com.sun.star.sdbc.XPreparedStatement;
33 import com.sun.star.sdbc.XResultSetMetaData;
34 import com.sun.star.sdbc.XResultSetMetaDataSupplier;
35 import com.sun.star.sdbc.XRow;
36 import com.sun.star.uno.UnoRuntime;
37 import org.openoffice.test.OfficeConnection;
38 
39 import org.junit.After;
40 import org.junit.AfterClass;
41 import org.junit.Before;
42 import org.junit.BeforeClass;
43 import org.junit.Test;
44 import static org.junit.Assert.*;
45 
46 public class JdbcLongVarCharTest
47 {
48     private static final OfficeConnection connection = new OfficeConnection();
49 
50     @BeforeClass
beforeClass()51     public static void beforeClass() throws Exception
52     {
53         connection.setUp();
54     }
55 
56     @AfterClass
afterClass()57     public static void afterClass() throws Exception
58     {
59         connection.tearDown();
60     }
61 
62     @Test
testLongVarChar()63     public void testLongVarChar() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
64     {
65 
66         try
67         {
68             System.out.println("== Start testing ==");
69 
70             String url = "jdbc:mysql://localhost:3306/mysql?user=root";
71             //String url = "jdbc:ingres://localhost:II7/demodb;AUTO=multi";
72             com.sun.star.sdbc.XConnection xConnection = null;
73             com.sun.star.beans.PropertyValue prop[] = new PropertyValue[1];
74             prop[0] = new PropertyValue("JavaDriverClass", 0, "com.mysql.jdbc.Driver", PropertyState.DIRECT_VALUE);
75             //prop[0] = new PropertyValue("JavaDriverClass", 0, "com.ingres.jdbc.IngresDriver", PropertyState.DIRECT_VALUE);
76 
77             // get the remote office component context
78             XMultiServiceFactory xServiceManager = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
79             Object x = xServiceManager.createInstance("com.sun.star.sdbc.DriverManager");
80             com.sun.star.sdbc.XDriverAccess xDriverAccess = (XDriverAccess) UnoRuntime.queryInterface(XDriverAccess.class, x);
81             com.sun.star.sdbc.XDriver xDriver = xDriverAccess.getDriverByURL(url);
82             xConnection = xDriver.connect(url, prop);
83 
84             //Object prepStmnt = xConnection.prepareStatement("SELECT * FROM t1 WHERE t1.c1 = ?");
85             Object prepStmnt = xConnection.prepareStatement("SELECT * FROM i90114 WHERE i90114.c1 = ?");
86             ((XParameters) UnoRuntime.queryInterface(XParameters.class, prepStmnt)).clearParameters();
87             ((XParameters) UnoRuntime.queryInterface(XParameters.class, prepStmnt)).setInt(1, 1);
88             XResultSet xResultSet = ((XPreparedStatement) prepStmnt).executeQuery();
89             XRow xRow = (XRow) UnoRuntime.queryInterface(XRow.class, xResultSet);
90 
91             XResultSetMetaDataSupplier xRsMetaSup = (XResultSetMetaDataSupplier) UnoRuntime.queryInterface(XResultSetMetaDataSupplier.class, xResultSet);
92             XResultSetMetaData xRsMetaData = xRsMetaSup.getMetaData();
93             int nColumnCount = xRsMetaData.getColumnCount();
94 
95             System.out.println("== MetaData ==");
96             for (int i = 1; i <= nColumnCount; ++i)
97             {
98                 System.out.println("Name: " + xRsMetaData.getColumnName(i) + " Type: " +
99                         xRsMetaData.getColumnType(i));
100             }
101 
102             System.out.println("== Result ==");
103             while (xResultSet.next())
104             {
105                 String str = "not set";
106 
107                 XClob xClob = null;
108                 xClob = xRow.getClob(2);
109                 if (xClob != null)
110                 {
111                     System.out.println("xClob != null");
112                     int len = (int) xClob.length();
113                     str = xClob.getSubString(1, len);
114                 }
115                 else
116                 {
117                     System.out.println("xClob == null");
118                 }
119 
120                 System.out.println("c1 (Int): " + xRow.getInt(1) + " c2 (String): " + xRow.getString(2) + " c3 (Clob): " + str);
121             }
122 
123             xConnection.close();
124         }
125         catch (java.lang.Exception e)
126         {
127             System.out.println("== Exception occurred while testing ==");
128             e.printStackTrace();
129         } finally
130         {
131             System.out.println("== End testing ==");
132             System.exit(0);
133         }
134     }
135 }
136