import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Dati { private String stazione; private String testo; private String data; private float valore; public Dati() {} public Dati(String stazione, String testo, String data, float valore) { this.stazione = stazione; this.testo = testo; this.data = data; this.valore = valore; } @XmlElement public String getStazione() { return stazione; } @XmlElement public String getTesto() { return testo; } @XmlElement public String getData() { return data; } @XmlElement public float getValore() { return valore; } public void setStazione(String stazione) { this.stazione = stazione; } public void setTesto(String testo) { this.testo = testo; } public void setData(String data) { this.data = data; } public void setValore(float valore) { this.valore = valore; } }
import java.util.ArrayList; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class ListaDati { @XmlElement(name="dati") private ArrayList<Dati> lista = new ArrayList<Dati>(); public ListaDati() { } public ListaDati(ArrayList<Dati> lista) { this.lista = lista; } public void add(Dati dati) { lista.add(dati); } public int size() { return lista.size(); } public Dati get(int index) { return lista.get(index); } public void removeAll() { lista.clear(); } public void remove(int index) { lista.remove(index); } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; public class Database { private String schema; private String username; private String password; private String host; private int port; private Connection conn = null; public Database(String schema, String username, String password, String host, int port) { this.schema = schema; this.username = username; this.password = password; this.host = host; this.port = port; } public void Connetti() { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection( "jdbc:mysql://"+host+":"+port+"/"+schema,username,password); } catch (Exception ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } } public void Sconnetti() { if (conn != null) try { conn.close(); } catch (SQLException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } } public ListaDati getDati(String codice) { ListaDati ret = null; if (conn != null) { try { String sql = "select * from dati where codice = ? order by data"; PreparedStatement psd = conn.prepareStatement(sql); psd.setString(1, codice); ResultSet rsd = psd.executeQuery(); if (rsd != null) { while(rsd.next()) { String stazione = rsd.getString("stazione"); String testo = rsd.getString("testo"); String data = rsd.getString("data"); float valore = rsd.getFloat("valore"); Dati st = new Dati(stazione, testo, data, valore); ret.add(st); } } } catch(Exception ex) { System.out.println("errore query stati " + ex.getMessage()); } } return ret; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StringWriter; import java.net.Socket; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; public class ThreadDati extends Thread { private Socket socket = null; private BufferedReader in; private PrintWriter out; private Database db; public ThreadDati(Socket socket, String schema, String username, String password, String host, int port) { this.socket = socket; if (socket != null) { try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); db = new Database(schema, username, password, host, port); } catch (IOException ex) { Logger.getLogger(ThreadDati.class.getName()).log(Level.SEVERE, null, ex); } } } @Override public void run() { if (socket != null && db != null) { db.Connetti(); while(true) { try { String codice = in.readLine(); if(codice != null && codice.length() > 0) { ListaDati lista = db.getDati(codice); JAXBContext context = JAXBContext.newInstance(ListaDati.class); Marshaller m = context.createMarshaller(); StringWriter sw = new StringWriter(); m.marshal(lista, sw); out.println(sw.toString()); } } catch (Exception ex) { Logger.getLogger(ThreadDati.class.getName()).log(Level.SEVERE, null, ex); } } } } }