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();
}
}
Erro:
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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:981)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:339)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at br.com.ConexaoBanco.ConexaoMySQL.getConexaoMySQL(ConexaoMySQL.java:81)
at controlesincronismo.ControleSincronismo.main(ControleSincronismo.java:27)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:298)
... 16 more
Qual tem sido meu erro ? E gostaria de saber também se estas sãos as melhores praticas para me proporcionar segurança.
Agradeço a colaboração.