Jsp 从带有空格的ResultSet获取字符串

Jsp 从带有空格的ResultSet获取字符串,jsp,servlets,jdbc,spaces,getstring,Jsp,Servlets,Jdbc,Spaces,Getstring,我正在用servlet/jsp管理一个JDBC数据库,表中的一个属性是一个字符串,它在单词之间可能有空格,也可能没有空格。我有一个JSP来显示所有信息,还有一个JSP来编辑它,在这两个JSP上,我对结果集执行getString,当我只是显示它时,它工作得很好,但在edit JSP上,它只在空格和字符串的其余部分消失之前抓取第一个单词。以下是部分代码: perfilusarioconectado.jsp是我用来显示数据的 <% Connection conexion = Driver

我正在用servlet/jsp管理一个JDBC数据库,表中的一个属性是一个字符串,它在单词之间可能有空格,也可能没有空格。我有一个JSP来显示所有信息,还有一个JSP来编辑它,在这两个JSP上,我对结果集执行getString,当我只是显示它时,它工作得很好,但在edit JSP上,它只在空格和字符串的其余部分消失之前抓取第一个单词。以下是部分代码:

perfilusarioconectado.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方法在这两种情况下表现不同,我将非常感激。

ResultSet类中的getString方法用于从指定为String的列返回数据。它可以接受两个参数

String getStringString columnLabel引发SQLException

String getStringString columnIndex引发SQLException

第一个是使用指定的ex列迭代ResultSet

字符串marca=rs.getStringmarca

第二个可能是这样的

字符串marca=rs.getString1

还将整个结果集迭代到rs.next,以从表中获取所有值。详细信息


希望能有帮助

错误在以下几行中:

<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value=<%= movil%>>
然后将输入值设置为abc,并创建另一个属性def,该属性无法识别,将被忽略。事实上,堆栈溢出上突出显示的标记语法指出了这一点:对于值,abc为蓝色,对于属性名,def为红色

至少,您需要在以下内容周围加上引号:

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

除此之外,我还想提出几点意见:

首先,您的代码容易受到攻击。如果username会话变量以类似test'或1=1-的形式结束,则将返回数据库中的所有结果。更糟糕的是,如果这是类似于“测试”的东西;升降台Usuario;-,你可能会丢失数据。改用

其次,正如Aniket在评论中指出的,您真的不应该再在JSP中使用Scriptlet了。相反,您应该使用JSTL标记和EL表达式。这是一个很好的开始


我很感激这可能是您的第一个JSP应用程序。但是,一旦你让它工作了,我建议你考虑做这些改变。

不要在JSP中编写脚本,因为Script脚本不应该在JSP中使用超过十年。学习JSP、servlet和使用servlet编写Java代码。您能告诉我们您没有获得完整字符串的字段吗。一个可能的错误可能是您正在使用的模式以及用于验证值的输入字段。您检查字段的模式,您没有获得完整的字符串,如果无法解决,请告诉我们您没有获得值的行输入,因为语言不是英语,所以请告诉我们行号。感谢有关避免JSP中使用Java代码的信息,但问题是我只有2天的时间交付此项目,我只是不能承受改变它有很多像这样的问题,在我的辩护中,我只有12天的时间去做。我相信所有这些都不起作用,尽管我只尝试了“Modelo”和“Color”。我会处理掉这些模式,然后回去报告。谢谢大家!!模式不是问题,因为我遇到问题的字段是没有模式的字段。感谢您的评论,我修复了“String getStringString columnIndex”必须是“String getStringint columnIndex”,我尝试了getStringint columnIndex,得到了相同的结果。无论如何谢谢。哇,非常感谢你!问题是参数周围缺少引号。再次感谢你!一旦我在期末考试后有时间,我很可能会尝试改进这段代码。
<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value=abc def>
<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value="<%= movil%>">
<input pattern="[0-9]{9}" type="text" name="movil" id="movil" required value="abc def">