0

I'm actually have the Problem that I don't know how to get a random nickname from my Table called "nicks" where the column called "nicknames".

Here is an example what I've.

private String getNames() {
    String names= "";
    try {
        ResultSet rs = Core.getSQL().getResultSet("SELECT * FROM nicks");

        names = rs.getString("nicknames");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return names;
  }
}

Can someone pls tell me why I only get the String "" and how I can get just one random entry out of my database?

Hope that someone can help me.

CreatorBlo
  • 133
  • 1
  • 1
  • 8
  • I'm surprised you're not just running into an Exception, considering you would need to point the `ResultSet` to a row before being able to pull any information. Or maybe you are and that is the reason you only receive an empty String – Dragondraikk Jun 03 '15 at 15:25
  • Saw that I'm getting an Error: java.sql.SQLException: Before start of result set – CreatorBlo Jun 03 '15 at 15:27
  • In that case simply add an `rs.first()` or `rs.next()` and it should work. – Dragondraikk Jun 03 '15 at 15:30
  • Yeah, it is working now, but it isn't random. How can i randomize it ? – CreatorBlo Jun 03 '15 at 15:32
  • Like I said "I actually don't want to randomize it in the SQL Query. Could I randomize it in Java too ?" – CreatorBlo Jun 03 '15 at 15:36
  • Why would you want to do that though? It will be far less efficient as you will still retrieve all rows first. – Dragondraikk Jun 03 '15 at 15:40

1 Answers1

0

Have a look at this Question: How to request a random row in SQL?

In addition your try block should look something like this:

try {
    ResultSet rs = Core.getSQL().getResultSet("SELECT * FROM nicks");

    rs.next();
    names = rs.getString("nicknames");
}
Community
  • 1
  • 1
whatTheFox
  • 118
  • 1
  • 8