Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. public static String host;
  2. public static String user;
  3. public static String pass;
  4. public static int port;
  5. public static String db;
  6.  
  7. private Connection conn;
  8.  
  9. public MySQLHandler(){
  10.  
  11. }
  12.  
  13. public void closeConnection()
  14. {
  15. try
  16. {
  17. if (this.conn != null)
  18. {
  19. this.conn.close();
  20. }
  21.  
  22. }
  23. catch (SQLException e)
  24. {
  25. e.printStackTrace();
  26. }
  27. finally
  28. {
  29. this.conn = null;
  30. }
  31. }
  32.  
  33. public void closeRessources(ResultSet rs, PreparedStatement st)
  34. {
  35. if (rs != null)
  36. {
  37. try
  38. {
  39. rs.close();
  40. }
  41. catch (SQLException e)
  42. {
  43. e.printStackTrace();
  44. }
  45.  
  46. }
  47.  
  48. if (st != null)
  49. {
  50. try
  51. {
  52. st.close();
  53. }
  54. catch (SQLException e)
  55. {
  56. e.printStackTrace();
  57. }
  58. }
  59. }
  60.  
  61. public Connection getConnection()
  62. {
  63. return this.conn;
  64. }
  65.  
  66. public boolean hasConnection()
  67. {
  68. try
  69. {
  70. return (this.conn != null) || (this.conn.isValid(1));
  71. }
  72. catch (SQLException e)
  73. {
  74. }
  75. return false;
  76. }
  77.  
  78. public Connection openConnection()
  79. throws Exception
  80. {
  81. Class.forName("com.mysql.jdbc.Driver");
  82.  
  83. Connection conn = DriverManager.getConnection("jdbc:mysql://" + MySQLHandler.host + ":" + MySQLHandler.port + "/" + MySQLHandler.db, MySQLHandler.user, MySQLHandler.pass);
  84.  
  85. this.conn = conn;
  86.  
  87. return conn;
  88. }
  89.  
  90. public void plain_query(String s)
  91. {
  92. try
  93. {
  94. Connection conn = openConnection();
  95.  
  96. PreparedStatement st = null;
  97. try
  98. {
  99. st = conn.prepareStatement(s);
  100. st.executeUpdate();
  101. }
  102. catch (SQLException e)
  103. {
  104. e.printStackTrace();
  105. }
  106. finally
  107. {
  108. closeRessources(null, st);
  109. closeConnection();
  110. }
  111.  
  112. }
  113. catch (Exception e1)
  114. {
  115. e1.printStackTrace();
  116. }
  117. }
  118.  
  119. public ResultSet query(String s)
  120. {
  121. try
  122. {
  123. Connection conn = openConnection();
  124.  
  125. PreparedStatement st = null;
  126.  
  127. ResultSet rs = null;
  128. try
  129. {
  130. st = conn.prepareStatement(s);
  131. rs = st.executeQuery();
  132. }
  133. catch (SQLException e)
  134. {
  135. e.printStackTrace();
  136. }
  137.  
  138. return rs;
  139. }
  140. catch (Exception e1)
  141. {
  142. e1.printStackTrace();
  143. }
  144.  
  145.  
  146. return null;
  147. }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement