MySQL read data

Discussion in 'Plugin Development' started by Leon1309, Oct 21, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Leon1309

    Hey Guys,

    i have a problem an i hope sb. can solve it :D I want to read the data of a mysql database. For example:
    Name: Age:
    Leon1309 15

    And now i want to get the age!
    How can i do it?

    Greetings Leon1309
    PS: I'm from germany -> bad egnlish
     
  2. Offline

    RingOfStorms

    Leon1309

    Well you aren't being very specific to what you want here. If you need to get the data then use something along the lines of this query:
    PHP:
    SELECT Age FROM {TableNameWHERE Name "Leon1309";
    Then use the result set you get to get the age. Also I recommend not using names as your unique identifier, and use UUID instead, as names can change in the future.
     
    FerusGrim likes this.
  3. Offline

    FerusGrim

    I'm going to assume that, because you're unfamiliar with SQL queries, you're probably unfamiliar with using SQL in conjunction with Java. Use this as an example:

    Code:java
    1. public int getAge(UUID uuid) {
    2. Connection conn = null;
    3. PreparedStatement ps = null;
    4. ResultSet rs = null;
    5. try {
    6. conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&user=" + user + "&password=" + password);
    7. ps = conn.prepareStatement("SELECT Age FROM TableName WHERE Uuid = ?;");
    8. ps.setString(1, uuid);
    9. rs = ps.executeQuery();
    10. return rs.getInt(1);
    11. } catch (SQLException e) {
    12. e.printStackTrace();
    13. } finally {
    14. if (conn != null) conn.close();
    15. if (ps != null) ps.close();
    16. if (rs != null) rs.close();
    17. }
    18. return 0;
    19. }


    Then, obviously, you would have a check to see if what is returned is 0, and do whatever you need to do in the case that a query doesn't return anything. Or, if 0 is an acceptable return state, if nothing is returned, return null instead and check for that.
     
  4. Offline

    mythbusterma

    FerusGrim

    And, of course, this should be done on a separate thread so returning the value is not the ideal option, instead it should store it in a variable visible to both threads, or use a callback.
     
    FerusGrim likes this.
Thread Status:
Not open for further replies.

Share This Page