Advertisement
MilenaComp

testeBancoDeDados

Jan 11th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. package connection;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. //import java.util.logging.Level;
  9. //import java.util.logging.Logger;
  10.  
  11. public class TesteBanco {
  12.    
  13.     private static final String DRIVER = "com.mysql.jdbc.Driver";
  14.     private static final String URL = "jdbc:mysql://localhost:3306/pizzaria";
  15.     private static final String USER = "root";
  16.     private static final String PASS = "root";
  17.    
  18.     public static Connection getConnection(){
  19.         try {
  20.             Class.forName(DRIVER);
  21.            
  22.             return DriverManager.getConnection(URL, USER, PASS);    //.getConnection(URL, USER, PASS);
  23.         } catch (ClassNotFoundException | SQLException ex) {
  24.             //Logger.getLogger(TesteBanco.class.getName()).log(Level.SEVERE, null, ex);
  25.             throw new RuntimeException("Erro na conecxão com o banco", ex);
  26.         }
  27.     }
  28.    
  29.     public static void CloseConnection(Connection con){
  30.        
  31.         if(con != null){
  32.             try {
  33.                 con.close();
  34.             } catch (SQLException ex) {
  35.                 //Logger.getLogger(TesteBanco.class.getName()).log(Level.SEVERE, null, ex);
  36.                 System.err.println("Erro: "+ex);
  37.             }
  38.         }
  39.     }
  40.    
  41.     public static void CloseConnection(Connection con, PreparedStatement statement){
  42.        
  43.         if(statement != null){
  44.             try {
  45.                 statement.close();
  46.             } catch (SQLException ex) {
  47.                 System.err.println("Erro: "+ex);            }
  48.         }
  49.         CloseConnection(con);
  50.     }
  51.    
  52.     public static void CloseConnection(Connection con, PreparedStatement statement, ResultSet rs){
  53.        
  54.         if(rs != null){
  55.             try {
  56.                 rs.close();
  57.             } catch (SQLException ex) {
  58.                 System.err.println("Erro: "+ex);
  59.             }
  60.         }
  61.         CloseConnection(con, statement);
  62.     }
  63.    
  64.     public static void main(String []args){
  65.         getConnection();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement