[Resolvido] Problemas nos Getters

2 respostas
Starkk
Boa noite pessoal, Eu estava assistindo uma video aula(do site devmedia) sobre JDBC, e após assistir fui praticar e me deparei com o seguinte problema: Eu criei uma classe para a tabela CLIENTE:
package br.edu.jdbc.dto;

public class Cliente {
		
	private Double Codigo_Cliente;
    private String Nome_Cliente;
    private String Cidade_Cliente;
    private Long Cep_Cliente;
    private String Uf_Cliente;
    private Double Ie_Cliente;
    private String Cnpj_Cliente;
    
    //Get Codigo_Cliente
	public Double getCodigo_Cliente() {
		return Codigo_Cliente;
	}
	//Set Codigo_Cliente
	public void setCodigo_Cliente(Double codigo_Cliente) {
		this.Codigo_Cliente = codigo_Cliente;
	}
	
	//Get Nome_Cliente
	public String getNome_Cliente() {
		return Nome_Cliente;
	}
	//Set Nome_Cliente
	public void setNome_Cliente(String nome_Cliente) {
		this.Nome_Cliente = nome_Cliente;
	}
	
	//Get Cidade_Cliente
	public String getCidade_Cliente() {
		return Cidade_Cliente;
	}
	//Set Cidade_Cliente
	public void setCidade_Cliente(String cidade_Cliente) {
		this.Cidade_Cliente = cidade_Cliente;
	}
	
	//Get Cep_Cliente
	public Long getCep_Cliente() {
		return Cep_Cliente;
	}
	//Set Cep_Cliente
	public void setCep_Cliente(Long cep_Cliente) {
		this.Cep_Cliente = cep_Cliente;
	}
	
	//Get Uf_Cliente
	public String getUf_Cliente() {
		return Uf_Cliente;
	}
	//Set Uf_Cliente
	public void setUf_Cliente(String uf_Cliente) {
		this.Uf_Cliente = uf_Cliente;
	}
	
	//Get Ie_Cliente
	public Double getIe_Cliente() {
		return Ie_Cliente;
	}
	//Set Ie_Cliente
	public void setIe_Cliente(Double ie_Cliente) {
		this.Ie_Cliente = ie_Cliente;
	}
	
	//Get Cnpj_Cliente
	public String getCnpj_Cliente() {
		return Cnpj_Cliente;
	}
	//Set Cnpj_Cliente
	public void setCnpj_Cliente(String cnpj_Cliente) {
		this.Cnpj_Cliente = cnpj_Cliente;
	}
    
}
E uma classe para a conexão.
package br.edu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import br.edu.jdbc.dto.Cliente;

public class ConexaoUtil {
	
	private static ConexaoUtil conexaoUtil;
	
	public static ConexaoUtil getInstance(){
		if (conexaoUtil == null){
			conexaoUtil = new ConexaoUtil();
		}
		return conexaoUtil;
	}
	
	public Connection getConnection() throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		return DriverManager.getConnection("jdbc:mysql://localhost:3306/atividade_supervisionada","root","root");
	}
	
	public void listar() throws SQLException{
		Connection connection = null;
		try {
			//Recupera o objeto connection
			connection = getConnection();
			
			//Cria uma variavel que recebe a Query
			String sql = "SELECT * FROM CLIENTE";
			
			//Abre um cursor de comunicação com o banco de dados
			PreparedStatement statement = connection.prepareStatement(sql);
			//Executa a Query e rwcupera o resultado
			ResultSet resultSet = statement.executeQuery();
			
			//Imprime os valores até o final da tabela
			while (resultSet.next()){
				System.out.print(resultSet.getDouble("CODIGO_CLIENTE"));
				System.out.print(" - ");
				System.out.println(resultSet.getString("NOME_CLIENTE"));
			}
			
		} catch (Exception e){
			e.printStackTrace();			
		}finally {
			connection.close();			
		}
	}
	
	public void inserir(Cliente cliente) throws SQLException {
		Connection connection = null;
		try {
			connection = getConnection();
			
			String sql = "INSERT INTO CLIENTE(CODIGO_CLIENTE, NOME_CLIENTE, CIDADE_CLIENTE, CEP_CLIENTE, "
					+ "UF_CLIENTE, IE_CLIENTE, CNPJ_CLIENTE) VALUES(?,?,?,?,?,?,?)";
			
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setDouble(1, Cliente.getCodigo_Cliente());
			statement.setString(2, Cliente.getNome_Cliente());
			statement.setString(3, Cliente.getCidade_Cliente());
			statement.setLong(4, Cliente.getCep_Cliente());
			statement.setString(5, Cliente.getUf_Cliente());
			statement.setDouble(6, Cliente.getIe_Cliente());
			statement.setString(7, Cliente.getCnpj_Cliente());
			
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			connection.close();
		}
		
	}
	
	public static void main(String[] args) {
		try {
			getInstance().listar();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
}

Só que nas linhas 64 a 70 da classe de conexão o Eclipse fica me mostrando erros referente a esses pedaços "Cliente.getCodigo_Cliente()":


Linha 64

Cannot make a static reference to the non-static method getCodigo_Cliente() from the type Cliente
1 quick fix available:
Change modifier of 'getCodigo.Cliente()' to 'static'

Se eu aceito a sugestão do eclipse ele altera o get de Codigo_Cliente e acrescente o static:

linha 14 da classe cliente
public static Double getCodigo_Cliente() {
		return Codigo_Cliente;
	}

E depois pede para eu alterar a declaração da variável Codigo_Cliente para:

linha 5 da classe cliente
private static Double Codigo_Cliente;

Erro some, mas eu altero a estrutura dos Getters, e na video aula o professor nao precisava fazer essas alterações para o codigo rodar.

Se alguem souber onde estou errando e puder me ajudar eu agradeço.

Obs: uso o "Eclipse IDE for Java EE Developers Eclipse IDE for Java EE Developers" no Ubuntu 13.04.

2 Respostas

D

Pelo que estou entendendo de seu código, o erro está na variável que você está utilizando que é do tipo Cliente e não o parâmetro recebido pelo seu método inserir.

A diferença sutil aí é o uso Cliente vs cliente (Classe vs parâmetro - o primeiro tem a letra C maiúscula é o tipo do objeto, já o segundo é uma variável do tipo Cliente).

Talvez não existisse essa confusão, se o parâmetro “cliente” não tivesse o mesmo nome do tipo do objeto.

Como você está usando o Tipo do objeto, o Eclipse está correto na sugestão de mudar os métodos para estáticos, como você descreveu.

Starkk

Entendi, eu realmente estava chamando o Objeto no lugar da variável.
Foi só trocar que resolveu o problema.
Obrigado pela ajuda.

Criado 30 de junho de 2013
Ultima resposta 30 de jun. de 2013
Respostas 2
Participantes 2