lundi 23 novembre 2020

how to fill all array values in JSP, if they are empty

I'm doing my first WEB project for college that I just started, and I need that JSP file to send the informations to my data bank, but to send all checkbox values I need to deal with a String array variable, that variable should put the right values in the SQL comand, but it only works if all the 3 checkboxes are checked, and I know it's because I called 3 list itens in the SQL comand, so if the user only checks 2, the 3º one doesn't exist, so I tried to put something like a (If array[1] == null){put a "XXX" value on that array}, but I couldn't make it work.

I tried something like this just to put some value in the empty ones: ` String vserv1; String vserv2; String vserv3;

if(vservA[0] == null){
String vserv1 = "XXX";
}
else{
String vserv1 = vservA[0];
}

if(vservA[1] == null){
String vserv2 = "XXX";
}
else{
String vserv2 = vservA[1];
}

if(vservA[2] == null){
String vserv3 = "XXX";
}
else{
String vserv3 = vservA[2];
}

`

By now I only know some things about HTML, CSS, and just a tiny little about JavaScript and JSP.

And I'm still learning English so... I'm sorry if I wrote something wrong = (.

Thats my code:

<head>
    <link rel="shortcut icon" href="img/ico/top.ico">
    <meta name="viewport" content="device=device-width, initial-scale=1.0">
    <title>Crie sua conta Top Show Pet </title>
    <meta charset = "UTF-8">
  <!--<meta http-equiv="refresh" content="0; URL='http://localhost:9999/TopPet/Pagina-Pet/Top-Pet-(PetForm).html'"/>-->
</head>
<body>

<%@page language="java" import="java.sql.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%
  // cria as variaveis e obtem os dados digitados pelo usuario
  String[] vservA       = request.getParameterValues("checkservico");
  String vanimal        = request.getParameter("PetEsc");
  String vdia           = request.getParameter("Data");
  String vhora          = request.getParameter("Hora");


  // variaveis para acessar o banco de dados
  String banco   = "toppetcadastro";
  String usuario = "root";
  String senha   = "";
  String url     = "jdbc:mysql://localhost:3306/" + banco;
  String driver  = "com.mysql.jdbc.Driver";

  // carrega o driver na memoria
    Class.forName( driver );

    // criar variavel para conectar com banco de dados
    Connection conexao ;

  // abrir a conexao com o banco
  conexao = DriverManager.getConnection( url , usuario , senha ) ;
  String select = "SELECT cliente.cpf, cliente.Nome, pet.id from cliente join pet on cliente.CPF = pet.Dono WHERE Email = '"+session.getAttribute("user")+"' and pet.Nome = '"+vanimal+"';";

  // cria o statement
    Statement stm = conexao.createStatement() ;

  // executa o comando do SQL
  ResultSet rs = stm.executeQuery(select);
    rs.next();

    String insert = "INSERT INTO agendado (Servico, Proprietario, Nome_Dono, Animal, Nome_Animal, Dia, Hora) VALUES('"+vservA[0]+" - "+vservA[1]+" - "+vservA[2]+"','"+rs.getString("CPF")+"','"+rs.getString("Nome")+"',"+rs.getString("id")+",'"+vanimal+"','"+vdia+"','"+vhora+"');";

    // executa o comando do SQL
    stm.executeUpdate( insert ) ;

  rs.close();
    stm.close() ;
    conexao.close() ;

    out.print("<br><br>") ;
    out.print("Dados gravados com sucesso!!!") ;

    out.print("<br><br>") ;
    out.print("<a href='http://localhost:9999/TopPet/Pagina-Agendar/Agendamento.jsp'>Continuar</a>") ;
%>

</body>
</html>```



Aucun commentaire:

Enregistrer un commentaire