Use Date Field in Java with Mysql

Asked

Viewed 527 times

1

I’m having a little problem, I’m running a test using the data(Date) field in Java, and I’m having problems running. Below are the codes I’m using: 1º o Code to create table in Mysql

Create table clientes (
cli_id Integer NOT NULL AUTO_INCREMENT,
cli_nome Varchar(200),
cli_dt_inclusao Date,
cli_endereco Varchar(200),
cli_bairro Varchar(80),
cli_email Varchar(200),
cli_tel Varchar(15),
cli_cidade Varchar(200),
cli_uf Varchar(2), Primary Key (cli_id)) ENGINE = MyISAM;

2nd Class clienteDAL

public class clienteDAL extends conexaoMySQL {
public void incluirCliente(clienteDTO cliente) throws Exception
{
    //Prepara a conexão com o MySQL
    abrirBD();
    sql = "INSERT INTO clientes (cli_id, cli_nome, cli_dt_inclusao, cli_endereco, cli_bairro, cli_email, cli_tel, cli_cidade, cli_uf) VALUES (null, ?, ?, ?, ?, ?, ?, ?, ?)";
    ps = con.prepareStatement(sql);
    //Busca os valores da classe clientesDTO
    ps.setLong(1, cliente.getCli_id());
    ps.setString(2, cliente.getCli_nome());
    ps.setDate(3, (java.sql.Date) cliente.getCli_dt_inclusao());
    ps.setString(4, cliente.getCli_endereco());
    ps.setString(5, cliente.getCli_bairro());
    ps.setString(6, cliente.getCli_email());
    ps.setString(7, cliente.getCli_tel());
    ps.setString(8, cliente.getCli_cidade());
    ps.setString(8, cliente.getCli_uf());
    ps.execute();
    fecharBD();
}

3rd Class clienteDTO

public class clienteDTO {
    private long cli_id;
    private String cli_nome;
    private Date cli_dt_inclusao;
    private String cli_endereco;
    private String cli_bairro;
    private String cli_email;
    private String cli_tel;
    private String cli_cidade;
    private String cli_uf;

    public long getCli_id()
    {
        return cli_id;
    }    
    public void setCli_id(int cli_id)
    {
        this.cli_id = cli_id;
    } 

    public String getCli_nome()
    {
        return cli_nome;
    }    
    public void setCli_nome(String cli_nome)
    {
        this.cli_nome = cli_nome;
    }

    public Date getCli_dt_inclusao()
    {
        return this.cli_dt_inclusao;
    }    
    public void setCli_dt_inclusao(Date cli_dt_inclusao)
    {
        this.cli_dt_inclusao = cli_dt_inclusao;
    }

    public String getCli_endereco()
    {
        return cli_endereco;
    }    
    public void setCli_endereco(String cli_endereco)
    {
        this.cli_endereco = cli_endereco;
    }

    public String getCli_bairro()
    {
        return cli_bairro;
    }    
    public void setCli_bairro(String cli_bairro)
    {
        this.cli_bairro = cli_bairro;
    }

    public String getCli_email()
    {
        return cli_email;
    }
    public void setCli_email(String cli_email)
    {
        this.cli_email = cli_email;
    }    

    public String getCli_tel()
    {
        return cli_tel;
    }
    public void setCli_tel(String cli_tel)
    {
        this.cli_tel = cli_tel;
    }

    public String getCli_cidade()
    {
        return cli_cidade;
    }
    public void setCli_cidade(String cli_cidade)
    {
        this.cli_cidade = cli_cidade;
    }

    public String getCli_uf()
    {
        return cli_uf;
    }
    public void setCli_uf(String cli_uf)
    {
        this.cli_uf = cli_uf;
    }

}

4th Test Class to Include

public class TesteIncluir {
    public static void main(String[] args) throws Exception
    {
        clienteDTO cliente = new clienteDTO();
        cliente.setCli_nome("Teste");
        cliente.setCli_dt_inclusao(new java.sql.Date(System.currentTimeMillis()));
        cliente.setCli_endereco("Rua Teste, 15");
        cliente.setCli_bairro("Java Norte");
        cliente.setCli_email("[email protected]");
        cliente.setCli_tel("77777777");
        cliente.setCli_cidade("Java");
        cliente.setCli_uf("JV");
        clienteDAL dal = new clienteDAL();
        dal.incluirCliente(cliente);
        System.out.print("Cliente "+cliente.getCli_nome()+" Cadastrado com sucesso!");
    }

}

When I have the test class run, in Netbeans, it returns the following error:

Exception in thread "main" com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: 'Teste' for column 'cli_dt_inclusao' at row 1
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4118)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
    at br.uniplan.DAL.clienteDAL.incluirCliente(clienteDAL.java:33)
    at br.uniplan.Testes.TesteIncluir.main(TesteIncluir.java:28)
C:\Users\profe\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)

Where am I wrong? Hello, I managed to solve include, my problem is now in the item change, below my method change in DAL class:

public void alterarCliente(clienteDTO cliente) throws Exception
 {
     abrirBD();         
     sql = "UPDATE clientes SET cli_nome = ?, cli_dt_inclusao = ?, cli_endereco = ?, cli_bairro = ?, cli_email = ?, cli_tel = ?, cli_cidade = ?, cli_uf = ? WHERE cli_id = ?";
     ps = con.prepareStatement(sql);
     ps.setLong(1, cliente.getCli_id());
     ps.setString(2, cliente.getCli_nome());
     ps.setDate(3, new java.sql.Date(cliente.getCli_dt_inclusao().getTime()));
     ps.setString(4, cliente.getCli_endereco());
     ps.setString(5, cliente.getCli_bairro());
     ps.setString(6, cliente.getCli_email());
     ps.setString(7, cliente.getCli_tel());
     ps.setString(8, cliente.getCli_cidade());
     ps.setString(9, cliente.getCli_uf());
     ps.execute();
     fecharBD();
 }

Test class to Change as below:

package br.uniplan.Testes;

public class Testealterarcliente { public Static void main(String[] args) throws Exception { client DTO client = new clientDTO(); setCli_id(2) client; client.setCli_name("Test"); client.setCli_dt_inclusion(new java.sql.Date(System.currentTimeMillis())); setCli_addressco("Test Street, 77"); setCli_neighborhood client("South Java"); client.setCli_email("[email protected]"); client.setCli_tel("88888888"); setCli_city client("Java 8"); setCli_uf client ("J8"); clientDAL dal = new clientDAL(); dal.alterarCliente(client); System.out.println("Client " + client.getCli_name() + " Successfully changed!"); } }

When I have it executed there is no error, but it does not change the table. Where can I be missing?

  • Did the answer help you solve the problem? Is it possible to accept it?

1 answer

2


You are missing in the parameters of Query SQL, each "query" represents a parameter to be set in the Statement, as there is no ? for the Client id so it doesn’t need to be set in your Statement the right thing would be:

Query

INSERT INTO clientes (
cli_nome, 
cli_dt_inclusao, 
cli_endereco, 
cli_bairro, 
cli_email, 
cli_tel, 
cli_cidade,
cli_uf
VALUES (?, ?, ?, ?, ?, ?, ?, ?);

Statement

ps.setString(1, cliente.getCli_nome());
ps.setObject(2, cliente.getCli_dt_inclusao());
ps.setString(3, cliente.getCli_endereco());
ps.setString(4, cliente.getCli_bairro());
ps.setString(5, cliente.getCli_email());
ps.setString(6, cliente.getCli_tel());
ps.setString(7, cliente.getCli_cidade());
ps.setString(8, cliente.getCli_uf());
  • Good morning, dear José Anderson, very grateful, that was it, solved the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.