Advertisement
Guest User

Untitled

a guest
Jul 12th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. package me.gtacraft.economy.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7.  
  8. /*
  9. * Multithreaded SQL "pooling" for running queries to and from the database server.
  10. * Reset connection every 1,000 connections or on timeout, whichever happens first.
  11. */
  12.  
  13. public class SQLConnectionThread {
  14. private static Connection con = null;
  15. public static int query_count = 0;
  16.  
  17. public static Connection getConnection() {
  18. try{
  19. if(query_count >= 1000) {
  20. if(con != null) {
  21. con.close();
  22. }
  23.  
  24. con = DriverManager.getConnection(SQLVars.formatSqlCall(SQLVars.SQL_URL), SQLVars.SQL_USER, SQLVars.SQL_PASS);
  25. query_count = 0;
  26. }
  27. if(con==null || con.isClosed()){
  28. Class.forName("com.mysql.jdbc.Driver");
  29. con = DriverManager.getConnection(SQLVars.formatSqlCall(SQLVars.SQL_URL), SQLVars.SQL_USER, SQLVars.SQL_PASS);
  30. }
  31. } catch(Exception e) {
  32. e.printStackTrace();
  33. }
  34.  
  35. query_count++;
  36. return con;
  37. }
  38.  
  39. public static ResultSet getResultSet(String query) {
  40. PreparedStatement pst = null;
  41.  
  42. try {
  43. pst = SQLConnectionThread.getConnection().prepareStatement(query);
  44. pst.execute();
  45.  
  46. ResultSet rs = pst.getResultSet();
  47. return rs;
  48. } catch(Exception err) {
  49. err.printStackTrace();
  50. return null;
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement