Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private String host = "";
- private String dataBase = "";
- private String user = "";
- private String password = "";
- public static Connection connection;
- private Statement statement;
- public MySQL(String host, String dataBase, String user, String password) {
- this.host = host;
- this.dataBase = dataBase;
- this.user = user;
- this.password = password;
- getConnection();
- }
- public void getConnection() {
- try {
- this.connection = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + dataBase + "?autoReconnect=true", user, password);
- this.statement = connection.createStatement();
- System.out.println("[MySQL] The connection was sucesfully created!");
- } catch (SQLException e) {
- System.out.println("[MySQL] The connection was not sucesfully created! Mistake:" + e.getMessage());
- e.printStackTrace();
- }
- }
- public void closeConnection() {
- try {
- if (connection != null) {
- connection.close();
- System.out.println("[MySQL] The connection was sucesfully closed!");
- }
- } catch (SQLException e) {
- System.out.println("[MySQL] The connection was not sucesfully closed! Mistake: " + e.getMessage());
- e.printStackTrace();
- }
- }
- public void updateTable(String querry) {
- try {
- Statement statement = connection.createStatement();
- statement.executeUpdate(querry);
- statement.close();
- } catch (SQLException e) {
- getConnection();
- System.err.println(e);
- }
- }
- public ResultSet querry(String querry) {
- ResultSet resultSet = null;
- try {
- Statement statement = connection.createStatement();
- resultSet = statement.executeQuery(querry);
- } catch (SQLException e) {
- getConnection();
- System.err.println(e);
- }
- return resultSet;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement