Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private ExecutorService executorService =Executors.newFixedThreadPool(2);
- private String host = "***";
- private String user "root";
- private String password = "****";
- private String database "databaseName";
- private int port = 3306;
- private Connection connection;
- private void connect() {
- try {
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database + "?autoReconnect=true", this.user, this.password);
- System.out.println("MySQL wird nun aufgebaut.");
- } catch (SQLException e) {
- this.connection = null;
- e.printStackTrace();
- }
- }
- public void asyncUpdate(String qry) {
- executorService.execute(() -> {
- Statement statement = null;
- try {
- statement = connection.createStatement();
- statement.executeUpdate(qry);
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- statement.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- });
- }
- public ResultSet query(String qry) {
- if (isConnected()) {
- try {
- return connection.createStatement().executeQuery(qry);
- } catch (SQLException e) {
- System.out.println("[Error] Error while getting Result" + e);
- }
- }
- return null;
- }
- private boolean isConnected() {
- if (connection != null)
- return true;
- return false;
- }
- public Connection getConnection() {
- return connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement