Advertisement
Guest User

Untitled

a guest
Mar 30th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. package dao.impl.jdbc.util;
  2.  
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.logging.Logger;
  8.  
  9. import com.mysql.jdbc.Connection;
  10.  
  11. public class JdbcUtil {
  12.  
  13. private static final Logger LOGGER = Logger.getLogger(JdbcUtil.class.getName());
  14. private static final String DRIVER = "com.mysql.jdbc.Driver";
  15. private static final String DBURL = "jdbc:mysql://localhost:3306/a1";
  16. private static final String USER = "root";
  17. private static final String PASS = "";
  18. private static Connection connection = null;
  19.  
  20. public JdbcUtil() {
  21. try {
  22. Class.forName(DRIVER);
  23. } catch (ClassNotFoundException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27.  
  28. // public ConnectionFactory() {
  29. //
  30. // }
  31. /**
  32. * method which creates the connection with the database
  33. */
  34. public static void createConnection() {
  35. try {
  36. connection = (Connection) DriverManager.getConnection(
  37. "jdbc:mysql://localhost:3306/a1?autoReconnect=true&useSSL=false", USER, PASS);
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. return;
  41. }
  42. if (connection != null)
  43. System.out.println("Good");
  44. else
  45. System.out.println("Bad");
  46. }
  47. /**
  48. * method which returns the connection created before
  49. * @return Connection
  50. */
  51. public static Connection getConnection() {
  52. return connection;
  53. }
  54. /**
  55. * method used to close a connection
  56. * @param connection connection to be closed
  57. */
  58. public static void close(Connection connection) {
  59. try {
  60. connection.close();
  61. } catch (SQLException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. }
  66. /**
  67. * method used to close a statement
  68. * @param statement statement to be closed
  69. */
  70. public static void close(Statement statement) {
  71. try {
  72. statement.close();
  73. } catch (SQLException e) {
  74. // TODO Auto-generated catch block
  75. e.printStackTrace();
  76. }
  77. }
  78. /**
  79. * method used to close a result set
  80. * @param rs result set to be closed
  81. */
  82. public static void close(ResultSet rs) {
  83. try {
  84. rs.close();
  85. } catch (SQLException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement