0
I’m a beginner in Java and I’m making a mini RPG-style program for Database Testing, and I want to display the data contained in the BD using a ArrayList
, but the result of the exhibition is always so:
[rpg. Character@1de0aca6, rpg.Character@255316f2, rpg.Character@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;
}
}
Follows the main class:
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);
}
}
Try:
ResSet.getString(0); ResSet.getString(1); ResSet.getString(2); ...
– gato
How are you showing the arraylist? by what appears to be listed the Character Object itself, not the attributes of that object. If there is a way, edit the question and put the snippet you are shows the array
– Paulo H. Hartmann
Tip: java variables are declared in lower case. Always!
– igventurelli
Another tip: you are not closing the connection with the bank. This is wrong. Always close Resultset, Preparedstatement and Connection (in that order). The connection to the bank shall be
stateless
, that is, it should be opened when it is used and closed immediately afterwards. As it stands, every time you instantiate your class a new connection will be opened. If your bank’s connection pool has 5 connections, for example, if you instantiate your class 6 times you will have problems for sure.– igventurelli
Now, on the doubt itself, post the code where iterates (where displays) the character list.
– igventurelli
Possible duplicate of Doubts about the toString() method of the Object class
– user28595
Duplicate as well:It is possible to return a variable from an object-oriented class without using a function?
– user28595
Thanks, thanks for the tips, but I tried everything and the error persists. I edited the question and added the class I sent the list
– thatsadkatyperrymeme