[text] db

Viewer

  1. package com.connection;
  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. public class SingleConnection {
  10.  
  11.     private static String url = "jdbc:mysql://localhost:3306/usuariosDb";
  12.     private static String password = "";
  13.     private static String user = "root";
  14.     private static Connection connection;
  15.  
  16.     public static Connection getConnection() {
  17.         if (connection == null) {
  18.             try {
  19.                 connection = DriverManager.getConnection(url, user, password);
  20.                 connection.setAutoCommit(false);
  21.             } catch (SQLException ex) {
  22.                 ex.printStackTrace();
  23.             }
  24.         }
  25.  
  26.         return connection;
  27.     }
  28.  
  29.     public static void closeConnection() {
  30.         if (connection != null) {
  31.             try {
  32.                 connection.close();
  33.             } catch (SQLException e) {
  34.                 e.printStackTrace();
  35.             }
  36.         }
  37.     }
  38.  
  39.     public static void closeStatement(Statement statement) {
  40.         if (statement != null) {
  41.             try {
  42.                 statement.close();
  43.             } catch (SQLException e) {
  44.                 e.printStackTrace();
  45.             }
  46.         }
  47.     }
  48.  
  49.     public static void closeResultSet(ResultSet resultSet) {
  50.         if (resultSet != null) {
  51.             try {
  52.                 resultSet.close();
  53.             } catch (SQLException e) {
  54.                 e.printStackTrace();
  55.             }
  56.         }
  57.     }
  58. }
  59.  

Editor

You can edit this paste and save as new: