Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class QueryRunner {
- String hostname, port, username, password;
- int retries;
- Connection conn = null;
- Statement s = null;
- public QueryRunner(String hostname, String port, String username, String password, int retries) {
- this.hostname = hostname;
- this.port = port;
- this.username = username;
- this.password = password;
- this.retries = retries;
- }
- private void connect() {
- try {
- String url = "jdbc:mysql://" + hostname + ":" + port + "/";
- conn = DriverManager.getConnection(url, username, password);
- s = conn.createStatement();
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- }
- private boolean refresh() {
- int tries = 0;
- boolean buf = false;
- try {
- while (!(buf = conn.isClosed()) && tries < retries) {
- tries++;
- connect();
- }
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- return buf;
- }
- public boolean query(String q) {
- boolean buf = false;
- try {
- if (!refresh()) {
- ResultSet rs = s.executeQuery(q);
- buf = (rs != null);
- }
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- return buf;
- }
- public void end() {
- try {
- s.close();
- conn.close();
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement