Não tô conseguindo fazer uma pesquisa sql

2 respostas Resolvido
Vinicius_Silva2
olá estou tentando fazer um projeto, e me deparei com esse erroThu Sep 05 23:12:47 BRT 2019 WARN: Establishing SSL connection without servers identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isnt set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to false’. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table castanha.castanha doesnt exist

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)

at com.mysql.jdbc.Util.getInstance(Util.java:408)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3933)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3869)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2675)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2465)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1912)

at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2020)

at br.com.castanha.dao.ClienteDAO.buscarPorCodigo(ClienteDAO.java:68)

at br.com.castanha.dao.ClienteDAO.main(ClienteDAO.java:142)

deu erro

código fonte "package br.com.castanha.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import br.com.castanha.domain.Cliente;
import br.com.castanha.factory.ConexaoFactory;

public class ClienteDAO {

public void salvar(Cliente f) throws SQLException {

StringBuilder sql = new StringBuilder();

sql.append("INSERT INTO cliente “);

sql.append(”(nome_cli, clientecol) ");

sql.append(VALUES (?, ?));
Connection conexao = ConexaoFactory.conectar();
	
	PreparedStatement comando = conexao.prepareStatement(sql.toString());
	comando.setString(1, f.getNome_cli());
	comando.setString(2, f.getClientecol());
	
	comando.executeUpdate();
	
}

public void excluir(Cliente f) throws SQLException{
	StringBuilder sql = new StringBuilder();
	sql.append("DELETE FROM cliente ");
	sql.append("WHERE idcliente = ? ");
	
	Connection conexao = ConexaoFactory.conectar();
	
	PreparedStatement comando = conexao.prepareStatement(sql.toString());
	comando.setLong(1, f.getIdcliente());
	
	comando.executeUpdate();
}

public void editar(Cliente f) throws SQLException{
	StringBuilder sql = new StringBuilder();
	sql.append("UPDATE cliente ");
	sql.append("SET nome_cli = ? ");
	sql.append("where idcliente = ? ");
	
	Connection conexao = ConexaoFactory.conectar();
	
	PreparedStatement comando = conexao.prepareStatement(sql.toString());
	comando.setString(1, f.getNome_cli());
	comando.setLong(2, f.getIdcliente());
	
	comando.executeUpdate();
}


public Cliente buscarPorCodigo(Cliente f) throws SQLException {
	StringBuilder sql = new StringBuilder();
	sql.append("SELECT idcliente, nome_cli ");
	sql.append("FROM castanha ");
	sql.append("WHERE idcliente = ? ");
	
	Connection conexao = ConexaoFactory.conectar();
	
	PreparedStatement comando = conexao.prepareStatement(sql.toString());
	comando.setLong(1, f.getIdcliente());
	
	ResultSet resultado = comando.executeQuery();
	
	Cliente retorno = null;
	
	if(resultado.next()) {
		retorno = new Cliente();
		retorno.setIdcliente(resultado.getLong("idcliente"));
		retorno.setNome_cli(resultado.getString("nome_cli"));
	}
	return retorno;
}
public static void main(String[] args) throws SQLException {
	/*
	Cliente f1 = new Cliente();
	f1.setNome_cli("vinicius");
	
	Cliente f2 = new Cliente();
	f2.setClientecol("989073599");
	
	
	ClienteDAO fdao = new ClienteDAO();
	
	try {
		fdao.salvar(f1);
		fdao.salvar(f2);
		System.out.println("OS fablicante foram salvo salvo");
	} catch (SQLException e) {
		System.out.println("ocorreu um erro foi triste");
		// TODO Auto-generated catch block
		e.printStackTrace();
	} */
	
	/*Cliente f1 = new Cliente();
	f1.setIdcliente(1L);
	
	Cliente f2 = new Cliente();
	f2.setIdcliente(2L);
	
	ClienteDAO fdao = new ClienteDAO();
	
	try {
		fdao.excluir(f1);
		fdao.excluir(f2);
		System.out.println("deu certo apagou corretamente");
	} catch (SQLException e) {
		System.out.println("não deu certo, não apagou nada");
		e.printStackTrace();
	} */
	/*
	Cliente f1 = new Cliente();
	f1.setIdcliente(3L);
	f1.setNome_cli("carlos 3");
	
	ClienteDAO fdao = new ClienteDAO();
	
	try {
		fdao.editar(f1);
		System.out.println("Deu certo, editou ");
	} catch (SQLException e) {
		System.out.println("não deu certo");
		// TODO Auto-generated catch block
		e.printStackTrace();
	}*/
	
	Cliente f1 = new Cliente();
	f1.setIdcliente(1L);
	
	Cliente f2 = new Cliente();
	f2.setIdcliente(9L);
	
	ClienteDAO fdao = new ClienteDAO();
	
	
	try {
		Cliente f3 = fdao.buscarPorCodigo(f1);
		Cliente f4 = fdao.buscarPorCodigo(f2);
		
		System.out.println("Resultado 1: " + f3);
		System.out.println("deu tudo certo " + f4);
	}catch (SQLException e) {
		System.out.println("deu erro");
		e.printStackTrace();
	}
}

}"

desde já obrigado!

2 Respostas

darlan_machado
Solucao aceita

Você só consegue executar algo numa tabela se ela existir.

Vinicius_Silva2

obrigado darlan_machado

Criado 6 de setembro de 2019
Ultima resposta 27 de jan. de 2020
Respostas 2
Participantes 2