Display Java Database Values

0

I'm a beginner in Java and I'm doing a mini-RPG-style program for database testing, and I want to display the data contained in the DB using a ArrayList , however the output is always like this:

  

[rpg.Personagem@1de0aca6, rpg.Personagem@255316f2, rpg.Personagem@41906a77]

follows the class code:

 package rpg;

import java.sql.*;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

public class PersonagemDB {

    private Connection Connection;
    int ID;
    String User;
    String Nome;
    String Classe;
    int Ataque;
    int Defesa;

    public PersonagemDB() {
        this.Connection = new ConnectionDB().getConnection();
    }

    public void AdicionarPersonagem(Personagem Person){

        String sql = "INSERT INTO personagem(username, nome, classe, ataque, defessa) VALUES(?,?,?,?,?)";

        try { 

            PreparedStatement State = Connection.prepareStatement(sql);
            State.setString(1, Person.getUser());
            State.setString(2, Person.getNome());
            State.setString(3, Person.getClasse());
            State.setInt(4, Person.getAtaque());
            State.setInt(5, Person.getDefesa());
            State.execute();
            State.close();

        } catch (SQLException u) { 
            throw new RuntimeException(u);
        }

    }

    public List<Personagem> showPersonagem() throws SQLException{
        PreparedStatement State = this.Connection.prepareStatement("select * from personagem");
        ResultSet ResSet = State.executeQuery();

        List<Personagem> pers = new ArrayList<Personagem>();

        while (ResSet.next()) {

            Personagem perso = new Personagem();
            perso.setUser(ResSet.getString("username"));
            perso.setNome(ResSet.getString("nome"));
            perso.setClasse(ResSet.getString("classe"));
            perso.setAtaque(ResSet.getInt("ataque"));
            perso.setDefesa(ResSet.getInt("defessa"));

            pers.add(perso);
        }

        ResSet.close();
        State.close();

        return pers;
    }

}

The main class follows:

    package rpg;

import java.sql.Connection; 
import java.sql.ResultSet;
import java.sql.SQLException; 
import java.sql.Statement;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MainRPG {

    public static void main(String[] args) throws SQLException{

        Scanner inString = new Scanner(System.in);
        Scanner inInt = new Scanner(System.in);

        Connection connection = new ConnectionDB().getConnection();
        System.out.println("Conexão aberta!");
        connection.close();

        String User = null, Nome = null, Classe = null;
        int Ataque = 0, Defesa = 0, opc;

        Personagem Personagens = new Personagem();

        System.out.println("\n// RPG UNKNOWN //\n");

            do{
                System.out.println("\n~~ MENU ~~\n");
                System.out.println("\n1. Adicionar Personagem\t2. Listar Personagens\t 3. Fechar\n");
                opc = inInt.nextInt();

                if(opc == 1){
                    System.out.println("\nUsuário: ");
                    Personagens.setUser(inString.nextLine());

                    System.out.println("\nNome do Personagem: ");
                    Personagens.setNome(inString.nextLine());

                    System.out.println("\nClasse do Personagem: ");
                    Personagens.setClasse(inString.nextLine());

                    System.out.println("\nAtaque: ");
                    Personagens.setAtaque(inInt.nextInt());

                    System.out.println("\nDefesa: ");
                    Personagens.setDefesa(inInt.nextInt());

                    PersonagemDB DB = new PersonagemDB();
                    DB.AdicionarPersonagem(Personagens);

                } else if(opc == 2){    
                    PersonagemDB nwDB = new PersonagemDB();
                    System.out.println(nwDB.showPersonagem());
                }

        } while(opc != 3);
    }
}
    
asked by anonymous 14.06.2017 / 06:07

1 answer

0

The showPersonagem() method returns a list of characters.

When you print a list directly,       System.out.println(nwDB.showPersonagem()); , you are calling the toString() method of the Personagem object.

When this method is not overwritten, it simply prints the default value:

getClass().getName() + '@' + Integer.toHexString(hashCode())

To view information, you need to override this method in your character class.

@Override
public String toString(){
  return this.user + this.nome;
  //aqui você adiciona todas as características que serão exibidas
}
    
14.06.2017 / 18:13