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 * To change this template, choose Tools | Templates 24 * and open the template in the editor. 25 */ 26 27 package connectivity.tools.sdb; 28 29 import com.sun.star.lang.XMultiServiceFactory; 30 import com.sun.star.sdb.XSingleSelectQueryComposer; 31 import com.sun.star.sdbc.SQLException; 32 import com.sun.star.sdbc.XConnection; 33 import com.sun.star.sdbc.XDatabaseMetaData; 34 import com.sun.star.sdbc.XPreparedStatement; 35 import com.sun.star.sdbc.XResultSet; 36 import com.sun.star.sdbc.XStatement; 37 import com.sun.star.sdbcx.XTablesSupplier; 38 import com.sun.star.uno.Exception; 39 import com.sun.star.uno.UnoRuntime; 40 import com.sun.star.util.XRefreshable; 41 42 /** 43 * is a convenience wrapper around a SDB-level connection object 44 */ 45 public class Connection 46 { 47 private final XConnection m_connection; 48 Connection( final XConnection _connection )49 public Connection( final XConnection _connection ) 50 { 51 m_connection = _connection; 52 } 53 getXConnection()54 public XConnection getXConnection() 55 { 56 return m_connection; 57 } 58 execute( final String _sql )59 public boolean execute( final String _sql ) throws SQLException 60 { 61 XStatement statement = createStatement(); 62 return statement.execute( _sql ); 63 } 64 executeQuery( final String _sql )65 public XResultSet executeQuery( final String _sql ) throws SQLException 66 { 67 XStatement statement = createStatement(); 68 return statement.executeQuery( _sql ); 69 } 70 executeUpdate( final String _sql )71 public int executeUpdate( final String _sql ) throws SQLException 72 { 73 XStatement statement = createStatement(); 74 return statement.executeUpdate( _sql ); 75 } 76 refreshTables()77 public void refreshTables() 78 { 79 final XTablesSupplier suppTables = UnoRuntime.queryInterface(XTablesSupplier.class, m_connection); 80 final XRefreshable refresh = UnoRuntime.queryInterface( XRefreshable.class, suppTables.getTables() ); 81 refresh.refresh(); 82 } 83 createSingleSelectQueryComposer()84 public XSingleSelectQueryComposer createSingleSelectQueryComposer() throws Exception 85 { 86 final XMultiServiceFactory connectionFactory = UnoRuntime.queryInterface( XMultiServiceFactory.class, m_connection ); 87 return UnoRuntime.queryInterface( 88 XSingleSelectQueryComposer.class, connectionFactory.createInstance( "com.sun.star.sdb.SingleSelectQueryComposer" ) ); 89 } 90 91 public createStatement()92 XStatement createStatement() throws SQLException 93 { 94 return m_connection.createStatement(); 95 } 96 97 public prepareStatement( String _sql )98 XPreparedStatement prepareStatement( String _sql ) throws SQLException 99 { 100 return m_connection.prepareStatement( _sql ); 101 } 102 103 public getMetaData()104 XDatabaseMetaData getMetaData() throws SQLException 105 { 106 return m_connection.getMetaData(); 107 } 108 109 public close()110 void close() throws SQLException 111 { 112 m_connection.close(); 113 } 114 } 115