Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.bobakanoosh.fnabot;
- import java.sql.*;
- /**
- * Created by Jack Englund on 8/20/2018.
- */
- public class QueryManager {
- private Connection connection;
- private Statement statement;
- public QueryManager(String host, String username, String password) {
- try {
- StringBuilder builder = new StringBuilder();
- builder.append(host);
- builder.append("?user=").append(username);
- builder.append("&password=").append(password);
- builder.append("&autoreconnect=true");
- builder.append("&useSSL=false");
- connection = DriverManager.getConnection(builder.toString());
- //connection = DriverManager.getConnection(host, username, password);
- statement = connection.createStatement();
- System.out.println("Connected to DB");
- } catch (SQLException e) { System.err.println("-" + e.getMessage());}
- }
- public ResultSet select(PreparedStatement statement) {
- try {
- return statement.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- throw new NullPointerException();
- }
- public boolean hasEntry(String id) {
- PreparedStatement statement = null;
- try {
- statement = this.getConnection().prepareStatement("SELECT discordid FROM users WHERE discordid = ?");
- statement.setString(1, id);
- ResultSet set = statement.executeQuery();
- // See if the resultset has anything in it, and return whether or not it does.
- try {
- return set.next();
- } catch (SQLException e) {System.err.println(e.getMessage());}
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (statement != null)
- statement.close();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- return false;
- }
- public int delete(String sql) {
- System.out.println(sql);
- try {
- return statement.executeUpdate(sql);
- } catch (SQLException e) {
- System.err.println(e.getMessage());
- return 0;
- }
- }
- public boolean insert(PreparedStatement statement) {
- try {
- statement.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- } finally {
- try {
- if (statement != null) {
- statement.close();
- }
- }
- catch (Exception e) {
- // log this error
- e.printStackTrace();
- }
- }
- return true;
- }
- public Connection getConnection() {
- return connection;
- }
- public Statement getStatement() {
- return statement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement