0

I have a problem
Tecno are the tags: java,php,javascript
my Recoge_datos.jsp

String[] tecno=request.getParameterValues("tecno");
try{

java.sql.Connection miConexion=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/proyecto_jsp","root","");
java.sql.Statement miStatement=miConexion.createStatement();

String instruccionSql="INSERT INTO USERS (nombre, apellido, usuario, contra, pais, tecno) VAlUES ('" + nombre + "','" + apellido +"','"+ usuario +"','"+ contra +"','" + pais +"','" + tecno + "')";

miStatement.executeUpdate(instruccionSql);

out.println(" Registrado con exito ");
}catch(Exception e){
out.println("Ha habido un error");
}

and my formulario_registro.html

<form action="Recoge_datos.jsp" method="post">
<tr>
  <td>Tecnologias: </td>
  <td><label>
    <input type="checkbox" name="tecno" value="Java" id="tecnologias_0">
    Java</label>
    <br>
    <label>
      <input type="checkbox" name="tecno" value="PHP" id="tecnologias_1">
      Php</label>
    <br>
    <label>
      <input type="checkbox" name="tecno" value="JavaScript" id="tecnologias_2">
  JavaScript</label></td>
</tr>

In MySQL I have this:

[Ljava.lang.String;@6ecf7e94<br>

But it should be java,javascript,php If they were selected
I use the latest version of java and tomcat 9, all the latest version 02/2017

1 Answer 1

0

I think you intended to insert the array as String. If so, you need to convert the array into proper String. You can use String.join() in Java 8,

String tecnoStr = String.join(",", tecno);

For more options to do this, see:

Also, always use parameterized statement PreparedStatement. Your code is currently vulnerable to SQL injection attack.

String instruccionSql = "INSERT INTO USERS (nombre, apellido, usuario, contra, pais, tecno) VAlUES (?,?,?,?,?,?)";

PreparedStatement ps = miConexion.prepareStatement(instruccionSql);
ps.setString(1, nombre);
ps.setString(2, apellido);
ps.setString(3, usuario);
ps.setString(4, contra);
ps.setString(5, pais);
ps.setString(6, tecnoStr);

ps.executeUpdate();

Also, remember to close the connection, statement at the end (left here for the sake of brevity).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.