0

That's my code, never had this problem before... Tried with toString and ArrayList<String> too but it still printing the hashcode.
I want something like a JSON, thought ArrayList was supposed to do it automatically.
I want my select into an ArrayList.

output is:

[com.firebirdangularjs.ws.rest.paises.Paises@49e4cb85, com.firebirdangularjs.ws.rest.paises.Paises@2133c8f8, com.firebirdangularjs.ws.rest.paises.Paises@43a25848]
package testebd;

            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.Statement;
            import java.util.ArrayList;
            import java.sql.ResultSet;

            import com.firebirdangularjs.ws.rest.paises.Paises;

            public class TesteDoBDAngularJS {

                public static void main(String[] args) {
                    ArrayList<Paises> listaPaises= new ArrayList<Paises>();

                    try {
                        Class.forName("org.firebirdsql.jdbc.FBDriver");
                        Connection con = DriverManager.getConnection("jdbc:firebirdsql:localhost/3050:C:\\Exercicio_Banco\\UNICAMBIO_AVS.FDB?encoding=ISO8859_1","sysdba", "password");
                        Statement st = con.createStatement();


                        String Query = "SELECT * FROM CMBPESPAIS WHERE PAISCODIGO >= 244 ";
                        System.out.println(Query  + "\n\n\n");
                        ResultSet rs = st.executeQuery(Query);
                        rs.next();

                        do {
                            String paisCodigo = rs.getString(1);

                            String nome = rs.getString(2);

                            String bacenCodigo = rs.getString(3);

                            String nacionalidade = rs.getString(4);

                            String coafPaisRestrito = rs.getString(5);

                            String codigoDeSegUsu = rs.getString(6);

                            String usudt = rs.getString(7);

                            String siglaIso = rs.getString(8);

                            String siglaIsoTr = rs.getString(9);

                            Paises pais = new Paises(paisCodigo, nome, bacenCodigo, nacionalidade, coafPaisRestrito, codigoDeSegUsu, usudt, siglaIso, siglaIsoTr);
                            listaPaises.add(pais);      
                        } while (rs.next());

                    } catch(Exception e){
                        System.out.println("OPS! Something went wrong...");
                        System.out.println(e.getMessage());
                    }
                    System.out.println(listaPaises);
                }
            }

    package com.firebirdangularjs.ws.rest.paises;

    public class Paises {
        private String NOME, NACIONALIDADE, COAFPAISRESTRITOSN, SIGLAISO, SIGLAISO3, BACENCODIGO, SEGURANCAUSUARIOCODIGO, USUARIODATAHORA, PAISCODIGO;


        public Paises(String paisCodigo2, String nOME, String bACENCODIGO, String nACIONALIDADE, String cOAFPAISRESTRITOSN,String sEGURANCAUSUARIOCODIGO, String dataUsu, String sIGLAISO, String sIGLAISO3) {
            super();
            this.NOME = nOME;
            this.NACIONALIDADE = nACIONALIDADE;
            this.COAFPAISRESTRITOSN = cOAFPAISRESTRITOSN;
            this.SIGLAISO = sIGLAISO;
            this.SIGLAISO3 = sIGLAISO3;
            this.BACENCODIGO = bACENCODIGO;
            this.SEGURANCAUSUARIOCODIGO = sEGURANCAUSUARIOCODIGO;
            this.PAISCODIGO = paisCodigo2;
            this.USUARIODATAHORA = dataUsu;
        }
        public String getPAISCODIGO() {
            return PAISCODIGO;
        }

        public void setPAISCODIGO(String pAISCODIGO) {
            PAISCODIGO = pAISCODIGO;
        }

        public String getNOME() {
            return NOME;
        }

        public void setNOME(String nOME) {
            NOME = nOME;
        }

        public String getNACIONALIDADE() {
            return NACIONALIDADE;
        }

        public void setNACIONALIDADE(String nACIONALIDADE) {
            NACIONALIDADE = nACIONALIDADE;
        }

        public String getCOAFPAISRESTRITOSN() {
            return COAFPAISRESTRITOSN;
        }

        public void setCOAFPAISRESTRITOSN(String cOAFPAISRESTRITOSN) {
            COAFPAISRESTRITOSN = cOAFPAISRESTRITOSN;
        }

        public String getSIGLAISO() {
            return SIGLAISO;
        }

        public void setSIGLAISO(String sIGLAISO) {
            SIGLAISO = sIGLAISO;
        }

        public String getSIGLAISO3() {
            return SIGLAISO3;
        }

        public void setSIGLAISO3(String sIGLAISO3) {
            SIGLAISO3 = sIGLAISO3;
        }

        public String getBACENCODIGO() {
            return BACENCODIGO;
        }

        public void setBACENCODIGO(String bACENCODIGO) {
            BACENCODIGO = bACENCODIGO;
        }

        public String getSEGURANCAUSUARIOCODIGO() {
            return SEGURANCAUSUARIOCODIGO;
        }

        public void setSEGURANCAUSUARIOCODIGO(String sEGURANCAUSUARIOCODIGO) {
            SEGURANCAUSUARIOCODIGO = sEGURANCAUSUARIOCODIGO;
        }

        public String getUSUARIODATAHORA() {
            return USUARIODATAHORA;
        }

        public void setUSUARIODATAHORA(String uSUARIODATAHORA) {
            USUARIODATAHORA = uSUARIODATAHORA;
        }


    }
israelidq
  • 5
  • 5
  • What is database schema? may it be that the columns are `BLOB` type not `VARCHAR` ? or that the charset of those columns is larger than `ISO8859_1` constraints? Also you list in your output only one single column, but you have 9 different columns in the loop? – Arioch 'The Nov 05 '19 at 13:36
  • The type is VARCHAR, and the charset isn't larger. I guess it should print all columns because i'm adding the values into a Paises Object. And on debuging i could see that pais has all values, but when I do listaPaises.add, it goes what my output shows, the hashcode... Maybe i should do the Override toString, that is on answers. – israelidq Nov 05 '19 at 14:00
  • I added my class Paises on pub now. – israelidq Nov 05 '19 at 14:07
  • 1
    see, that was crucial information about program BEHAVIOUR, I mean - telling us until which place in your program the data seemed to be correct, and at which exactly junction the behavior turns from expected to unexpected. Good that your problem seems to be typical, otherwise it would be rather hard to just guess where things went off the rail. I suggest you to read ESR's "Asking questions smart way" essay. While it is intentionally harsh, it structures one's thinking for days, when next problems would be non-typical and difficult. – Arioch 'The Nov 05 '19 at 14:08
  • Sure i'll, thanks for helping! – israelidq Nov 05 '19 at 14:13
  • Also `SELECT * FROM CMBPESPAIS` is a poor practice... As time goes by - your program might get random number of columns in random order. Unless that is the intention and your program is equipped to analyze what columns and in which order where returned, you better avoid such queries. Why not return one single string column instead? `SELECT fieldA || ' ' || fieldB || ' ' || fieldC /* ....more columns ...*/ FROM CMBPESPAIS` instead ? Actually, that later query has no account for possible `SQL NULL` values (does your code? I don't know how JavaDBC would convert NULL to string...)... – Arioch 'The Nov 05 '19 at 14:15
  • Safer way would be like `SELECT COALESCE(fieldA, '-') || ' ' || COALESCE(fieldB, '-') || ' ' || COALESCE(fieldC, '-') /* ....more columns ...*/ FROM CMBPESPAIS` . Also, take some time to read about prepared parametrized queries, instead of splashing magic constants into query texts - https://bobby-tables.com/java – Arioch 'The Nov 05 '19 at 14:17
  • Gonna read about! – israelidq Nov 05 '19 at 14:31
  • Good luck diving into this new abyss :-) If it will work out good for you, when you are less overwhelmed and ready to have your mind cracked and reshaped, consider reading Martin Gruber's "Essential SQL", if you would have to work with SQL databases. Because there you would have to make new patterns of reasoning about data flows, different from ones about code execution flow in objects and functions. If you really would have to work with SQL your way of thinking would have to change. – Arioch 'The Nov 05 '19 at 14:36
  • Additionally, `Class.forName("org.firebirdsql.jdbc.FBDriver");` - AFAIR this line is a long obsolete remnant from some really old Java/JDBC versions. Assuming your `Jaybird` and `Java` are not very ancient it should not be required. I do not remember details, Java is not my forte, but I think Jaybird documentation might have it... I don't know if it is just useless or can get harmful, I just think I saw somewhere it was suggested against it... – Arioch 'The Nov 05 '19 at 14:41
  • noted, thank you! Removed Class.forName line and kept working. – israelidq Nov 05 '19 at 14:43

1 Answers1

1

You must override toString() function and give it expected output implementation in class Paises to achieve the objective.

Example :

class Paises {

       private String paramA;
       private String paramB;
       .....

        public String toString(){
              return paramA + " " + paramB.....;
        }

}


www.hybriscx.com
  • 1,129
  • 4
  • 22
  • So after override i execute: lista Paises.add(pais.toString()); ?I added my class Paises on pub now. – israelidq Nov 05 '19 at 14:04
  • 1
    @israelidq your question text says `ArrayList` but your question code says `ArrayList` - and those are very different types. Which one is the real one for your purposes? – Arioch 'The Nov 05 '19 at 14:11
  • Yeah, i was trying to say that i used String when I was using toString, but without override the method. And tried too, with a ArrayList of my class, and none of them worked... I would prefer Paises than String – israelidq Nov 05 '19 at 14:16
  • @israelidq which means you have two pumping/conversions there: from database and JavaDBC into `Paises` objects, then from `Paises` objects into console log that you read. And you have to determine WHICH of those conversions was wrong, and which was not a problem, thus was just cluttering your thinking. For example, why not create a perfectly good `Paise` object with known data like "Aaaa", "Bbbbb", etc and insert it into the array (thus, bypassing the conversion #1) just to see, if it will be logged ok or not (thus, directly testing the conversion #2 by known data) ? DIVIDE ET EMPERA :-D – Arioch 'The Nov 05 '19 at 14:23