[Solved]MySQL - getting string record

Discussion in 'Plugin Development' started by agd555, Aug 8, 2012.

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

    agd555

    Hi, i've got a problem with getting from record a string. I trying to do it like this:
    Code:
    mysql.open();
    String a = mysql.query("SELECT `itemid` FROM `mc_banks` WHERE `owner`='" + player.getName() + "'").getString("itemid");
    mysql.close();
    but Eclipse throw error - Unhandled exception type SQLException.
    I using PatPeter's library. I don't have idea how to fix it, it should work..
     
  2. Offline

    LucasEmanuel

    Well if you know you are only going to use a MySQL server then you maybe should look at doing the connections and info retrieving yourself. It isnt hard at all and you no longer need to provide a seperate driver since its pretty much all in the latest CraftBukkit and Java libraries.

    These are exact copies from my plugin SurvivalGames:

    Getting a connection.
    Code:
    try {
                Class.forName("com.mysql.jdbc.Driver");
             
                String url = "jdbc:mysql://" + host + ":" + port + "/" + database;
             
                return DriverManager.getConnection(url, username, password);
            }
            catch (SQLException | ClassNotFoundException e) {
                logger.severe("Exception caught while connecting to database! :: " + e.getMessage());
                return null;
            }
    Inserting/Updating some values:
    Code:
    try {
                Statement stmt = this.connection.createStatement();
             
                ResultSet rs = stmt.executeQuery("SELECT * FROM " + this.playerstatstable + " WHERE playernames='" + playername + "'");
             
                if(rs.next() && rs.getString("playernames") != null) {
                    kills  = kills + rs.getInt("kills");
                    deaths = deaths + rs.getInt("deaths");
                 
                    logger.debug("Updating -- " + playername + " - Kills: " + kills + " - Deaths: " + deaths);
                    stmt.executeUpdate("UPDATE " + this.playerstatstable + " SET kills=" + kills + ", deaths=" + deaths + " WHERE playernames='" + playername + "'");
                }
                else {
                    logger.debug("Inserting -- " + playername + " - Kills: " + kills + " - Deaths: " + deaths);
                    stmt.executeUpdate("INSERT INTO " + this.playerstatstable + " VALUES('" + playername + "', " + kills + ", " + deaths + ")");
                }
             
                rs.close();
             
            } catch (SQLException e) {
                logger.severe(e.getMessage());
            }
    To put it in context, this is where the code is used:
    https://github.com/Chilinot/Surviva...nuel/survivalgames/managers/StatsManager.java
     
  3. Offline

    agd555

    I don't want to stop using library, without it I need to put before every query try/catch. :(
    Any other way?
     
  4. Offline

    LucasEmanuel

    Yea well you still seem to have to since you are not handeling that SQLException, which means that you have to surround it in a try-catch block.
     
    agd555 likes this.
  5. Offline

    agd555

    Ok, I see now it is not so hard to using try/catch block. ;)
    Thanks!
     
Thread Status:
Not open for further replies.

Share This Page