lunedì 3 febbraio 2014

JDBC 4.0 utilizzo WebRowSet

L'interfaccia WebRowSet eredita da CachedRowSet e rappresenta una vista disconnessa dai dati del db.
Produce un output in formato xml dei risultati e della struttura delle tabelle coinvolte.
Es:

package it.rowsetTest;
import java.sql.Connection;
import java.sql.DriverManager;
import javax.sql.rowset.WebRowSet;
import com.sun.rowset.WebRowSetImpl;
public class XmlRowSet {
 public static void main(String[] args) throws Exception {
  String urlConn="";
  
  WebRowSet p= new WebRowSetImpl();
  String select="select top 5 id,descrizione from S_ATECO2007";
  p.setCommand(select);
  p.setUrl("jdbc:sqlserver://127.0.0.1:1433;databaseName=test;selectMethod=cursor");
  p.setUsername("test");
  p.setPassword("test"); 
  p.execute();
  java.io.FileOutputStream oStream =new java.io.FileOutputStream("test.xml");
  p.writeXml(oStream);
 }

}


In questo modo avremo un output di questo tipo:

<?xml version="1.0"?>
<webRowSet xmlns="http://java.sun.com/xml/ns/jdbc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/jdbc http://java.sun.com/xml/ns/jdbc/webrowset.xsd">
  <properties>
    <command>select top 5 id,descrizione from S_ATECO2007</command>
    <concurrency>1008</concurrency>
    <datasource><null/></datasource>
    <escape-processing>true</escape-processing>
    <fetch-direction>1000</fetch-direction>
    <fetch-size>0</fetch-size>
    <isolation-level>2</isolation-level>
    <key-columns>
    </key-columns>
    <map>
    </map>
    <max-field-size>0</max-field-size>
    <max-rows>0</max-rows>
    <query-timeout>0</query-timeout>
    <read-only>true</read-only>
    <rowset-type>ResultSet.TYPE_SCROLL_INSENSITIVE</rowset-type>
    <show-deleted>false</show-deleted>
    <table-name>S_ATECO2007</table-name>
    <url>jdbc:sqlserver:gianos_professionisti</url>
    <sync-provider>
      <sync-provider-name>com.sun.rowset.providers.RIOptimisticProvider</sync-provider-name>
      <sync-provider-vendor>Oracle Corporation</sync-provider-vendor>
      <sync-provider-version>1.0</sync-provider-version>
      <sync-provider-grade>2</sync-provider-grade>
      <data-source-lock>1</data-source-lock>
    </sync-provider>
  </properties>
  <metadata>
    <column-count>2</column-count>
    <column-definition>
      <column-index>1</column-index>
      <auto-increment>true</auto-increment>
      <case-sensitive>false</case-sensitive>
      <currency>false</currency>
      <nullable>0</nullable>
      <signed>true</signed>
      <searchable>true</searchable>
      <column-display-size>11</column-display-size>
      <column-label>id</column-label>
      <column-name>id</column-name>
      <schema-name></schema-name>
      <column-precision>10</column-precision>
      <column-scale>0</column-scale>
      <table-name>S_ATECO2007</table-name>
      <catalog-name></catalog-name>
      <column-type>4</column-type>
      <column-type-name>int</column-type-name>
    </column-definition>
    <column-definition>
      <column-index>2</column-index>
      <auto-increment>false</auto-increment>
      <case-sensitive>false</case-sensitive>
      <currency>false</currency>
      <nullable>1</nullable>
      <signed>false</signed>
      <searchable>true</searchable>
      <column-display-size>250</column-display-size>
      <column-label>descrizione</column-label>
      <column-name>descrizione</column-name>
      <schema-name></schema-name>
      <column-precision>250</column-precision>
      <column-scale>0</column-scale>
      <table-name>S_ATECO2007</table-name>
      <catalog-name></catalog-name>
      <column-type>12</column-type>
      <column-type-name>varchar</column-type-name>
    </column-definition>
  </metadata>
  <data>
    <currentRow>
      <columnValue>1</columnValue>
      <columnValue>AGRICOLTURA, SILVICOLTURA E PESCA                                                                                                                                                                                                                         </columnValue>
    </currentRow>
    <currentRow>
      <columnValue>2</columnValue>
      <columnValue>COLTIVAZIONI AGRICOLE E PRODUZIONE DI PRODOTTI ANIMALI, CACCIA E SERVIZI CONNESSI                                                                                                                                                                         </columnValue>
    </currentRow>
    <currentRow>
      <columnValue>3</columnValue>
      <columnValue>COLTIVAZIONE DI COLTURE AGRICOLE NON PERMANENTI                                                                                                                                                                                                           </columnValue>
    </currentRow>
    <currentRow>
      <columnValue>4</columnValue>
      <columnValue>Coltivazione di cereali (escluso il riso), legumi da granella e semi oleosi                                                                                                                                                                               </columnValue>
    </currentRow>
    <currentRow>
      <columnValue>5</columnValue>
      <columnValue>Coltivazione di cereali (escluso il riso)                                                                                                                                                                                                                 </columnValue>
    </currentRow>
  </data>
</webRowSet>


Nessun commento:

Posta un commento