mercoledì 12 marzo 2014

Java utilizzo di Apache Derby

Per utilizzare Apache Derby occorre:
  1. Scaricare dal sito l'ultima release ( https://db.apache.org/derby/derby_downloads.html#Latest+Official+Releases );
  2. Dei vari jar scaricati prendere il derby.jar
Di default quando si creano gli archivi sono installati sotto la directory di esecuzione del programma.
In alternativa si può specificare una locazione differente settando la variabile derby.system.home .

Occorre prestare attenzione quando si fanno le insert di campi di tipo varchar a scrivere nelle query il singolo apice invece del doppio apice, per evitare di incappare in un errore di questo tipo:


 Column 'xxxx' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE  statement then 'mario' is not a column in the target table.

Di seguito un esempio di codice che crea un db, una tabella, effettua una insert e recupera quindi i dati:

package it.test.derby;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class ProvaDerby {
 public static void main(String[] args) throws Exception {
  System.setProperty("derby.system.home", "c:\\lav\\derbyTest");
  String url="jdbc:derby:database_test;create=true;user=root;password=root;";
  Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
  int risultato=0;
  Connection conn=DriverManager.getConnection(url);
  Statement st=conn.createStatement();
  String creazione="create table persona (id integer not null,nome varchar(16),cognome varchar(30), PRIMARY KEY(id))";
  risultato=st.executeUpdate(creazione);
  System.out.println("RISULTATO: "+risultato);
  String insert= "insert into persona (id,nome,cognome) values(1,\"mario\",'rossi')";
  PreparedStatement pst=conn.prepareStatement(insert);
   risultato=pst.executeUpdate();
  System.out.println("RISULTATO INSERT: "+risultato);
  String inquiry="select * from persona";
  pst=conn.prepareStatement(inquiry);
  ResultSet rs=pst.executeQuery();
  while(rs.next()){
   System.out.println("Nome: "+rs.getString("nome"));
   System.out.println("Cognome: "+rs.getString("cognome"));
  }
  rs.close();
  pst.close();
  conn.close();
  

 }

}



Il contenuto della directory derbyTest sarà il seguente:


giovedì 6 marzo 2014

Java leggere file di properties

Utilizzando un file di properties in formato CHIAVE=VALORE è possibile leggere a runtime gli elementi presenti in questo modo


Properties p=new Properties();
p.load(new FileInputStream(new File("log.properties")));
        Set<Entry<Object, Object>> s=p.entrySet();
       Iterator<Entry<Object,Object>> it= s.iterator();
       while(it.hasNext()){
     Entry<Object,Object> e=it.next();
     String chiave=(String)e.getKey();
     String valore=(String)e.getValue();
     System.out.println("Chiave: "+chiave);
     System.out.println("Valore: "+ valore);
   }

lunedì 3 marzo 2014

JAX-B errore Two declarations cause a collision in the ObjectFactory class

Questo errore si presenta quando si prova a generare un client di un Web Service e alcuni tipi definiti nel servizio contengono elementi dal nome simile.
Una prova per cercare di risolvere il problema è specificare nel lancio del wsimport i parametri:

-B-XautoNameResolution

quindi con un comando di questo tipo

wsimport -keep -p it.test -B-XautoNameResolution [URL_WSDL]