Eu fiz a classe de conexão da seguinte forma:
package conect_DB;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conexao {
package conect_DB;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conexao {
public static java.sql.Connection conexao;
private static String servidor = "jdbc:mysql://localhost:3306/ucsal";
public static boolean conectar() {
try {
Class driverMysql = Class.forName("com.mysql.jdbc.Driver");
conexao = DriverManager.getConnection(servidor, "root", "");
if (conexao == null) {
return false;
}
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage() + " on method 'conectar' error");
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
return true;
}
public static void desconectar() {
try {
conexao.close();
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
catch (NullPointerException e) {
System.out.println("method 'close()' error. Not conection opened");
}
}
}
}