Examples
Examples
Example 1
An easy example. We want to have only a list of the names from the players, which were online on our server until now. I won't show you the plugin which can do that. I also won't explain it to you, because I will explain here the RedDataBase-API to you.
We have a class, that saves all und read the information. We name it DataBase:
- public class DataBase {
- public DataBase(){
- }
- }
Now we create all the Objekts and initialize them:
- public class DataBase {
- private RedDataBase redDataBase = new RedDataBase();
- private Table table = new Table("Playernames");
- public DataBase(){
- }
- }
After that we configure the table:
- public DataBase(){
- table.addcolumn("Names",DataValue.STRING);
- }
Now we are going to write in the table:
- public void addName(String name){
- Entry entry = new Entry();
- entry.addString("Names",name);
- redDataBase.add(table,entry);
- }
To read from the table is as easy as to write in it:
- public List<String> getNames(){
- List<String> names = new ArrayList<>();
- List<Entry> entrys = redDataBase.get(table);
- for(Entry entry:entrys){
- namen.add(e.getString("Names"));
- }
- return names;
- }
At least the can clear our table:
- public void clear(){
- redDataBase.dropDB(table);
- }
And thats all you need to use the RedDataBase-API.
Example 2
This is a more difficult example. Here I'm going to show you the changes for using multiple tables or columns.
Multiple tables
This is very easy. We only have to create another Table objekt.
- public class DataBase{
- private Table tabelle_one = new Table("One");
- private Table tabelle_two = new Table("Two");
Multiple Columns
This is also very easy.
- Add a new column to the table
- Add a new column to the entry
- Get multiple information out of the entry
- public DataBase(){
- tabelle.addcolumn("Name",DataValue.STRING);
- tabelle.addcolumn("Rang",DataValue.INTEGER);
- }
- public void addPlayer(String name, int rang){
- Entry entry = new Entry();
- entry.addString("Name",name);
- entry.addInteger("Rang",rang);
- redDataBase.add(table,entry);
- }
- public Map<String,Integer> getPlayera(){
- Map<String,Integer> players = new HashMap<>();
- List<Entry> entrys = redDataBase.get(table);
- for(Entry entry: entrys){
- players.put(entry.getString("Name"),entry.getInteger("Rang"));
- }
- return players;
- }
Comments