Ho riscritto tutto cosi:
Modello:
Server:codice:ublic class Busta implements Serializable{ private static final long serialVersionUID = -6812221116817885840L; String Nbusta, Nome; public Busta(String codice, String nome) { this.Nbusta = codice; this.Nome = nome; } public String getNbusta() { return Nbusta; } public void setNbusta(String nbusta) { this.Nbusta = nbusta; } public String getNome() { return Nome; } public void setNome(String nome) { this.Nome = nome; } }
codice:public class Server { static ArrayList<Busta> prova; public static void main(String srgs[]) { int count = 0; initList(); //hard code to use port 8080 try (ServerSocket serverSocket = new ServerSocket(6789)) { System.out.println("I'm waiting here: " + serverSocket.getLocalPort()); while (true) { try { Socket socket = serverSocket.accept(); count++; System.out.println("#" + count + " from " + socket.getInetAddress() + ":" + socket.getPort()); HostThread myHostThread = new HostThread(socket, count); myHostThread.start(); } catch (IOException ex) { System.out.println(ex.toString()); } } } catch (IOException ex) { System.out.println(ex.toString()); } } private static class HostThread extends Thread{ private Socket hostThreadSocket; int cnt; HostThread(Socket socket, int c){ hostThreadSocket = socket; cnt = c; } @Override public void run() { OutputStream outputStream; try { outputStream = hostThreadSocket.getOutputStream(); try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) { objectOutputStream.writeObject(prova); } } catch (IOException ex) { System.out.println(ex.toString()); }finally{ try { hostThreadSocket.close(); } catch (IOException ex) { System.out.println(ex.toString()); } } } } private static void initList(){ try { prova = new ArrayList<Busta>(); String DATABASE_URL = "jdbc:ucanaccess:///OneDrive//Programmazione//Android//dati.mdb;password=gmpa;memory=false"; Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection conn = DriverManager.getConnection(DATABASE_URL); System.out.println(conn); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM [tabripa]"); while (rs.next()) { prova.add( new Busta(rs.getString("Nbusta").toString(),rs.getString("Nome").toString())); //prova.add(new Busta("Nome","cognome")); } s.close(); conn.close(); } catch(Exception ex) { ex.printStackTrace(); } }
Client:
Funziona solo che invece di restituirmi i dati in formato string me li restituisce con il segnaposto in memoria:codice:public class Server { static ArrayList<Busta> prova; public static void main(String srgs[]) { int count = 0; initList(); //hard code to use port 8080 try (ServerSocket serverSocket = new ServerSocket(6789)) { System.out.println("I'm waiting here: " + serverSocket.getLocalPort()); while (true) { try { Socket socket = serverSocket.accept(); count++; System.out.println("#" + count + " from " + socket.getInetAddress() + ":" + socket.getPort()); HostThread myHostThread = new HostThread(socket, count); myHostThread.start(); } catch (IOException ex) { System.out.println(ex.toString()); } } } catch (IOException ex) { System.out.println(ex.toString()); } } private static class HostThread extends Thread{ private Socket hostThreadSocket; int cnt; HostThread(Socket socket, int c){ hostThreadSocket = socket; cnt = c; } @Override public void run() { OutputStream outputStream; try { outputStream = hostThreadSocket.getOutputStream(); try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream)) { objectOutputStream.writeObject(prova); } } catch (IOException ex) { System.out.println(ex.toString()); }finally{ try { hostThreadSocket.close(); } catch (IOException ex) { System.out.println(ex.toString()); } } } } private static void initList(){ try { prova = new ArrayList<Busta>(); String DATABASE_URL = "jdbc:ucanaccess:///OneDrive//Programmazione//Android//dati.mdb;password=gmpa;memory=false"; Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); Connection conn = DriverManager.getConnection(DATABASE_URL); System.out.println(conn); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT * FROM [tabripa]"); while (rs.next()) { prova.add( new Busta(rs.getString("Nbusta").toString(),rs.getString("Nome").toString())); //prova.add(new Busta("Nome","cognome")); } s.close(); conn.close(); } catch(Exception ex) { ex.printStackTrace(); } }
codice:provaserverclient3.Busta@448139f0provaserverclient3.Busta@7ba4f24f provaserverclient3.Busta@7699a589 provaserverclient3.Busta@4dd8dc3 provaserverclient3.Busta@568db2f2 provaserverclient3.Busta@5fd0d5ae provaserverclient3.Busta@16b98e56 provaserverclient3.Busta@27d6c5e0 provaserverclient3.Busta@15aeb7ab provaserverclient3.Busta@6acbcfc0 provaserverclient3.Busta@3feba861 provaserverclient3.Busta@6f496d9f provaserverclient3.Busta@10f87f48 provaserverclient3.Busta@2f4d3709 provaserverclient3.Busta@1d81eb93 provaserverclient3.Busta@34a245ab provaserverclient3.Busta@6e8cf4c6 provaserverclient3.Busta@34c45dca BUILD SUCCESSFUL (total time: 0 seconds)

Rispondi quotando
