BukkitSQL - A little API for getting connections with SQL.

Discussion in 'Resources' started by BlockCat, Jan 28, 2012.

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

    BlockCat

    I always hated it, that I had to configure each config file from each plugin that used SQL.
    That's why I created BukkitSQL.
    BukkitSQL is nothing to be really exited about, it has a configuration where you have to provide the:
    host, username and password.

    But, with its API (or that's what i like to call it) you can easily get a connection without having to type all the boring stuff yourself.

    <Edit by Moderator: Redacted mediafire url>
    <Edit by Moderator: Redacted mediafire url>
    I'll give an example of what it could do:

    The SQL connecting from StockCraft without BukkitSQL:

    Code:
        public static Connection connecting() {
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance ();
                conn = (Connection) DriverManager.getConnection(StockCraftPropertiesVar.url, StockCraftPropertiesVar.user, StockCraftPropertiesVar.pass);
                return conn;
            }
            catch (Exception e) {
                e.printStackTrace(System.out);
                return null;
            }
        }
    and With BukkitSQL:

    Code:
     
    public static BSQLinterface inter;
        public static Connection conn;
     
    public Connection connecting() {
            RegisteredServiceProvider<BSQLinterface> bukkitSQLProvider = plugin.getServer()
                    .getServicesManager()
                    .getRegistration(me.BlockCat.bukkitSQL.BSQLinterface.class);
     
            if(bukkitSQLProvider != null){
                inter = bukkitSQLProvider.getProvider();
                conn = inter.getConnection();
                return conn;
            }
            else
            {
                return null;
            }
       
        }
    Yes this might be a little longer.
    But it is really easy to implement, and you don't need to add extra configuration...
    it also can check if tables exist, and create them.

    that has easily changed this:
    Code:
    StockCraftDatabase.connecting();
     
        Statement statement = null;
     
        if(statement != null)
     
        {
     
            try {
     
                statement.execute("SELECT * FROM stocks");
     
                statement.execute("SELECT * FROM stockstats");
     
                statement.execute("SELECT * FROM idtable");
     
            } catch (SQLException e) {
     
                try {
     
                    statement.execute("CREATE TABLE stocks (name char(50),stockname char(50), sumpaid float, amount int)");
     
                } catch (SQLException e1) {
     
                    // TODO Auto-generated catch block
     
                    e1.printStackTrace();
     
                }
     
                try {
     
                    statement.execute("CREATE TABLE stockstats (name char(50), profit float)");
     
                } catch (SQLException e1) {
     
                    // TODO Auto-generated catch block
     
                    e1.printStackTrace();
     
                }
     
                try {
     
                    statement.execute("CREATE TABLE idtable (longid char(50), shortid char(50))");
     
                } catch (SQLException e1) {
     
                    // TODO Auto-generated catch block
     
                    e1.printStackTrace();
     
                }
     
                e.printStackTrace();
     
            }
     
        }
    to this:
    Code:
    inter = StockCraftDatabase.inter;   
            Statement statement = inter.getStatement();
            if(statement != null)
            {
                    if(inter.tableExists("stocks")){
                        inter.addTable("stocks", "name:char(50)" , "stockname:char(50)" , "sumpaid:float" , "amount:int");
                    }
                    if(inter.tableExists("stockstats")){
                        inter.addTable("stockstats", "name:char(50)","profit:float");
                    }
                    if(inter.tableExists("idtable")){
                        inter.addTable("idtable", "longid:char(50)","shortid:char(50)");
                    }
            }
    Of course, this ideal can only be brought to life if developers really use this...
    I can only dream for now.

    p.s. You can find the source here.
     
    Last edited by a moderator: Nov 11, 2016
  2. Offline

    Tauryuu

    Zino Mind giving an example for getting data from the table?
     
  3. Offline

    BlockCat

    That's normal mysql language.


    PHP:
        Statement statement = (StatementStockCraftDatabase.conn.createStatement();
                        if(
    statement != null)
                        {   
                            try {
                                
    ResultSet resultset null;
                                
    String sql "SELECT longid FROM idtable WHERE longid='"+split[1]+"'";
                                
    resultset statement.executeQuery(sql);
     
Thread Status:
Not open for further replies.

Share This Page