Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Database mysql;
- private DatabaseInformation dbinfo = new DatabaseInformation();
- private String host = dbinfo.host;
- private String port = dbinfo.port;
- private String database = dbinfo.database;
- private String username = dbinfo.user;
- private String password = dbinfo.pass;
- private Connection con;
- public Connection openConnection(String database) throws SQLException {
- return DriverManager.getConnection(
- "jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false", username, password);
- }
- public Connection openConnection() throws SQLException {
- return DriverManager.getConnection(
- "jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false", username, password);
- }
- public void closeConnection(Connection c) throws SQLException {
- if (c != null && c.isClosed()) {
- c.close();
- }
- }
- public PreparedStatement prepareStatement(String qry) {
- PreparedStatement st = null;
- try {
- st = con.prepareStatement(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return st;
- }
- public void update(PreparedStatement statement) {
- try {
- statement.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- try {
- statement.close();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- } finally {
- try {
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- public boolean checkQuery(String sql) {
- try {
- return !con.createStatement().executeQuery(sql).next();
- } catch (SQLException e) {
- }
- return false;
- }
- public boolean hasConnection() {
- try {
- return !con.isClosed();
- } catch (Exception e) {
- }
- return false;
- }
- public ResultSet query(String query) {
- try {
- Statement statement = con.createStatement();
- return statement.executeQuery(query);
- }
- catch (SQLException e) {
- e.printStackTrace(); }
- return null;
- }
- public boolean update(String query) {
- try {
- Statement statement = con.createStatement();
- statement.executeUpdate(query);
- statement.close();
- return true;
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return false;
- }
- public boolean execute(PreparedStatement statement) {
- try {
- return statement.execute();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- public ResultSet query(PreparedStatement statement) {
- try {
- return statement.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement