Advertisement
Guest User

connection

a guest
May 8th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. public class Database {
  6. private static final String URL = "jdbc://localhost/football";
  7. private static final String USER = "root";
  8. private static final String PASSWORD = "";
  9. private static Connection connection = null;
  10. private Database() { }
  11.  
  12. public static Connection getConnection() throws SQLException {
  13. if (connection == null) {
  14. createConnection();
  15. connection.setAutoCommit(false);
  16. }
  17. return connection;
  18. }
  19. public static void createConnection() {
  20. try {
  21. Class.forName("com.mysql.jdbc.Driver");
  22. connection = DriverManager.getConnection(URL, USER, PASSWORD);
  23. } catch (ClassNotFoundException e) {
  24. System.err.print("ClassNotFoundException: " + e);
  25. } catch(SQLException e) {
  26. System.err.println("SQLException: " + e);
  27. }
  28. }
  29.  
  30. public static void closeConnection() {
  31. try {
  32. connection.close();
  33. } catch (SQLException e) {
  34. System.err.print("D: " + e);
  35. }
  36. }
  37.  
  38. public static void commit() {
  39. try {
  40. connection.commit();
  41. } catch (SQLException e) {
  42. System.err.print("D: " + e);
  43. }
  44. }
  45.  
  46. public static void rollback() {
  47. try {
  48. connection.rollback();
  49. } catch (SQLException e) {
  50. System.err.print("D: " + e);
  51. }
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement