Estou aprendendo a desenvolver java e estou tentando avançar em meus estudos para entender como fazer uma conexão com banco de dados, porém está havendo algum problema que não estou conseguindo identificar.
Abaixo o erro escrito no log:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Eu criei um pacote br.com.ConexaoBanco que dentro existe um arquivo ConexaoMySQL, o pacote eu descrevo a classe que efetua a conexão. Logo abaixo o exemplo dela.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.ConexaoBanco;
/**
*
* @author hotsystems
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConexaoMySQL {
public static String status = "Não conectou...";
public ConexaoMySQL() {
}
public static java.sql.Connection getConexaoMySQL() {
Connection connection = null; //atributo do tipo Connection
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName);
String serverName = "";
String mydatabase ="";
String url = "jdbc:mysql://" + serverName + "/" + mydatabase;
String username = "";
String password = "";
connection = DriverManager.getConnection(url, username, password);
//Testa sua conexão//
if (connection != null) {
status = ("STATUS--->Conectado com sucesso!");
} else {
status = ("STATUS--->Não foi possivel realizar conexão");
}
return connection;
} catch (ClassNotFoundException e) {
System.out.println("O driver do banco de dados nao foi encontrado.");
return null;
} catch (SQLException e) {
System.out.println(e);
System.out.println("Nao foi possivel conectar ao Banco de Dados.");
return null;
}
}
public static String statusConection() {
return status;
}
public static boolean FecharConexao() {
try {
ConexaoMySQL.getConexaoMySQL().close();
return true;
} catch (SQLException e) {
return false;
}
}
public static java.sql.Connection ReiniciarConexao() {
FecharConexao();
return ConexaoMySQL.getConexaoMySQL();
}
}
Na classe main, tenho um pacote controlesincronismo que descrevo o main do programa, a seguir o exemplo dele.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package controlesincronismo;
import br.com.ConexaoBanco.ConexaoMySQL;
/**
*
* @author hotsystems
*/
public class ControleSincronismo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ConexaoMySQL con = new ConexaoMySQL();
con.getConexaoMySQL();
con.statusConection();
}
}
Qual tem sido meu erro ? E gostaria de saber também se estás sãos as melhores praticar que me proporciona segurança.
Agradeço a colaboração.