Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.horus.lobby.utils;
- import lombok.Getter;
- import lombok.Setter;
- import lombok.ToString;
- import java.sql.*;
- @Getter
- @Setter
- @ToString(exclude = {"username", "password"})
- public class MySQL
- {
- private String hostname;
- private Integer port;
- private String username;
- private String password;
- private String database;
- private Connection connection;
- private Statement statement;
- private ResultSet result;
- public MySQL()
- {
- this("localhost", 3306);
- }
- public MySQL(String hostname)
- {
- this(hostname, 3306);
- }
- public MySQL(Integer port)
- {
- this("localhost", port);
- }
- public MySQL(String hostname, Integer port)
- {
- this.setHostname(hostname);
- this.setPort(port);
- }
- public Boolean connect(String username, String password, String database)
- {
- this.setUsername(username);
- this.setPassword(password);
- try {
- this.setConnection(DriverManager.getConnection("jdbc:mysql://"+ this.getHostname() + ":" + this.getPort() + "/" + this.getDatabase() + "?" +
- "user="+ this.getUsername() + "&password=" + this.getPassword()));
- return true;
- }
- catch (SQLException ex)
- {
- return false;
- }
- }
- public Boolean exec(String query)
- {
- try
- {
- this.setStatement(this.getConnection().createStatement());
- if (this.getStatement().execute(query))
- {
- this.setResult(this.getStatement().getResultSet());
- }
- return true;
- }
- catch (SQLException ex)
- {
- return false;
- }
- finally
- {
- if (this.getResult() != null)
- {
- try
- {
- this.getResult().close();
- }
- catch (SQLException sqlEx)
- {
- }
- this.setResult(null);
- }
- if (this.getStatement() != null)
- {
- try
- {
- this.getStatement().close();
- }
- catch (SQLException sqlEx)
- {
- }
- this.setStatement(null);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement