Guest User

Untitled

a guest
May 13th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. import com.mysql.jdbc.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.util.Properties;
  7.  
  8. /**
  9. * Simple JAVA Database Connection Class
  10. *
  11. * Date: 2018/05/08
  12. * @author sithira
  13. */
  14. public class Database
  15. {
  16.  
  17. // init database constants
  18. private static final String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
  19. private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/database_name";
  20. private static final String USERNAME = "root";
  21. private static final String PASSWORD = "";
  22. private static final String MAX_POOL = "250";
  23.  
  24. // init connection object
  25. private Connection connection;
  26.  
  27. // init properties object
  28. private Properties properties;
  29.  
  30. // init the statement
  31. private Statement statement;
  32.  
  33. // create properties
  34. private Properties getProperties()
  35. {
  36. if (properties == null)
  37. {
  38. properties = new Properties();
  39.  
  40. properties.setProperty("user", USERNAME);
  41.  
  42. properties.setProperty("password", PASSWORD);
  43.  
  44. properties.setProperty("MaxPooledStatements", MAX_POOL);
  45. }
  46.  
  47. return properties;
  48. }
  49.  
  50. /**
  51. * Connect to the database
  52. *
  53. * @return Connection
  54. */
  55. public Connection connect()
  56. {
  57. if (connection == null)
  58. {
  59. try
  60. {
  61. Class.forName(DATABASE_DRIVER);
  62.  
  63. connection = (Connection) DriverManager.getConnection(DATABASE_URL, getProperties());
  64. }
  65. catch (ClassNotFoundException | SQLException e)
  66. {
  67. e.printStackTrace();
  68. }
  69. }
  70. return connection;
  71. }
  72.  
  73. /**
  74. * Disconnect database
  75. */
  76. public void disconnect()
  77. {
  78. if (connection != null)
  79. {
  80. try
  81. {
  82. connection.close();
  83.  
  84. connection = null;
  85.  
  86. }
  87. catch (SQLException e)
  88. {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93.  
  94. /**
  95. * Return the result set when a correct SQL statement is provided
  96. *
  97. * @param query
  98. * @return
  99. * @throws SQLException
  100. */
  101. public ResultSet select(String query) throws SQLException
  102. {
  103. statement = connection.createStatement();
  104.  
  105. ResultSet resultSet = statement.executeQuery(query);
  106.  
  107. return resultSet;
  108. }
  109.  
  110. /**
  111. * Return the status when a SQL query is provided for INSERT, UPDATE or DELETE
  112. *
  113. * @param query
  114. * @return
  115. * @throws SQLException
  116. */
  117. public int createOrUpdateOrDelete(String query) throws SQLException
  118. {
  119. statement = connection.createStatement();
  120.  
  121. int result = statement.executeUpdate(query);
  122.  
  123. return result;
  124. }
  125.  
  126. }
Add Comment
Please, Sign In to add comment