在数据库上插入DateTime值时出错

时间:2019-05-23 08:52:35

标签: java php android mysql

我在网上搜索了此内容,甚至在StackOverflow上的一些帖子中也对此进行了讨论,但从未解决我的问题。 我在将DateTime插入数据库时​​遇到问题。第一个问题是,我只知道如何从Java中生成StringRequests,因此我将日期转换为String(输出示例:“ 2000-04-23 10:25:06”)。

这是我的Java代码:

StringRequest stringRequest = new StringRequest(Request.Method.GET, testInserirPontoURL,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {}
    }) {
  @RequiresApi(api = Build.VERSION_CODES.O)
  @Override
  public Map<String, String> getHeaders() throws AuthFailureError {
      String codColab = getIntent().getStringExtra("codigo colaborador");
      LocalDate ldHoraEntrada = LocalDate.of(ponto.getEntrada().get(Calendar.YEAR), ponto.getEntrada().get(Calendar.MONTH) + 1, ponto.getEntrada().get(Calendar.DAY_OF_MONTH));
      String horaEntrada = ldHoraEntrada + " " + sdfHora2.format(ponto.getEntrada().getTime());
      LocalDate ldSaidaAlmoco = LocalDate.of(ponto.getSaidaAlmoco().get(Calendar.YEAR), ponto.getSaidaAlmoco().get(Calendar.MONTH) + 1, ponto.getSaidaAlmoco().get(Calendar.DAY_OF_MONTH));
      String saidaAlmoco = ldSaidaAlmoco + " " + sdfHora2.format(ponto.getSaidaAlmoco().getTime());
      LocalDate ldEntradaTarde = LocalDate.of(ponto.getEntradaTarde().get(Calendar.YEAR), ponto.getEntradaTarde().get(Calendar.MONTH) + 1, ponto.getEntradaTarde().get(Calendar.DAY_OF_MONTH));
      String entradaTarde = ldEntradaTarde + " " + sdfHora2.format(ponto.getEntradaTarde().getTime());
      LocalDate ldHoraSaida = LocalDate.of(ponto.getSaida().get(Calendar.YEAR), ponto.getSaida().get(Calendar.MONTH) + 1, ponto.getSaida().get(Calendar.DAY_OF_MONTH));
      String horaSaida = ldHoraSaida + " " + sdfHora2.format(ponto.getSaida().getTime());

      Map<String, String> map = new HashMap<String, String>();
      map.put("horaEntrada", horaEntrada);
      map.put("saidaAlmoco", saidaAlmoco);
      map.put("entradaTarde", entradaTarde);
      map.put("horaSaida", horaSaida);
      map.put("cod_colab", codColab);
      return map;
  }
};
RequestQueue requestQueue = Volley.newRequestQueue(PontoActivity.this);
requestQueue.add(stringRequest);

注意:codColab在这里是一个String,但在数据库中是一个int,但是它工作得很好,因为我已经使用它进行登录了。

这是我的php代码:

    if($_SERVER['REQUEST_METHOD']=='GET'){

    $horaEntrada = $_GET['horaEntrada'];
    $saidaAlmoco = $_GET['saidaAlmoco'];
    $entradaTarde = $_GET['entradaTarde'];
    $horaSaida = $_GET['horaSaida'];
    $cod_colab = $_GET['cod_colab'];

    $sql= "INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('".$horaEntrada."', '".$saidaAlmoco."', '".$entradaTarde."', '".$horaSaida."', '".$cod_colab."')";
    //$sql= "INSERT INTO `ponto` (`id`, `hora_entrada`, `saida_almoco`, `entrada_tarde`, `hora_saida`, `cod_colab`) VALUES (NULL, '2019-05-15 10:25:41', '2019-05-09 14:25:37', '2019-05-16 11:20:13', '2019-05-09 13:25:30', '1')";
    //$sql= "INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('$horaEntrada', '$saidaAlmoco', '$entradaTarde', '$horaSaida', '$cod_colab')";

    if (mysqli_query($conn, $sql)){
        echo "registado";
    } else {
        echo "erro a registar";
    }

注意:注释行是我尝试过的。第一条注释行有效,但我想要变量的值

1 个答案:

答案 0 :(得分:0)

由于$ horaSaida可以为NULL,因此输出将如下所示:

import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import org.apache.commons.codec.binary.Base64;


    String Key = "Something";
            String value = "Test";
            byte[] KeyData = Key.getBytes();
            SecretKeySpec KS = new SecretKeySpec(KeyData, "RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
            Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding");
            cipher.init(Cipher.ENCRYPT_MODE, KS);
            byte[] encrypted = cipher.doFinal(value.getBytes());
            log.info(Base64.encodeBase64String(encrypted));

和''不是正确的数据时间值

您可以通过此操作获得正确的SQL:

INSERT INTO ponto (hora_entrada,saida_almoco,entrada_tarde,hora_saida,cod_colab) VALUES ('',...)