Configuring Connections Explicitly
To specify connection information explicitly using XQJ, construct an XQDataSource instance in your Java application using the DDXQDataSource class.
If your Java application contains queries that access an XML file, you can directly access the file as shown in the following XQJ code, where the location and name of the XML file is specified as a parameter of fn:doc(), an XQuery function.
DDXQDataSource ds = new DDXQDataSource(); XQConnection conn = ds.getConnection(); conn.createExpression().executeQuery("doc('path_and_filename')");How you configure connection information for relational databases using XQJ depends on whether you are accessing a single database or multiple databases. If your Java application contains XQuery queries that access a single database, you can configure connection information using the DDXQDataSource class as shown in the following XQJ code:
DDXQDataSource ds = new DDXQDataSource(); ds.setJdbcUrl("jdbc:xquery:sqlserver://server1:1433;databaseName=stocks"); XQConnection conn = ds.getConnection("myuserid", "mypswd");If your Java application contains XQuery queries that access multiple databases, use the DDXQJDBCConnection class to configure connection information for each database connection, then construct an XQDataSource instance that specifies all database connections using the DDXQDataSource class as shown in the following XQJ code. When specifying the URI for multiple databases, use the DDXQJDBCConnection Url property instead of the DDXQDataSource jdbcUrl property to set the JDBC URI for each connection.
DDXQJDBCConnection jc1 = new DDXQJDBCConnection(); jc1.setUrl("jdbc:xquery:sqlserver://server1:1433;databaseName=stocks"); DDXQJDBCConnection jc2 = new DDXQJDBCConnection(); jc2.setUrl("jdbc:xquery:sqlserver://server2:1433;databaseName=holdings"); DDXQDataSource ds = new DDXQDataSource(); ds.setDdxqJdbcConnection(new DDXQJDBCConnection[] {jc1, jc2}); XQConnection conn = ds.getConnection("myuserid", "mypswd");In the preceding example, notice that the user name and password specified in the getConnection() method are used to establish all underlying JDBC connections. If you require different user names and passwords for each connection, set the user name and password on each DDXQJDBCConnection as shown in the following XQJ code:
DDXQJDBCConnection jc1 = new DDXQJDBCConnection(); jc1.setUrl("jdbc:xquery:sqlserver://server1:1433;databaseName=stocks"); jc1.setUser("myuserid1"); jc1.setPassword("mypswd1"); DDXQJDBCConnection jc2 = new DDXQJDBCConnection(); jc2.setUser("myuserid2"); jc2.setPassword("mypswd2"); jc2.setUrl("jdbc:xquery:sqlserver://server2:1433;databaseName=holdings"); DDXQDataSource ds = new DDXQDataSource(); ds.setDdxqJdbcConnection(new DDXQJDBCConnection[] {jc1, jc2}); XQConnection conn = ds.getConnection();The following table lists properties of the DDXQDataSource class and the corresponding properties of the DDXQJDBCConnection class:
If any of these DDXQJDBCConnection properties is specified for an individual connection and then specified again using DDXQDataSource, the latter overrides the former. The following example shows the Options property first specified for the jc1 connection, and specified again using the JdbcOptions property of DDXQDataSource. In this case, the precision and scale for xs:decimal specified for DDXQDataSource overrides the precision and scale for xs:decimal specified for the jc1 connection.
DDXQJDBCConnection jc1 = new DDXQJDBCConnection(); jc1.setUrl("jdbc:xquery:sqlserver://server1:1433;databaseName=stocks"); jcl.setUser("myuserid1"); jcl.setPassword("mypswd1"); jc1.setOptions("sql-decimal-cast=25,5"); DDXQJDBCConnection jc2 = new DDXQJDBCConnection(); jc2.setUser("myuserid2"); jc2.setPassword("mypswd2"); jc2.setUrl("jdbc:xquery:sqlserver://server2:1433;databaseName=holdings"); DDXQDataSource ds = new DDXQDataSource(); ds.setDdxqJdbcConnection(new DDXQJDBCConnection[] {jc1, jc2}); ds.setJdbcOptions("sql-decimal-cast=35,20"); XQConnection conn = ds.getConnection();See DDXQDataSource and DDXQJDBCConnection Properties for a description of properties you can set using XQJ.