Read all Data

Discussion in 'Plugin Development' started by DaveLillo, Jan 31, 2015.

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

    DaveLillo

    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.
     
  2. Online

    timtower Administrator Administrator Moderator

    @DaveLillo Select all, then loop through the records?
     
  3. Offline

    tommykins20

    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
     
  4. Offline

    DaveLillo

    What would be the Code? I have one to enter the names in the table but none to read it out...
     
  5. Offline

    tommykins20

    Whatever library you're using to execute SQL queries should have a method to execute a query and get the results.
    Oracle docs
     
  6. Online

    timtower Administrator Administrator Moderator

  7. Offline

    DaveLillo

    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();
                }
        }
       
    }
    
     
  8. Offline

    tommykins20

    Code:java
    1.  
    2. String query = "SELECT * FROM `table_v2`";
    3. ResultSet rs = s.executeQuery(query);
    4. while (rs.next()) {
    5. String playername = rs.getString("playername");
    6. String friends = rs.getString("friends");
    7. }
    8.  

    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.
     
  9. Offline

    DaveLillo

    I'll check :)
     
  10. Offline

    tommykins20

    Sorry, ignore this message. Can't seem to delete this post.
     
  11. Offline

    DaveLillo

    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.
     
    Last edited by a moderator: Jun 13, 2016
    tommykins20 likes this.
  12. Offline

    tommykins20

    @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.
     
    Last edited: Jan 31, 2015
    DaveLillo likes this.
Thread Status:
Not open for further replies.

Share This Page