Advertisement
yVenus

Untitled

Nov 25th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. package conexaogames;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class ConexaoGames {
  8.     // Essa primeira parte é feita para definir as informações do banco
  9.     public Connection connection = null;
  10.     private final String DRIVER = "com.mysql.jdbc.Driver"; //Driver JDBC
  11.     private final String DBNAME = "conexaogames"; //Nome do banco de dados
  12.     private final String URL = "jdbc:mysql://localhost:3306/"+DBNAME+"?useTimezone=true&serverTimezone=UTC";
  13.     private final String LOGIN = "root"; //Login padrão
  14.     private final String SENHA = ""; //Senha padrão
  15.    
  16.    
  17.    
  18.     /* Vai ser criada duas funções para podermos usar no JFRAME
  19.         O getConnection para realizar a conexão
  20.         e o close para fechar a conexão
  21.     */
  22.    
  23.     public boolean getConnection(){ /*Ele é boolean pois iremos utilizar isso a nosso favor
  24.                                     no JFrame*/
  25.                                        
  26.         try{
  27.             Class.forName(DRIVER);    
  28.             connection = DriverManager.getConnection(URL, LOGIN, SENHA);
  29.             System.out.println("Conectou");
  30.             return true;    // Quando a conexão é feita, ele retorna true
  31.         }catch(ClassNotFoundException erro){
  32.             System.out.println("Driver não encontrado!\n"+erro.toString());
  33.             return false;    // Quando a conexão é malsucedida, ele retorna false
  34.         }catch(SQLException erro){
  35.             System.out.println("Falha ao conectar!\n"+erro.toString());
  36.             return false;    
  37.            
  38.            
  39.         }
  40.     }
  41.    
  42.     public void close(){
  43.         try{
  44.             connection.close();
  45.             System.out.println("Desconectou");
  46.         }catch(SQLException erro){
  47.            
  48.         }
  49.     }
  50.    
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement