0

This is a very broad question, but I'm having trouble with the entire concept of Objects, mapping mysql table with objects, and how would I map my data, which is a resultset to that object.

So I' don't know how to go about creating an Object, mapping mysql table to that object, and storing data into that object. Let's say I have a table with the following rows:

    String sql = "CREATE TABLE IF NOT EXISTS `mocha` ( " +
            " `id` INT NOT NULL AUTO_INCREMENT ," +
            "`uuid` VARCHAR(255) NOT NULL ," +
            " `join_message` VARCHAR(255) NOT NULL ," +
            " `quit_message` VARCHAR(255) NOT NULL ," +
            " `change_points` INT NOT NULL," +
            " `last_modified` TIMESTAMP NOT NULL," +
            " PRIMARY KEY (`id`)" +
            ")";

And a method getting data:

private ResultSet getDataWithUUID(String uuid) {
    String sqlPlayer = "SELECT * FROM `mocha` WHERE `uuid` = ?";
    ResultSet rs[] = new ResultSet[1];
    try (PreparedStatement q = conn.connect().prepareStatement(sqlPlayer)) {
        q.setString(1, uuid);
        rs[0] = q.executeQuery();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return rs[0];
}

Which is bad, because, well, I shouldn't be passing around resultsets (In-fact this doesn't even work)

How do I do this properly? What classes would I create? What would the java class look like for the object?

I'm so confused and I have no idea how this works.

kinx
  • 463
  • 5
  • 12
  • 31
  • Please [refer to this tutorial](https://www.mkyong.com/jdbc/jdbc-preparestatement-example-select-list-of-the-records/) for help. You are using the `PreparedStatement` and `ResultSet` in wrong way – TuyenNTA Aug 06 '17 at 00:18
  • 3
    You are also leaking connections. You can't return `ResultSets` out of methods that close them. You have to use something else, e.g. a `Map`. – user207421 Aug 06 '17 at 00:49
  • you are correct, it is **too-broad** but more importantly it is a **duplicate**, did you stop to think *someone else has probably wanted to do this and asked this question countless times before* –  Aug 06 '17 at 04:25
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) –  Aug 06 '17 at 04:25

0 Answers0