how to save date in java

0

I'm doing a project in java netbeans but I can not save the date in my database can you help me thanks this is the code I use:

if(txtgenerico.getText().length()==0
            ||txtcomercial.getText().length()==0||txtforma.getText().length()==0||txtconcentracion.getText().length()==0||txtcantidad.getText().length()==0||fechavenc.getDate().getDate()==0||txtmarca.getText().length()==0||txtlote.getText().length()==0){
            JOptionPane.showMessageDialog(null, "faltan datos por ingresar \n"+"Completelos Todos Por Favor" );
        }else{
            try{
                ResultSet rst;
                ConexionBD cc=new ConexionBD();
                Statement stmtr;
                try (Connection cn = cc.conexion()) {
                    stmtr = cn.createStatement();
                    rst = stmtr.executeQuery("SELECT * FROM insumos WHERE generico = '"+txtgenerico.getText()+"'");
                    if(rst.next()){
                        JOptionPane.showMessageDialog(null, " Este producto ya existe");
                    }else{
                        stmtr.executeUpdate("INSERT INTO insumos (generico, comercial, formafarma, concentracion, cantidad, fechaVenc, marca, numero_lote)"
                                + "VALUES ('"+txtgenerico.getText()+"','"+txtcomercial.getText()+"'"
                                + ",'"+txtforma.getText()+"','"+txtconcentracion.getText()+"','"+txtcantidad.getText()+"','"+fechavenc.getDate().getDate()+"','"+txtmarca.getText()+"','"+txtlote.getText()+"')");
                        JOptionPane.showMessageDialog(null, "registro exitoso");
                        limpiar(); bloquear(); 
                    }
                }
                stmtr.close();
                rst.close();

            }catch(Exception e){
                System.out.printf("Error aqui: "+ e.toString());
            }
        }

In the MYsql data base, date is a DATE . Thanks and how to save images in this same language with your route.

    
asked by Carlos Ariza 10.07.2017 в 00:23
source

1 answer

0

The best thing in these cases is to use a PreparedStatement since the same object is responsible for casting objects, in your case for the date.

An example would be like that for your case:

ConexionBD cc=new ConexionBD();
Connection con = cc.cc.conexion()

String sql = "INSERT INTO insumos (generico, comercial, formafarma, concentracion, cantidad, fechaVenc, marca, numero_lote)" 
           + "VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)";

PreparedStatement pstm = con.prepareStatement(sql);
//Se añadirian los diferentes campos en los parametros dependiendo del tipo de campo
pstm.setString(1, txtgenerico.getText());
pstm.setString(2, txtcomercial.getText());
pstm.setString(3, txtforma.getText());
pstm.setString(4, txtconcentracion.getText());
pstm.setString(5, txtcantidad.getText());
pstm.setDate(6, fechavenc.getDate());
pstm.setString(7, txtmarca.getText());
pstm.setString(8, txtlote.getText());

pstm.executeUpdate();

For numerical cases you could use pstm.setInt or pstm.setBigDecimal for example. It would be to look at the documentation of PreparedStatement for each case that you needed.

I hope I helped you! Greetings!

    
answered by 10.07.2017 в 12:32