Advertisement
hercioneto

Classe Conexao - deve ir junto ao projeto javacombanco

Dec 5th, 2023 (edited)
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4.  
  5. /**
  6.  *
  7.  * @author Professor
  8.  */
  9. public class Conexao {
  10.  
  11.     static final String JDBC_DRIVER = "org.mariadb.jdbc.Driver";
  12.     static final String DB_URL = "jdbc:mariadb://localhost:3306/bdteste";
  13.  
  14.     static final String USER = "root";
  15.     static final String PASS = "";
  16.  
  17.     public Connection conectar() {
  18.  
  19.         Connection conn = null;
  20.  
  21.         try {
  22.             conn = DriverManager.getConnection(DB_URL, USER, PASS);
  23.             //System.out.println("Conectado");
  24.  
  25.             return conn;
  26.  
  27. //conn.close();
  28.         } catch (SQLException ex) {
  29.             System.out.println("Erro " + ex);
  30.             conn = null;
  31.  
  32.         }
  33.         return conn;
  34.  
  35.     }
  36.  
  37.     public void desconectar(Connection conn) {
  38.         try {
  39.             if (conn != null && !conn.isClosed()) {
  40.                 conn.close();
  41.                 //System.out.println("Desconectou do banco de dados.");
  42.             }
  43.         } catch (SQLException ex) {
  44.             System.out.println("Não conseguiu desconectar do BD.");
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement