0% found this document useful (0 votes)
23 views2 pages

Package Connection

Classe para Conexão
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Package Connection

Classe para Conexão
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

package connection;

import java.sql.*;

public class ConexaoMySQL {

private static String MYDATABASE= "autopeca";


private static String SERVERNAME = "127.0.0.1:3306";
private static String DRIVER = "com.mysql.cj.jdbc.Driver";
private static String URL = "jdbc:mysql://" + SERVERNAME +
"/" + MYDATABASE + "?autoReconnect=true&useSSL=false";
private static String USER = "ROOT";
private static String PASS = "12345";

public static Connection iniciarConexao() {


try {
Class.forName(DRIVER);
return DriverManager.getConnection(URL, USER, PASS);
} catch (ClassNotFoundException | SQLException e) {
throw new RuntimeException("Erro na conexão: " + e);
}
}

public static void encerrarConexao(Connection connection) {


if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void encerrarConexao(Connection connection,


PreparedStatement pStatement) {
encerrarConexao(connection);
try {
pStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public static void encerrarConexao(Connection connection,


PreparedStatement pStatement, ResultSet rs) {
encerrarConexao(connection, pStatement);
try {
pStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

You might also like