Guest User

Untitled

a guest
May 20th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. package xenon.sql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. /**
  10. * Deals with SQL stuffz.
  11. *
  12. * @author Colby
  13. */
  14. public class SQLEngine {
  15.  
  16. private SQLEngine() {
  17. if(true)return;
  18.  
  19. try {
  20. Class.forName("org.gjt.mm.mysql.Driver");
  21. genericConnection = getDedicatedConnection();
  22.  
  23. } catch (Exception e) {
  24. throw new RuntimeException(e);
  25. }
  26. }
  27.  
  28. /**
  29. * Used to generate a dedicated connection.
  30. */
  31. public Connection getDedicatedConnection() throws SQLException {
  32. return DriverManager.getConnection(SQL_URL, SQL_USERNAME, SQL_PASSWORD);
  33. }
  34.  
  35. /**
  36. * Used to submit a query using a generic connection. This is synchronized
  37. * internally.
  38. */
  39. public ResultSet query(String query) throws SQLException {
  40. final Object lock = genericConnection;
  41. synchronized (lock) {
  42. return genericConnection.createStatement().executeQuery(query);
  43. }
  44. }
  45.  
  46. /**
  47. * Executes an SQL command
  48. *
  49. * @param command
  50. * The command to execute
  51. * @throws SQLException
  52. * If an error occours executing the command
  53. */
  54. public void exec(String command) throws SQLException {
  55. Statement s = null;
  56. try {
  57. s = genericConnection.createStatement();
  58. s.executeUpdate(command);
  59.  
  60. } finally {
  61. if (s != null) {
  62. s.close();
  63. }
  64. }
  65. }
  66.  
  67. /**
  68. * Gets a singleton instance of SQLEngine
  69. */
  70. public static SQLEngine getSingleton() {
  71. return SINGLETON;
  72. }
  73. /**
  74. * The name of the SQL database
  75. */
  76. public static final String SQL_NAME = "sevdim_forum";
  77. /**
  78. * The password for the SQL database
  79. */
  80. public static final String SQL_PASSWORD = "yjgpn311";
  81. /**
  82. * The username for the SQL database
  83. */
  84. public static final String SQL_USERNAME = "sevdim_admin";
  85. /**
  86. * The port for the SQL database
  87. */
  88. public static final short SQL_PORT = 3306;
  89. /**
  90. * The IP for the SQL database
  91. */
  92. public static final String SQL_IP = "174.120.136.252";
  93. /**
  94. * The full URL of the databse
  95. */
  96. public static final String SQL_URL = "jdbc:mysql://" + SQL_IP + "/" + SQL_NAME;
  97. /**
  98. * A generic connection to use when you don't need a dedicated one.
  99. */
  100. private Connection genericConnection;
  101. /**
  102. * The SQLEngine singleton
  103. */
  104. private static final SQLEngine SINGLETON = new SQLEngine();
  105. }
Add Comment
Please, Sign In to add comment