ResultSet中带有空格的getString

时间:2013-12-30 03:40:18

标签: jsp servlets jdbc spaces getstring

我正在使用Servlets / JSP管理JDBC数据库,我在表中的一个属性是一个字符串,在字之间可能有也可能没有空格。我有一个JSP显示所有信息,另一个用于编辑它,我执行getStringResultSet当我只是显示它时工作正常,但在编辑JSP上它只有“抓住”空格前的第一个单词,其余的字符串消失。这是代码的一部分:

PerfilUsuarioConectado.jsp (我用来显示数据的那个)

<%
    Connection conexion = DriverManager.getConnection("jdbc:odbc:gasteizcar", "", "");
    Statement set = conexion.createStatement();
    ResultSet rs = set.executeQuery("SELECT * FROM Usuario WHERE correoElectronico ='" + session.getAttribute("username") + "'");
    rs.next();
%>
<div id="principal">
    <table border="1" align="center">
        <tr>
            <td> Nombre: </td>
            <td> <%= rs.getString("nombre")%>
        </tr>
        <tr>
            <td> Apellidos: </td>
            <td> <%= rs.getString("apellidos")%>
        </tr>
        <tr>
            <td> E-mail: </td>
            <td> <%= rs.getString("correoElectronico")%>
        </tr>
        <tr>
            <td> Alias: </td>
            <td> <%= rs.getString("alias")%>
        </tr>
        <tr>
            <td> Nº móvil: </td>
            <td> <%= rs.getString("movil")%>
        </tr>
        <tr>
            <td> Coche: </td>
            <td> <%= rs.getString("marca") + " " + rs.getString("modelo") + " " + rs.getString("color")%>
        </tr>
    </table>
</div>

ModificarDatos.jsp (编辑数据的那个)

<%
    Connection conexion = DriverManager.getConnection("jdbc:odbc:gasteizcar", "", "");
    Statement set = conexion.createStatement();
    ResultSet rs = set.executeQuery("SELECT * FROM Usuario WHERE correoElectronico ='"
            + session.getAttribute("username") + "'");
    int i = 0;
    rs.next();
    String marca = rs.getString("marca");
    String modelo = rs.getString("modelo");
    String color = rs.getString("color");
    String movil = rs.getString("movil");
%>
<div id="principal">
    <form id="datos" action="ModificarDatos" method="post">
        <table border="1" align="center">
            <tr>
                <td> * Verificar con contraseña: </td>
                <td> <input pattern="[a-zA-Z0-9 ]{3,12}" type="password" id="password" name="password" required></td>
            </tr>
            <tr>
                <td> ** Nueva contraseña: </td>
                <td> <input pattern="[a-zA-Z0-9 ]{3,12}" type="password" id="nuevaContrasenia" name="nuevaContrasenia"> </td>
            </tr>
            <tr>
                <td> ** Repita la contraseña: </td>
                <td> <input pattern="[a-zA-Z0-9 ]{3,12}" type="password" id="repContrasenia" name="repContrasenia"> </td>
            </tr>
            <tr>
                <td> * Nº de móvil: </td>
                <td> <input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value=<%= movil%>> </td>
            </tr>
            <tr>
                <td> *** Marca del coche: </td>
                <td> <input type="text" name="marca" id="marca" value=<%= marca%>> </td>
            </tr>
            <tr>
                <td> *** Modelo del coche: </td>
                <td> <input type="text" name="modelo" id="modelo" value=<%= modelo%>> </td>
            </tr>
            <tr>
                <td> *** Color: </td>
                <td> <input type="text" name="color" id="color" value=<%= color%>> </td>
            </tr>
        </table>
</div>
<input type="button" id="bActualizar" value="Actualizar datos">

所以,如果有人能告诉我为什么getString方法在两种情况下都表现不同,我会非常感激。

2 个答案:

答案 0 :(得分:0)

使用ResultSet类中的getString()方法的目的是从指定为String的列返回数据。它可以采用两个参数作为

  

String getString(String columnLabel)抛出SQLException

     

String getString(String columnIndex)抛出SQLException

第一个是使用指定的

列的ex迭代ResultSet

String marca = rs.getString("marca");

第二个可能是这样的

String marca = rs.getString(1);

同样迭代结果集直到rs.next以获取表格中的所有值。更多信息here

希望它有所帮助!!

答案 1 :(得分:0)

错误在以下几行:

<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value=<%= movil%>>

如果您的变量movil包含abc def,那么生成的HTML将是:

<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value=abc def>

然后将输入值设置为abc并创建另一个属性def,该属性无法识别并将被忽略。事实上,Stack Overflow上突出显示的Markdown语法指出了这一点:对于值,abc为蓝色,对于属性名称,def为红色。

至少,您需要在<%= movil %>

周围加上引号
<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value="<%= movil%>">

如果movil包含abc def,则此次输出将为

<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value="abc def">

现在您可以看到该值已正确写入。


除此之外,还有一些我想提出的意见:

  • 首先,您的代码容易受到SQL injection的攻击。如果您的username会话变量最终为test' OR 1=1 --,则将返回数据库的所有结果。更糟糕的是,如果它类似于test'; DROP TABLE Usuario;--,您可能会丢失数据。请改用PreparedStatements

  • 其次,正如Aniket在评论中指出的那样,你真的不应该在JSP中使用scriptlet <% ... %>了。相反,您应该使用JSTL标记和EL表​​达式。 The question linked to by Aniket是一个很好的起点。

我很欣赏这可能是您的第一个JSP应用程序。但是,一旦你开始工作,我建议你考虑做出这些改变。

相关问题