Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package connection;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17. *
  18. * @author guerr
  19. */
  20. public class connectionFactory {
  21. private final String DRIVER = "com.mysql.jdbc.Driver";
  22. private final String URL = "jdbc:mysql://localhost:3306/db_museu";
  23. private final String USER = "root";
  24. private final String PASS = "";
  25.  
  26.  
  27. public Connection getConnection(){
  28. try {
  29. Class.forName(DRIVER);
  30. return DriverManager.getConnection(URL, USER, PASS);
  31.  
  32. } catch (ClassNotFoundException | SQLException ex) {
  33. throw new RuntimeException("Erro na conexao: ",ex);
  34. }
  35. }
  36.  
  37.  
  38. public static void closeConnection(Connection con){
  39. if(con!=null){
  40. try {
  41. con.close();
  42. } catch (SQLException ex) {
  43. Logger.getLogger(connectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  44. }
  45. }
  46. }
  47.  
  48.  
  49.  
  50. public static void closeConnection(Connection con, PreparedStatement stmt) {
  51.  
  52. closeConnection(con);
  53.  
  54. try {
  55.  
  56. if (stmt != null) {
  57. stmt.close();
  58. }
  59.  
  60. } catch (SQLException ex) {
  61. Logger.getLogger(connectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  62. }
  63. }
  64.  
  65. public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs) {
  66.  
  67. closeConnection(con, stmt);
  68.  
  69. try {
  70.  
  71. if (rs != null) {
  72. rs.close();
  73. }
  74.  
  75. } catch (SQLException ex) {
  76. Logger.getLogger(connectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  77. }
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement