This sample demonstrates how to use the Java Connection Factory to create a JDBC connection. Specifically, the sample explains how to perform these tasks:
It is possible to use the com.sas.rio.MVADriver to connect directly to a workspace server, as explained in Sample 26049. However, in some situations, connecting using the Java Connection Factory might provide these additional benefits:
The instructions on the Full Code tab are for SAS AppDev Studio users. If you do not use SAS AppDev Studio, then you must manually configure your build environment in order to run the Java code. This will require the Foundation Services JAR files.
This sample does not demonstrate how to use connection pooling, but uses a simple direct connection as an example. The Java Connection Factory documentation provides details on the options for pooling.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Tip: For help with building a SAS Java Project, see SAS Note 32218.
Edit the values for host, port, user, and password to match your environment.
String host = "server";
package samples;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.sas.iom.SAS.ILanguageService;
import com.sas.iom.SAS.IWorkspace;
import com.sas.iom.SAS.IWorkspaceHelper;
import com.sas.iom.SAS.ILanguageServicePackage.CarriageControlSeqHolder;
import com.sas.iom.SAS.ILanguageServicePackage.LineTypeSeqHolder;
import com.sas.iom.SASIOMDefs.GenericError;
import com.sas.iom.SASIOMDefs.StringSeqHolder;
import com.sas.rio.MVAConnection;
import com.sas.services.connection.BridgeServer;
import com.sas.services.connection.ConnectionFactoryConfiguration;
import com.sas.services.connection.ConnectionFactoryException;
import com.sas.services.connection.ConnectionFactoryInterface;
import com.sas.services.connection.ConnectionFactoryManager;
import com.sas.services.connection.ConnectionInterface;
import com.sas.services.connection.ManualConnectionFactoryConfiguration;
import com.sas.services.connection.Server;
public class JDBCThroughJavaConnectionFactory {
public static void main(String[] args) {
String host = "server";
int port = 8591;
String user = "sasdemo";
String password = "password";
MVAConnection jdbcConnection = null;
Statement stmt = null;
IWorkspace sasWorkspace = null;
try {
String classID = Server.CLSID_SAS;
// Connect to the server and retrieve an IWorkSpace
Server server = new BridgeServer(classID, host, port);
ConnectionFactoryConfiguration cxfConfig = new ManualConnectionFactoryConfiguration(
server);
ConnectionFactoryManager cxfManager = new ConnectionFactoryManager();
ConnectionFactoryInterface cxf = cxfManager.getFactory(cxfConfig);
ConnectionInterface cx = cxf.getConnection(user, password);
org.omg.CORBA.Object obj = cx.getObject();
sasWorkspace = IWorkspaceHelper.narrow(obj);
// Retrieve the ILanguageService. This will allow you to submit any SAS code
// We will use it to assign a libname
ILanguageService sasLanguage = sasWorkspace.LanguageService();
//sasLanguage.Submit("libname appdev 'c:\\data\\tables';");
sasLanguage.Submit("libname shrtest hostname=servername server=__8551 user=" + user + " password=" + password + ";");
// Retrieve the SAS log and print it out
CarriageControlSeqHolder logCarriageControlHldr = new CarriageControlSeqHolder();
LineTypeSeqHolder logLineTypeHldr = new LineTypeSeqHolder();
StringSeqHolder logHldr = new StringSeqHolder();
sasLanguage.FlushLogLines(Integer.MAX_VALUE,
logCarriageControlHldr, logLineTypeHldr, logHldr);
String[] logLines = logHldr.value;
for (int i = 0; i < logLines.length; i++) {
System.out.println(logLines[i]);
}
// Create a new jdbc connection using the Iworkspace
Properties jdbcProps = new Properties();
jdbcConnection = new MVAConnection(sasWorkspace, jdbcProps);
stmt = jdbcConnection.createStatement();
ResultSet rs = stmt.executeQuery("select * from shrtest.class");
// Just print out the column names, to verify the connection works
ResultSetMetaData rsmeta = rs.getMetaData();
for( int i=1; i< rsmeta.getColumnCount();i++ ){
System.out.println( "Column Label " + i + ": " + rsmeta.getColumnName(i));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ConnectionFactoryException e) {
e.printStackTrace();
} catch (GenericError e) {
e.printStackTrace();
} finally {
try {
if (null != stmt) {
stmt.close();
}
if (null != jdbcConnection) {
jdbcConnection.close();
sasWorkspace.Close();
}
} catch (SQLException e) {
e.printStackTrace();
} catch (GenericError e) {
e.printStackTrace();
}
}
}
}
|
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Date Modified: | 2008-09-16 15:01:39 |
Date Created: | 2008-08-26 09:26:47 |
Product Family | Product | Host | Product Release | SAS Release | ||
Starting | Ending | Starting | Ending | |||
SAS System | SAS Integration Technologies | z/OS | 9.1 TS1M3 SP4 | |||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M3 SP4 | |||||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M3 SP4 | |||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M3 SP4 | |||||
Microsoft Windows XP 64-bit Edition | 9.1 TS1M3 SP4 | |||||
Microsoft® Windows® for x64 | 9.1 TS1M3 SP4 | |||||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M3 SP4 | |||||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M3 SP4 | |||||
Microsoft Windows 2000 Server | 9.1 TS1M3 SP4 | |||||
Microsoft Windows 2000 Professional | 9.1 TS1M3 SP4 | |||||
Microsoft Windows NT Workstation | 9.1 TS1M3 SP4 | |||||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M3 SP4 | |||||
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M3 SP4 | |||||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M3 SP4 | |||||
Microsoft Windows XP Professional | 9.1 TS1M3 SP4 | |||||
Windows Vista | 9.1 TS1M3 SP4 | |||||
64-bit Enabled AIX | 9.1 TS1M3 SP4 | |||||
64-bit Enabled HP-UX | 9.1 TS1M3 SP4 | |||||
64-bit Enabled Solaris | 9.1 TS1M3 SP4 | |||||
HP-UX IPF | 9.1 TS1M3 SP4 | |||||
Linux | 9.1 TS1M3 SP4 | |||||
Linux on Itanium | 9.1 TS1M3 SP4 | |||||
OpenVMS Alpha | 9.1 TS1M3 SP4 | |||||
Tru64 UNIX | 9.1 TS1M3 SP4 | |||||
SAS System | SAS AppDev Studio | Microsoft® Windows® for x64 | 3.3 | 9.1 TS1M3 SP4 | ||
Microsoft Windows 2000 Advanced Server | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows 2000 Datacenter Server | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows 2000 Server | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows 2000 Professional | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows NT Workstation | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows Server 2003 Datacenter Edition | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows Server 2003 Enterprise Edition | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows Server 2003 Standard Edition | 3.3 | 9.1 TS1M3 SP4 | ||||
Microsoft Windows XP Professional | 3.3 | 9.1 TS1M3 SP4 | ||||
Windows Vista | 3.3 | 9.1 TS1M3 SP4 |