Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SQLUtil
- {
- public static Connection connection = null;
- public static ResultSet resultSet = null;
- private final String dbLocation;
- private final Plugin plugin;
- public SQLUtil(Plugin plugin, String dbLocation)
- {
- this.plugin = plugin;
- this.dbLocation = dbLocation;
- }
- public Connection openConnection()
- {
- try
- {
- if (!this.plugin.getDataFolder().mkdirs()) {
- this.plugin.getDataFolder().mkdirs();
- }
- String backend = this.plugin.getConfig().getString("backend");
- if (backend.equalsIgnoreCase("sqlite"))
- {
- Class.forName("org.sqlite.JDBC").newInstance();
- connection = DriverManager.getConnection("jdbc:sqlite://" + this.plugin.getDataFolder().getAbsolutePath() + "/" + this.dbLocation);
- }
- else if (backend.equalsIgnoreCase("mysql"))
- {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- connection = DriverManager.getConnection("jdbc:mysql://" + this.plugin.getConfig().getString("mysql.host") +
- ":" + this.plugin.getConfig().getString("mysql.port") + "/" + this.plugin.getConfig().getString("mysql.database") +
- "?useUnicode=true&characterEncoding=UTF-8&" + "user=" + this.plugin.getConfig().getString("mysql.user") +
- "&password=" + this.plugin.getConfig().getString("mysql.pass"));
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return connection;
- }
- public boolean checkConnection()
- {
- try
- {
- return !connection.isClosed();
- }
- catch (Exception var1) {}
- return false;
- }
- public void execute(String query, Object... values)
- {
- if (!checkConnection()) {
- openConnection();
- }
- PreparedStatement ps = null;
- try
- {
- ps = connection.prepareStatement(query);
- for (int i = 0; i < values.length; i++) {
- ps.setObject(i + 1, values[i]);
- }
- ps.executeUpdate();
- }
- catch (Exception var2)
- {
- var2.printStackTrace();
- }
- }
- public void executeStatement(String query)
- {
- if (!checkConnection()) {
- openConnection();
- }
- try
- {
- connection.createStatement().execute(query);
- }
- catch (Exception var2)
- {
- var2.printStackTrace();
- }
- }
- public ResultSet executeQuery(String query, Object... values)
- {
- if (!checkConnection()) {
- openConnection();
- }
- ResultSet rs = null;
- PreparedStatement ps = null;
- try
- {
- ps = connection.prepareStatement(query);
- for (int i = 0; i < values.length; i++) {
- ps.setObject(i + 1, values[i]);
- }
- rs = ps.executeQuery();
- }
- catch (Exception var3)
- {
- var3.printStackTrace();
- }
- return rs;
- }
- public void closeConnection()
- {
- try
- {
- if (connection != null) {
- connection.close();
- }
- }
- catch (Exception var1)
- {
- var1.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement