Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.cercus.adventsky.SQL;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import org.bukkit.entity.Player;
- public class SqlConnection {
- private Connection connection;
- private String urlbase,host,database,user,password;
- public SqlConnection(String urlbase, String host, String database, String user, String password) {
- this.urlbase = urlbase;
- this.host = host;
- this.database = database;
- this.user = user;
- this.password = password;
- }
- public void connection() {
- if(!isConnected()) {
- try {
- System.out.print("Connection to the database in progress...");
- connection = DriverManager.getConnection(urlbase + host + "/" + database, user, password);
- System.out.println("Connection established");
- } catch (SQLException e) {
- System.out.println("Error while connection with the database...");
- e.printStackTrace();
- }
- }
- }
- public void disconnect() {
- if(!isConnected()) {
- try {
- connection.close();
- System.out.println("Shutdown the connection to the database in progress");
- } catch (SQLException e) {
- System.out.println("Error while disconnection with the database");
- e.printStackTrace();
- }
- }
- }
- public boolean isConnected(){
- return connection != null;
- }
- public void createAccount(Player player) {
- if(!hasAccount(player)) {
- try {
- PreparedStatement q = connection.prepareStatement("INSERT INTO joueurs(pseudo, uuid, money, grades, rang) VALUES(?,?,?,?,?)");
- q.setString(1, player.getName().toString());
- q.setString(2, player.getUniqueId().toString());
- q.setInt(3, 100);
- q.setString(4, "Joueur");
- q.setString(5, "L");
- q.execute();
- q.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public boolean hasAccount(Player player) {
- try {
- PreparedStatement q = connection.prepareStatement("SELECT uuid FROM joueurs WHERE uuid = ?");
- q.setString(1, player.getUniqueId().toString());
- ResultSet resultat = q.executeQuery();
- boolean hasAccount = resultat.next();
- q.close();
- return hasAccount;
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
- }
- package fr.cercus.adventsky.SQL;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Core extends JavaPlugin implements Listener{
- public SqlConnection sql;
- public void onEnable(){
- sql = new SqlConnection("jdbc:mysql://","localhost","adventsk_serveur","adventsk_admin","Lali-ho4");
- sql.connection();
- getServer().getPluginManager().registerEvents(this, this);
- }
- public void onDisable(){
- sql.disconnect();
- }
- @EventHandler
- public void join(PlayerJoinEvent e) {
- Player p = e.getPlayer();
- //On appelle le createAccount
- sql.createAccount(p);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement