Hey Guys, i have a problem an i hope sb. can solve it 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
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 {TableName} WHERE 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.
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 public int getAge(UUID uuid) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&user=" + user + "&password=" + password); ps = conn.prepareStatement("SELECT Age FROM TableName WHERE Uuid = ?;"); ps.setString(1, uuid); rs = ps.executeQuery(); return rs.getInt(1); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) conn.close(); if (ps != null) ps.close(); if (rs != null) rs.close(); } return 0;} 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.
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.