Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public boolean onCommand(final CommandSender sender, Command cmd, String label, String[] args) {
- if (cmd.getName().equalsIgnoreCase("testupdate")) {
- if (args.length == 0) {
- sender.sendMessage("Starting an asynchronous task.");
- new BukkitRunnable() {
- @Override
- public void run() {
- sender.sendMessage("Asynchronous task started.");
- sender.sendMessage("Starting database transacation.");
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- try {
- connection = dataSource.getConnection();
- preparedStatement = connection.prepareStatement("INSERT INTO Test (Key, Value) VALUES ('0', '10')");
- int rows = preparedStatement.executeUpdate();
- sender.sendMessage("Transaction succeeded. Inserted: " + rows);
- // WARNING! If you want to call not thread-safe API methods here, put it into a synchronous task!
- } catch (SQLException e) {
- sender.sendMessage("Transaction failed.");
- } finally {
- sender.sendMessage("Closing resources.");
- if (preparedStatement != null) {
- try {
- preparedStatement.close();
- } catch (SQLException e) {
- sender.sendMessage("Error while closing statement.");
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- sender.sendMessage("Error while closing connection.");
- }
- }
- }
- }
- }.runTaskAsynchronously(this);
- }
- } else if (cmd.getName().equalsIgnoreCase("testquery")) {
- if (args.length == 0) {
- sender.sendMessage("Starting an asynchronous task.");
- new BukkitRunnable() {
- @Override
- public void run() {
- sender.sendMessage("Asynchronous task started.");
- sender.sendMessage("Starting database transaction.");
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- try {
- connection = dataSource.getConnection();
- preparedStatement = connection.prepareStatement("SELECT * FROM Test WHERE Key = '0'");
- resultSet = preparedStatement.executeQuery();
- if (resultSet.next()) {
- sender.sendMessage("Transaction succeeded. Value: " + resultSet.getInt("Value"));
- // WARNING! If you want to call not thread-safe API methods here, put it into a synchronous task!
- } else {
- sender.sendMessage("Transaction succeeded. No value.");
- }
- } catch (SQLException e) {
- sender.sendMessage("Transaction failed.");
- } finally {
- sender.sendMessage("Closing resources.");
- if (resultSet != null) {
- try {
- resultSet.close();
- } catch (SQLException e) {
- sender.sendMessage("Error while closing result set.");
- }
- }
- if (preparedStatement != null) {
- try {
- preparedStatement.close();
- } catch (SQLException e) {
- sender.sendMessage("Error while closing statement.");
- }
- }
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- sender.sendMessage("Error while closing connection.");
- }
- }
- }
- }
- }.runTaskAsynchronously(this);
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement