Visualizzazione dei risultati da 1 a 10 su 10

Discussione: Restituzione Array

Hybrid View

  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2013
    Messaggi
    158
    Ho riscritto tutto cosi:

    Modello:
    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;
        }
    
    
    
    
       
    }
    Server:

    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:

    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();
            }
            
            
        }
    Funziona solo che invece di restituirmi i dati in formato string me li restituisce con il segnaposto in memoria:
    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)

  2. #2
    Utente di HTML.it
    Registrato dal
    Oct 2014
    residenza
    Padova
    Messaggi
    361
    Quote Originariamente inviata da Nio74 Visualizza il messaggio
    Funziona solo che invece di restituirmi i dati in formato string me li restituisce con il segnaposto in memoria:
    Questo puoi facilmente risolverlo ridefinendo il metodo toString() nella classe Busta, dove restituirai una stringa per te significativa (di solito si ritorna qualcosa come una concatenazione di nome campo : valore campo, poi scegli tu come separarli e se aggiungere altro (come il nome della classe)).

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.