[UnSolved] MySQL Problem

Discussion in 'Plugin Development' started by CursedCoder, Sep 21, 2014.

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

    CursedCoder

    As you can see im running in some trouble because i dont know how to do this. Lets say i want to return all rows ''location'' where the ''worldname'' is equal to ''world''. But i dont know how to do this, If you know how please help me out.
     
  2. Offline

    Panjab

    Check out how to integrate MySQL in Java and use this query:

    Code:
    SELECT location FROM <table> WHERE worldname = 'world'
     
  3. Offline

    CursedCoder

    I need to return XLoc, YLoc and ZLoc, Or i can put them together and return the location. And i dont know how to return this:

    Code:java
    1. ResultSet res = statement.executeQuery("SELECT " + select + " FROM " + table + " WHERE " + where +" = '" + value + "';");
    2. res.next();
     
  4. Offline

    blablubbabc

    @CursedCode

    Take a look at the methods ResultSet provides you: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
    You can specify in your select sql query which coulms you want to have included in this ResultSet and then use thoe methods of ResultSet to get the x,y,z values from it to create your Location from.
     
  5. Offline

    CursedCoder

    I got it working though, But i cant get it to work with Multiple rows that has WorldName to world
     
  6. Offline

    FabeGabeMC

    CursedCoder
    Code:
    //SQL Command
    SELECT * FROM `your_table_name` WHERE *your var*=?
    //Java
    yourPreparedStatement.setString(1, *whatever you want*);
    Probably this?
     
  7. Offline

    CursedCoder

    FabeGabeMC Actually what im trying to do is, /robbery create <storename>. This creates a stone plate under the player and store's everything in a object PLUS MySQL. Object's reset every time the server gets reloaded, restarted, stopped etc. So i have to get that information from the MySQL and store it again in the Object. But i have to get all the information with the ''WorldName'' = world. So i need to put every row where worldname is equal to world in the object again.

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  8. Offline

    Nateb1121


    You said you got it to work to get a some data, can you show us the code you're using to get/parse the data? As well as the query you're running.
     
  9. Offline

    CursedCoder

    getLocation(); :

    Code:java
    1. private Location getLocation() {
    2. int XLoc = Integer.parseInt(MySQLUtil.getSQL("XLoc", "stores", "WorldName", "world", ""));
    3. int YLoc = Integer.parseInt(MySQLUtil.getSQL("YLoc", "stores", "WorldName", "world", ""));
    4. int ZLoc = Integer.parseInt(MySQLUtil.getSQL("ZLoc", "stores", "WorldName", "world", ""));
    5. Location location = new Location(Bukkit.getWorld("world"), XLoc, YLoc, ZLoc);
    6. return location;
    7. }


    getSQL:

    Code:java
    1. public static String getSQL(String select, String table, String where, String value, String returns) {
    2. try {
    3. openConnection();
    4. Statement statement;
    5. statement = Main.getConnection().createStatement();
    6. ResultSet res = statement.executeQuery("SELECT " + select + " FROM " + table + " WHERE " + where +" = '" + value + "';");
    7. res.next();
    8. String returned = res.getString(returns);
    9. return returned;
    10. } catch (SQLException e) {
    11. // TODO Auto-generated catch block
    12. e.printStackTrace();
    13. }
    14. return null;
    15. }


    I want it to get all the locations where WorldName is equal to World
     
  10. Offline

    mythbusterma

    CursedCoder

    Perhaps you should reconsider your storage solution. Does this really need to be SQL? Why can't this be done just as well with YAML? It would likely be easier to implement, and with the way you've poorly handled SQL, likely much faster as well.
     
  11. Offline

    CursedCoder

    I cant because im gonna use more server than 1.
     
  12. Offline

    mythbusterma

    CursedCoder

    Each server is going to have a different world, and therefore, different locations. They don't need to share this data.
     
  13. Offline

    CursedCoder

    Not each server is going to have a diffrent one, Some will have the same and im too lazy to copy paste the right one on the right place.
     
  14. Offline

    mythbusterma

    CursedCoder

    It's literally one file that you would copy, and it's a whole lot easier than writing a proper SQL implementation.
     
  15. Offline

    CursedCoder

    Its something that im willing to learn, Because what if i need to do it with PlayerData im sure not gonna copy paste that every time somethings update. Like what if i need to return all playername's where donator = 'true' or something.
     
  16. Offline

    mythbusterma

    CursedCoder

    Well, fair enough. Learning experiences are good. I would start by understanding that you need to perform SQL queries on a separate thread, and go from there.
     
  17. Offline

    CursedCoder

    mythbusterma

    Im sorry i dont know what you mean, Bad english sorry x].
     
  18. Offline

    macboinc

    Use this to translate
     
Thread Status:
Not open for further replies.

Share This Page