Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7.  
  8. public class Database {
  9.  
  10. private Connection conn;
  11. private Statement stmt;
  12.  
  13. private String host;
  14. private String user;
  15. private String pass;
  16. private String database;
  17. private int timeout;
  18.  
  19. public Database(String host, String user, String pass, String database) {
  20. this.host = host;
  21. this.user = user;
  22. this.pass = pass;
  23. this.database = database;
  24. this.timeout = 2500;
  25. }
  26.  
  27. public Connection getConnection() {
  28. return conn;
  29. }
  30.  
  31. public Statement getStatement() {
  32. return stmt;
  33. }
  34.  
  35. public boolean init() {
  36. try {
  37. Class.forName("com.mysql.jdbc.Driver").newInstance();
  38. this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database+"?connectTimeout="+timeout, user, pass);
  39. return true;
  40. } catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  41. System.err.println("[Database] Failed to connect! Reason: "+e.getMessage().split("\n")[0]+"");
  42. return false;
  43. }
  44. }
  45.  
  46. public boolean initBatch() {
  47. try {
  48. Class.forName("com.mysql.jdbc.Driver").newInstance();
  49. this.conn = DriverManager.getConnection("jdbc:mysql://"+host+":3306/"+database+"?connectTimeout="+timeout+"&rewriteBatchedStatements=true", user, pass);
  50. return true;
  51. } catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  52. System.err.println("[Database] Failed to connect! Reason: "+e.getMessage().split("\n")[0]+"");
  53. return false;
  54. }
  55. }
  56.  
  57. public int executeUpdate(String query) {
  58. try {
  59. this.stmt = this.conn.createStatement(1005, 1008);
  60. int results = stmt.executeUpdate(query);
  61. return results;
  62. } catch (SQLException ex) {
  63. System.err.println("[Database] Update failed! Reason: "+ex.getMessage().split("\n")[0]+"");
  64. }
  65. return -1;
  66. }
  67.  
  68. public ResultSet executeQuery(String query) {
  69. try {
  70. this.stmt = this.conn.createStatement(1005, 1008);
  71. ResultSet results = stmt.executeQuery(query);
  72. return results;
  73. } catch (SQLException ex) {
  74. System.err.println("[Database] Query failed! Reason: "+ex.getMessage().split("\n")[0]+"");
  75. }
  76. return null;
  77. }
  78.  
  79. public PreparedStatement prepare(String query) throws SQLException {
  80. return conn.prepareStatement(query);
  81. }
  82.  
  83. public int getTimeout() {
  84. return timeout;
  85. }
  86.  
  87. /**
  88. * Sets the timeout of the connection in milliseconds
  89. */
  90. public void setTimeout(int timeout) {
  91. this.timeout = timeout;
  92. }
  93.  
  94. public void destroyAll() {
  95. try {
  96. conn.close();
  97. conn = null;
  98. if (stmt != null) {
  99. stmt.close();
  100. stmt = null;
  101. }
  102. } catch(Exception e) {
  103. e.printStackTrace();
  104. }
  105. }
  106.  
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement