[LIB] SQLibrary - Database wrappers for all database engines

Discussion in 'Resources' started by PatPeter, Aug 26, 2011.

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

    Kanlaki101

    After about 6 hours of messing with it, I finally got a stable connection to my MySQL database.
    But I ended up finding an old plugin source and viewed how they did it.

    I used the MySQL Connector/J 5.1.17 and a MySQL tutorial.

    Connector:
    http://dev.mysql.com/downloads/connector/j/

    Tutorial:
    http://zetcode.com/databases/mysqljavatutorial/



    This is working for now, but I'm not sure for how long.
    Will this library work better for me than the above method?
     
  2. Offline

    Celtic Minstrel

    I believe the first of those two is included in the craftbukkit.jar. :p Anyway, this library probably won't really work much better; it may, however, be a little easier to use. Don't take my word for it though, because I haven't really looked at it.
     
  3. Offline

    PatPeter

    First of all: Sorry everyone, university picked up and most of my Java has been going there along with my free time.

    That was a derp on my part, I was staring at the return statement (return null;) thinking, "I see no problem here, it's returning the variable "result", oh wait."

    Why pass to another function when you can just use conditionals?

    How about this: I'll consider making methods that can automate the creation of queries when the stupid library stops losing connection on Linux.

    "'void' type not allowed here"; I'll give you that voids are checked at compile time and nulls at runtime, but as stated, it would only be from function misuse.


    As said, I dropped PHP in the thread as food for thought, which it was: mission accomplished. If you have any specific functionality from PDO, feel free to mention it.

    Well my first priority has been to solve this stupid connection issue that I can't get my head around. After that I would then finish the tutorial program and maybe make a video.

    :( Sorry, things like this where hanging over my mind all week.

    Are you on Windows or Linux, and how did you get it working?

    http://forums.bukkit.org/threads/lib-tut-mysql-sqlite-bukkit-drivers.33849/#post-641927 Mini-tutorial?

    What do you mean when you say the above method? Are you literally including the jars from these files into your plugin?

    Link to old plugin source please?

    They're not included in the jar, they're downloaded to the ./lib folder in your main Minecraft directory.
     
  4. Offline

    HSAR

  5. Offline

    Kanlaki101

    It's good to know that you just haven't abandoned it.
    Incorporating MySQL/SQLite into a plugin is one of the only things that don't have a written or video tutorial.

    This would be so helpful.
     
  6. Offline

    DrAgonmoray

    Err.. I'm on windows, the server is on linux. I used this code onEnable:
    PHP:
    mysql = new MySQL(logprefixhost"3306"dbuserpass);
    And I use these two methods I made:
    PHP:
        public int getCredits(String player) {
            try {
                
    ResultSet res mysql.query("SELECT coins FROM credits WHERE username='"+player+"';");
                if (
    res.first()) {
                    return 
    res.getInt("coins");
                }
                return 
    0;
            } catch (
    Exception e) {
                
    log.severe(prefix+"An error occured while fetching credits for player '"+player+"'");
                return 
    0;
            }
        }

        public 
    void changeCredis(String playerint amount) {
            try {
                
    mysql.query("UPDATE credits SET coins = coins +"+amount+" WHERE username='"+player+"';");
            } catch (
    Exception e) {
                
    log.severe(prefix+"An error occured while changing credits for player '"+player+"'");
                
    e.printStackTrace();
            }
        }
    It works perfect.
     
  7. Offline

    Celtic Minstrel

    Actually, the JDBC connectors for SQLite and MySQL are included in the craftbukkit.jar. Unzip it and see for yourself if you don't believe me. :)
     
  8. Offline

    alta189

    Celtic is right, that was why you didn't need the connector for my version of the SQL Library....
    CraftBukkit has it for the built in Database handling through eBeans
     
  9. Offline

    Kanlaki101

    Well good. That means no more editing my manifest file, which I guess now has been pointless this whole time.
     
  10. Offline

    MG127

    why not adding basic functions to this?
    like:
    createTable(String tableName, List<String> columns, HashMap<String,String> columnDefs){
    String content="";
    for (int i=0;i<columns.size();i++){
    content=content+", "+columns.get(i)+" "+columnDefs.get(columns.get(i));
    }
    query("CREATE TABLE "+tableName+"("+content+");");
     
  11. Offline

    Celtic Minstrel

    Honestly, I wish it didn't have it built-in, but no-one has listened to me on that point. <_<
     
  12. Offline

    Lolmewn

    I'd really like it if the API wasn't throwing all those Exceptions, but just try - catching it in-method. =D
     
  13. Offline

    PatPeter

    How much SQL do you know independent of Java? SQL is better to learn on its own seeing that you'll use it no matter what language or API you switch to eventually.

    The problem is writing an example plugin while trying to fix the library to begin with.

    Yeah, looks like my code as well. I don't know why people are losing connection on Linux.

    I knew SQLite was included in craftbukkit.jar (org.sqlite), but where is MySQL? If it is built-in I wonder why one of my plugins uses "mysql.jar" and "mysql-connector-java-bin.jar".

    That's why I was asking Kanlaki101 what he meant when he started talking about connectors.

    Wait, what were you doing with the manifest file?

    As I told @Celtic Minstrel, I plan on adding functions like this once we fix more important problems such as keeping the connection persistent on Linux.

    Or if the built-in were coded with a higher level of abstraction (but then this library wouldn't exist now would it?).

    I've been meaning to ask @alta189 why he had all those exceptions as I could not ascertain why they were there in the first place. I removed them so hopefully we'll find out why.
     
  14. Offline

    Kanlaki101

    Was setting the file path to the jdbc connector.
    Like I said, I was following some very old and outdated guides.
     
  15. Offline

    Lolmewn

    the version with the removed exceptions, has it been uploaded yet?
    Going to test when I get back home :p
     
  16. Offline

    PatPeter

    What the hell...

    There was a message in my last post that notified everyone of 3.0.5's release, above my quote of HSAR and it's gone. It said something along the lines of:

    3.0.5 is out! (And probably broken!) Feel free to test and give feedback.

    Now I understand what you were trying to do Kanlaki101, lol.
     
  17. Offline

    Lolmewn

    Magic *poof* I guess.. :p
     
  18. Offline

    HSAR

    I've heard some people complain about disappearing posts as well...

    I forgot to reply to your question - SQL is something I'm working on right now, but I figured it would make more sense if I just messed around with some code.
     
  19. Offline

    SwearWord

    @PatPeter
    Sigh...since this isn't up on GitHub I couldn't send a pull request. For your getStatement method in DatabaseHandler you need to change all of it to .equalsIgnoreCase
     
  20. Offline

    alexh

    i should test the new release i guess
    [edit] good news it still works my end...
    im adding http requests to my plugin so... ill send ya my code for that when im done :)
     
  21. Offline

    Mr Smith

    for me everything is okay. Plugin works very well and don't lose connection.
    Thanks Peter :)
     
  22. Offline

    PatPeter

    Oh no, I did that on purpose to teach proper, capitalized SQL statements.

    Just kidding.

    ANOTHER MISSING POST! I replied to someone telling them that getStatement() was broken because it wasn't equalsIgnoreCase()... Regardless:

    Code:
    - 2011-09-13 03:01 - Fixed getStatement() by changing equals() to equalsIgnoreCase().
    I'll start adding some of the query-automation methods and building PreparedStatement into the normal methods when university settles down.
     
  23. Offline

    Lolmewn

    The forum is making fun of you :p
    *Let's see.. Ah! PatPeter posted something! *delete*
     
  24. Offline

    PatPeter

    Silly @PatPeter, MySQL support is in Bukkit! *admin deletes post*
     
  25. Offline

    Lolmewn

    hmm.. it seems it's true what ur saying cuz that post didn't get deleted.
    :O
     
  26. Offline

    alexh

    If its ok with patpeter i might write the docs for this if i have time, i have about all day on saturdays to do what i want. im also planing or finishing my http request libary and sending that to patpeter for inclusion in this libary.

    But note im busy with college though the week so dont expect much quickley :)
    and patpeter is busy with uni so well see what happens :)
     
  27. Offline

    SwearWord

    Why would HTTP requests need a wrapper or belong here?
     
  28. Offline

    PatPeter

    What docs would you write? There is already Javadoc.
     
  29. Offline

    alexh

    java docs dont tell you much i mean like wiki stuff for n00bs, ya know the basic coding what it does ect
     
  30. Offline

    Lolmewn

    Can I store Floats and Doubles in SQL libraries?
     
Thread Status:
Not open for further replies.

Share This Page