Database Examples: [Simple]
Step 1:
Call a new DatabaseConnection:
1
2
3
4
5
6
7
8
9
10
11
12
13 | import me.vthreento.api.database.DatabaseConnect;
import org.bukkit.plugin.java.JavaPlugin;
public class VthreentoExample extends JavaPlugin
{
private DatabaseConnect connect;
public void onLoad()
{
// /-- Use DataFolder to save at 'plugins/<PluginName>/database.yml'
connect = new DatabaseConnect( getDataFolder().getPath() + "/database.yml" );
}
}
|
Step 2:
Use your DatabaseConnection, first create a table:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 | import me.vthreento.api.database.DatabaseConnect;
import org.bukkit.plugin.java.JavaPlugin;
public class VthreentoExample extends JavaPlugin
{
private DatabaseConnect connect;
public void onLoad()
{
// /-- Use DataFolder to save at 'plugins/<PluginName>/database.yml'
connect = new DatabaseConnect( getDataFolder().getPath() + "/database.yml" );
// First we select values at String[]!
String[] exampleValues = new String[]
{
"id int primary key unique auto_increment",
"example text",
};
// Create a new Table if not exists!
connect.createTable( "example", exampleValues );
}
}
|
Step 3:
Insert at table 'example'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 | import me.vthreento.api.database.DatabaseConnect;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
public class VthreentoExample extends JavaPlugin
{
private DatabaseConnect connect;
public void onLoad()
{
// /-- Use DataFolder to save at 'plugins/<PluginName>/database.yml'
connect = new DatabaseConnect( getDataFolder().getPath() + "/database.yml" );
// First we select values at String[]!
String[] exampleValues = new String[]
{
"id int primary key unique auto_increment",
"example text",
};
// Create a new Table if not exists!
connect.createTable( "example", exampleValues );
// Now we create a new Map<String, String> to insert at example table!
Map<String, String> exampleInsert = new HashMap<>( );
// /-- example = 'This is a big test bla bla bla 908uyUIN*H'
exampleInsert.put( "example", "This is a big test bla bla bla 908uyUIN*H" );
// Insert at database!
connect.insert( "example", exampleInsert );
}
}
|
Step 4:
Get value from table 'example'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | import me.vthreento.api.database.DatabaseConnect;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.HashMap;
import java.util.Map;
public class VthreentoExample extends JavaPlugin
{
private DatabaseConnect connect;
public void onLoad()
{
// [.. STEP 3 ..]
// It's time to get a value from this table :P
// /-- Get example value from table example where id like 0
String test = connect.getString( "example", "id", "0", "example" );
getLogger().info( test );
}
}
|
Config Example:
| # Usage MySQL or SQLite!
type: MySQL
# If you use SQLite this is file name :P
database: minecraft
hostname: localhost
port: 3306
username: root
password: *YUhb87943yoTH^8jn3ygh7YHJ
#If you want to change your password and ecrypted value is true, change it to false!
encrypted: false
|