Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 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 neils
  19. */
  20. public class ConnectionFactory {
  21. private final static String DRIVER="com.mysql.jdbc.Driver";
  22. private final static String URL="jdbc:mysql://localhost:3306/sigeo";
  23. private static final String USER="root";
  24. private static final String PASS="";
  25.  
  26. public static Connection getConnection(){
  27.  
  28. try {
  29. Class.forName(DRIVER);
  30. return DriverManager.getConnection(URL, USER, PASS);
  31. } catch (ClassNotFoundException | SQLException ex) {
  32. throw new RuntimeException("Erro de Conexao");
  33. }
  34. }
  35. //fecha a conexao
  36. public static void closeConnection(Connection con){
  37. try {
  38. if(con != null){
  39. con.close();
  40. }
  41. } catch (SQLException ex) {
  42. Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  43. }
  44. }
  45.  
  46. public static void closeConnection(Connection con, PreparedStatement stmt){
  47. closeConnection(con);
  48. try {
  49. if(stmt != null){
  50. stmt.close();
  51. }
  52. } catch (SQLException ex) {
  53. Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  54. }
  55. }
  56.  
  57. public static void closeConnection(Connection con, PreparedStatement stmt, ResultSet rs){
  58. closeConnection(con,stmt);
  59. try {
  60. if(rs != null){
  61. rs.close();
  62. }
  63. } catch (SQLException ex) {
  64. Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
  65. }
  66. }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement