Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.slimetwitch.mysql;
- import java.io.File;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.HashMap;
- import org.sqlite.SQLiteConfig;
- import org.sqlite.SQLiteConfig.Encoding;
- import org.sqlite.SQLiteConfig.JournalMode;
- import cn.nukkit.utils.Config;
- import cn.nukkit.utils.LogLevel;
- import ru.slimetwitch.core.FOCraft;
- public class Connector {
- private Connection connection;
- private String url;
- private String user;
- private String passwd;
- private HashMap<String, PreparedStatement> preparedStatements;
- private boolean autoCommit;
- /**
- * Creates new SQLite object, without opening connection
- * @param path Path to database file, ex: "plugins/YourPlugin/data.db"
- */
- public Connector(String path, String user, String pass) {
- this.url = path;
- this.user = user;
- this.passwd = pass;
- this.autoCommit = false;
- }
- /**
- * Open connection
- * @return
- */
- public boolean openConnection() {
- preparedStatements = new HashMap<>();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- SQLiteConfig config = new SQLiteConfig();
- config.setEncoding(Encoding.UTF8);
- config.setJournalMode(JournalMode.OFF);
- config.setCacheSize(4);
- connection = DriverManager.getConnection("jdbc:mysql://" + url, user, passwd);
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error connecting to database: '" + url + "'");
- ex.printStackTrace();
- return false;
- }
- catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- }
- /**
- * Close connection
- * @return
- */
- public boolean closeConnection() {
- try {
- if (connection != null) connection.close();
- return true;
- } catch (Exception ex) {
- log(LogLevel.WARNING, "Error closing connection.");
- ex.printStackTrace();
- return false;
- }
- }
- /**
- * Executes SQL update
- * @param sql SQL code
- * @return true if all is okay, false if error occured
- */
- public boolean executeUpdate(String sql) {
- Statement statement = null;
- try {
- statement = connection.createStatement();
- statement.executeUpdate(sql);
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
- ex.printStackTrace();
- return false;
- } catch (NullPointerException ex) {
- log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
- log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
- + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
- return false;
- }
- }
- /**
- * Executes SQL query
- * @param sql SQL code
- * @return true if all is okay, false if error occured
- */
- public ResultSet executeQuery(String sql) {
- Statement statement = null;
- try {
- statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery(sql);
- if (resultSet == null || resultSet.isClosed() || !resultSet.isBeforeFirst()) return null;
- else return resultSet;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
- ex.printStackTrace();
- return null;
- } catch (NullPointerException ex) {
- log(LogLevel.WARNING, "Error executing update: '" + sql + "'");
- log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
- + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
- return null;
- }
- }
- /**
- * Executes SQL update, you prepared earlier, without placeholders
- * @param name Name of prepared update
- * @return true if all is okay, false if error occured
- */
- public boolean executePreparedUpdate(String name) {
- if (!preparedStatements.containsKey(name)) {
- log(LogLevel.WARNING, "Update. No such prepared statement: " + name);
- return false;
- }
- PreparedStatement statement = null;
- try {
- statement = preparedStatements.get(name);
- statement.executeUpdate();
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error executing prepared update: " + name);
- ex.printStackTrace();
- return false;
- } catch (NullPointerException ex) {
- log(LogLevel.WARNING, "Error executing prepared update: " + name);
- log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
- + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
- return false;
- }
- }
- /**
- * Executes SQL query, you prepared earlier, without placeholders
- * @param name Name of prepared query
- * @return true if all is okay, false if error occured
- */
- public ResultSet executePreparedQuery(String name) {
- if (!preparedStatements.containsKey(name)) {
- log(LogLevel.WARNING, "Query. No such prepared statement: " + name);
- return null;
- }
- PreparedStatement statement = preparedStatements.get(name);
- try {
- ResultSet resultSet = statement.executeQuery();
- if (resultSet == null || resultSet.isClosed() || !resultSet.isBeforeFirst()) return null;
- else return resultSet;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error executing prepared query: " + name);
- ex.printStackTrace();
- return null;
- } catch (NullPointerException ex) {
- log(LogLevel.WARNING, "Error executing prepared query: " + name);
- log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
- + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
- return null;
- }
- }
- /**
- * Executes SQL update, you prepared earlier, including placeholders
- * @param name Name of prepared update
- * @param args Placeholders to replace '?'
- * @return true if all is okay, false if error occured
- */
- public boolean executePreparedUpdate(String name, String...args) {
- if (!preparedStatements.containsKey(name)) {
- log(LogLevel.WARNING, "Update. No such prepared statement: " + name);
- return false;
- }
- PreparedStatement statement = null;
- try {
- statement = preparedStatements.get(name);
- for (int i = 0; i < args.length; i++) statement.setString(i + 1, args[i]);
- statement.executeUpdate();
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error executing prepared update: " + name);
- ex.printStackTrace();
- return false;
- } catch (NullPointerException ex) {
- log(LogLevel.WARNING, "Error executing prepared update: " + name);
- log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
- + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
- return false;
- }
- }
- /**
- * Executes SQL query, you prepared earlier, including placeholders
- * @param name Name of prepared query
- * @param args Placeholders to replace '?'
- * @return true if all is okay, false if error occured
- */
- public ResultSet executePreparedQuery(String name, String...args) {
- if (!preparedStatements.containsKey(name)) {
- log(LogLevel.WARNING, "Query. No such prepared statement: " + name);
- return null;
- }
- PreparedStatement statement = preparedStatements.get(name);
- try {
- for (int i = 0; i < args.length; i++) statement.setString(i + 1, args[i]);
- ResultSet resultSet = statement.executeQuery();
- if (resultSet == null || resultSet.isClosed() || !resultSet.isBeforeFirst()) return null;
- else return resultSet;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error executing prepared query: " + name);
- ex.printStackTrace();
- return null;
- } catch (NullPointerException ex) {
- log(LogLevel.WARNING, "Error executing prepared query: " + name);
- log(LogLevel.WARNING, "NullPointerException. Connection is null? ("
- + (connection == null) + "). Statement is null? (" + (statement == null) + ").");
- return null;
- }
- }
- /**
- * Prepare and store statement to execute it in future
- * @param name Name of prepared statement to identify
- * @param sql SQL code
- * @return true if all is okay, false if error occured
- */
- public boolean prepareStatement(String name, String sql) {
- try {
- PreparedStatement prStatement = connection.prepareStatement(sql);
- preparedStatements.put(name, prStatement);
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error preparing statement: '" + sql + "'");
- ex.printStackTrace();
- return false;
- }
- }
- /**
- * Remove prepared statement if exists
- * @param name Name of prepared statement
- */
- public void removePreparedStatement(String name) {
- preparedStatements.remove(name);
- }
- /**
- * Enable auto-commit
- * @param set enable auto-commit?
- * @return true if all is okay, false if error occured
- */
- public boolean setAutoCommit(boolean set) {
- try {
- connection.setAutoCommit(set);
- autoCommit = set;
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error setting autocommit: " + set);
- ex.printStackTrace();
- return false;
- }
- }
- /**
- * Get auto-commit status
- * @return auto-commit status
- */
- public boolean getAutoCommit() {
- return autoCommit;
- }
- /**
- * Commit changes
- * @return true if all is okay, false if error occured
- */
- public boolean commit() {
- try {
- connection.commit();
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error committing.");
- ex.printStackTrace();
- return false;
- }
- }
- /**
- * Rollback changes
- * @return true if all is okay, false if error occured
- */
- public boolean rollback() {
- try {
- connection.rollback();
- return true;
- } catch (SQLException ex) {
- log(LogLevel.WARNING, "Error rollbacking.");
- ex.printStackTrace();
- return false;
- }
- }
- public void log(LogLevel level, String log) {
- FOCraft.plugin.getServer().getLogger().log(level, "[SQLite] " + log);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement