In my database I got 4 fields of data, I also got a method (getWalletName
) where I send an SQL query to retrieve only one piece of data out of 4, but after retrieving and displaying the result. I can see the other data beside the desired one I want like this :
SELECT Name FROM wallets WHERE Location ='ROOME' ;
[unknown,unknown,unknown,Omar]
Where I want it to be displayed like that [Omar]
, keep in mind to not modify my constructors because I need them to be in this way. I have tried the code in wallet
class below but didn't work !
Any help would be much appreciated
wallet class :
public class wallet {
private String Name;
private String Location;
private String Tag;
private String Reader;
public wallet(String Name , String Location, String Tag, String Reader)
{
this.Name = Name;
this.Location = Location;
this.Tag = Tag;
this.Reader = Reader;
}
public wallet(String Reader)
{
this.Reader = Reader;
this.Name = "unknown";
this.Location = "unknown";
this.Tag = "unknown";
}
public wallet(String Location,String Tag) {
this.Location = Location;
this.Tag = Tag;
}
public String getWalletName()
{
return Name;
}
public void setWalletName(String Name)
{
this.Name = Name;
}
public String getWalletLocation()
{
return Location;
}
public void setWalletLocation(String Location)
{
this.Location = Location;
}
public String getWalletTag()
{
return Tag;
}
public void setWalletTag(String Tag)
{
this.Tag = Tag;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
if(Objects.nonNull(this.Name)) {
sb.append(this.Name+",");
}
if(Objects.nonNull(this.Location)) {
sb.append(this.Location+",");
}
if(Objects.nonNull(this.Tag)) {
sb.append(this.Tag+",");
}
if(Objects.nonNull(this.Reader)) {
sb.append(this.Reader);
}
return sb.toString();
}
}
getWalletName method:
public ArrayList<wallet> getWalletName(String Location) throws SQLException {
System.out.println("----------------");
System.out.println("Retrieve the wallet name");
//Connection dbConnection = null;
//Statement statement = null;
ResultSet resultset = null;
String query = "SELECT Name FROM wallets WHERE Location ='" + Location + "' ;";
ArrayList<wallet> WalletsList = new ArrayList<>();
try {
getConnection();
//statement = dbConnection.createStatement();
System.out.println(query);
resultset = stmt.executeQuery(query);
while (resultset.next()) {
String Name = resultset.getString("Name");
WalletsList.add(new wallet (Name));
}
}
finally {
// closeConnection();
if (resultset !=null){
resultset.close();
}
if (stmt !=null) {
stmt.close();
}
if (resultset !=null) {
closeConnection();
}
}
return WalletsList;
}