Skip to content

Instantly share code, notes, and snippets.

@cristofersousa
Last active October 5, 2024 13:29
Show Gist options
  • Select an option

  • Save cristofersousa/905dee7e996ec1349f4465b844be0c97 to your computer and use it in GitHub Desktop.

Select an option

Save cristofersousa/905dee7e996ec1349f4465b844be0c97 to your computer and use it in GitHub Desktop.
conexaomysql.java
package org.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConexaoMySQL {
// URL de conexão (ajustar conforme o ambiente)
// private static final String URL = "jdbc:mysql://localhost:3306/produtoDB";
private static final String URL = "jdbc:mysql://localhost:3306/produtoDB?useSSL=false&serverTimezone=UTC";
private static final String USUARIO = "root";
private static final String SENHA = "";
public static void main(String[] args) {
Connection conexao = null;
try {
// Tenta estabelecer a conexão
conexao = DriverManager.getConnection(URL, USUARIO, SENHA);
System.out.println("Conexão bem-sucedida!");
} catch (SQLException e) {
System.out.println("Falha na conexão com o banco de dados.");
e.printStackTrace();
} finally {
// Fecha a conexão se foi aberta
if (conexao != null) {
try {
conexao.close();
System.out.println("Conexão fechada.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment