Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. package conectare;
  2.  
  3. import java.beans.Statement;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import java.sql.PreparedStatement;
  11.  
  12. public class ConectareBD {
  13.  
  14. private static final Logger LOGGER = Logger.getLogger(ConectareBD.class.getName());
  15. private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
  16. private static final String DBURL = "jdbc:mysql://localhost:3306/fooddelivery?autoReconnect=true&useSSL=false";//"jdbc:mysql://localhost:3306/project";
  17. private static final String USER = "root";
  18. private static final String PASS = "root";
  19.  
  20. private static ConectareBD singleInstance = new ConectareBD();
  21.  
  22. private ConectareBD() {
  23. try {
  24. Class.forName(DRIVER);
  25. } catch (ClassNotFoundException e) {
  26. //e.printStackTrace();
  27. }
  28. }
  29.  
  30. private Connection createConnection() {
  31. Connection connection = null;
  32. try {
  33. connection = DriverManager.getConnection(DBURL, USER, PASS);
  34. } catch (SQLException e) {
  35. LOGGER.log(Level.WARNING, "An error occured while trying to connect to the database");
  36. e.printStackTrace();
  37. }
  38. return connection;
  39. }
  40.  
  41. public static Connection getConnection() {
  42. return singleInstance.createConnection();
  43. }
  44.  
  45. public static void close(Connection connection) {
  46. if (connection != null) {
  47. try {
  48. connection.close();
  49. } catch (SQLException e) {
  50. LOGGER.log(Level.WARNING, "An error occured while trying to close the connection");
  51. }
  52. }
  53. }
  54.  
  55. public static void close(PreparedStatement statement) {
  56. if (statement != null) {
  57. try {
  58. statement.close();
  59. } catch (SQLException e) {
  60. LOGGER.log(Level.WARNING, "An error occured while trying to close the statement");
  61. }
  62. }
  63. }
  64.  
  65. public static void close(ResultSet resultSet) {
  66. if (resultSet != null) {
  67. try {
  68. resultSet.close();
  69. } catch (SQLException e) {
  70. LOGGER.log(Level.WARNING, "An error occured while trying to close the ResultSet");
  71. }
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement