【问题标题】:How to add multiple tags (array) to a single post using jsp and mysql?如何使用 jsp 和 mysql 将多个标签(数组)添加到单个帖子中?
【发布时间】:2017-02-26 19:19:36
【问题描述】:

我有问题
Tecno 是标签:java,php,javascript
我的 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");
}

还有我的公式rio_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>

在 MySQL 中我有这个:

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

但是应该是java,javascript,php 如果被选中
我用的是最新版的java和tomcat 9,都是02/2017最新版

【问题讨论】:

    标签: java mysql jsp checkbox


    【解决方案1】:

    我认为您打算将数组作为字符串插入。如果是这样,您需要将数组转换为正确的字符串。你可以在 Java 8 中使用String.join()

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

    有关执行此操作的更多选项,请参阅:

    另外,总是使用参数化语句PreparedStatement。您的代码目前容易受到 SQL 注入攻击。

    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();
    

    另外,记得关闭连接,声明在最后(为简洁起见留在这里)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      相关资源
      最近更新 更多