Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package livraria;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class ConexaoBD {
- // Essa primeira parte é feita para definir as informações do banco
- public Connection connection = null;
- private final String DRIVER = "com.mysql.jdbc.Driver"; //Driver JDBC
- private final String DBNAME = "livraria"; //Nome do banco de dados
- private final String URL = "jdbc:mysql://localhost:3306/"+DBNAME+"?useTimezone=true&serverTimezone=UTC";
- private final String LOGIN = "root"; //Login padrão
- private final String SENHA = "Juancl333"; //Senha padrão
- /* Vai ser criada duas funções para podermos usar no JFRAME
- O getConnection para realizar a conexão
- e o close para fechar a conexão
- */
- public boolean getConnection(){ /*Ele é boolean pois iremos utilizar isso a nosso favor
- no JFrame*/
- try{
- Class.forName(DRIVER);
- connection = DriverManager.getConnection(URL, LOGIN, SENHA);
- System.out.println("Conectou");
- return true; // Quando a conexão é feita, ele retorna true
- }catch(ClassNotFoundException erro){
- System.out.println("Driver não encontrado!\n"+erro.toString());
- return false; // Quando a conexão é malsucedida, ele retorna false
- }catch(SQLException erro){
- System.out.println("Falha ao conectar!\n"+erro.toString());
- return false;
- }
- }
- public void close(){
- try{
- connection.close();
- System.out.println("Desconectou");
- }catch(SQLException erro){
- }
- }
- }
Add Comment
Please, Sign In to add comment