how to find dates in java in a db with jdatachooser

0

I want to store data in a db but I get an error

@Override
public List<Ausentismo> list05(Date fechai, Date fechaf, Date fechaMesA, Date fechaMesS) {


    List<Ausentismo> list05 = null;


    String sql = "SELECT AU.COD_MOTIVO, AU.CODIGO, AU.FECHA_SALIDA, AU.FECHA_RETORNO, AU.DIAS_CIA \n"
            + "FROM RH_AUSENTISMO AU WHERE FECHA_SALIDA BETWEEN '01-07-2018' AND '31-08-2018' \n"
            + "AND FECHA_RETORNO BETWEEN "+fechai+" AND '30-09-2018' AND AU.COD_MOTIVO ='LS'";
    try {

        Connection cn = db.getConnection();
        PreparedStatement st = cn.prepareStatement(sql);

that's the query

 private void btnGenerarActionPerformed(java.awt.event.ActionEvent evt) {                                           
    // TODO add your handling code here:
    Date fechaf = (Date) fechaFinal.getDate();
    Date fechai = (Date) fechaInicio.getDate();
    Date mesSig = (Date)  mesSiguiente.getDate();
    Date mesAnt = (Date)  mesAnterior.getDate();



    snl snl = new snl();
    snl.grabar_snl(fechai ,fechaf, mesSig,mesAnt,a, m);

That's where he sent from the jdchooser and the erroe that comes out is this.

  

Error: ORA-03115: unsupported network datatype or representation

I'm waiting for your help.

    
asked by Dalstron 13.12.2018 в 17:08
source

1 answer

0

Your problem is that when you concatenate the date fechai the query is corrected.
First: because you do not add single quotes and
Second: fechai will concatenate with the default date format.

I'll give you this example, so you can see the error:

Date fechai = new Date();

String sql= "SELECT * FROM RH_AUSENTISMO WHERE FECHA_RETORNO BETWEEN " + fechai + " AND '30-09-2030'";

System.out.println(sql);

Very similar to what you have, but look how the exit is:

SELECT * FROM RH_AUSENTISMO WHERE FECHA_RETORNO BETWEEN Thu Dec 13 12:20:13 COT 2018 AND '30-09-2030'

What you should do is format the date output and add the single quotes:

Date fechai = new Date();

SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
String dateString = format.format(fechai);

String sql = "SELECT * FROM RH_AUSENTISMO WHERE FECHA_RETORNO BETWEEN '" + dateString + "' AND '30-09-2030'";

And the output will be:

SELECT * FROM RH_AUSENTISMO WHERE FECHA_RETORNO BETWEEN '13-12-2030' AND '30-09-2018'

Sometimes it's better to use the TO_DATE to avoid having problems between environments, either your machine , the test, pre-production or production. But the best option is to pass the dates with parameters.

An example of TO_DATE :

SELECT * FROM RH_AUSENTISMO WHERE FECHA_RETORNO BETWEEN TO_DATE('13-12-2018','DD-MM-YYYY') AND TO_DATE('30-09-2030','DD-MM-YYYY')
    
answered by 13.12.2018 / 18:37
source