Can someone tell me the command to read all data out of a MySQL database? I have a table with 5 columnes, and I want to save the datas from column 2, 3, 4 and 5 in seperate Strings.
I'm pretty sure you cannot just get all data from a database. You would have to get all the data from the tables. This and this might help
Whatever library you're using to execute SQL queries should have a method to execute a query and get the results. Oracle docs
A little, but my english isn't that good that i can read a tutorial or a javadoc so i understand what I'm reading... So it would be awesome if someone could post a Code to read it out... Here's my Code to save datas: Code: package me.DaveLillo.main; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin{ static final String DB_NAME = "jdbc:mysql://localhost:3306/test_v2"; static final String USER = ""; static final String PASS = ""; Connection conn; Statement s; public void onEnable(){ try { Class.forName("com.mysql.jdbc.Driver"); //Gets the driver class // getLogger().info("About to connect to database"); //These are just for debugging purposes. conn = DriverManager.getConnection(DB_NAME, USER, PASS); //Gets a connection to the database using the details you provided. // getLogger().info("Successfully connected."); // getLogger().info("About to create a statement"); s = conn.createStatement(); //Creates a statement. You can execute queries on this. // getLogger().info("Successfully created statement."); // getLogger().info("About to add new row"); String sql = "INSERT INTO table_v2 (playername, friends) VALUES ('DaveLillo', 'LisaPotterhead')"; //This line is completely MySQL. Also, it assumes you have a table called bukkitpoints, and it has the columns specified. s.executeUpdate(sql); //Execute the mysql statement. Remember the s variable we had made earlier? This line uses that. // getLogger().info("Successfully created row."); // getLogger().info("About to close everything"); } catch(Exception ex) { ex.printStackTrace(); } } }
Code:java String query = "SELECT * FROM `table_v2`";ResultSet rs = s.executeQuery(query);while (rs.next()) { String playername = rs.getString("playername"); String friends = rs.getString("friends");} The loop is executed every time there is a row read in the results. I haven't played around with databases in a while so please excuse me if my SQL is rusty.
EDIT: Thank you, now it worked!! And how can i make that i get the results of every row where in playernames the name xyz is? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
@DaveLillo You might want something like this query: Code: String name = "xyz"; String query = "SELECT * FROM `table_v2` WHERE `playername`=`" + xyz + "`"; Hopefully that is what you meant, so it gets all the data from the rows where the playername column is xyz.