Search code examples
javaresultset

Java getString from resultSet cuts off string at first space or takes substring inside of ''


I have a servlet that queries a mysql db and then displays the data in a table. I have a column that is called type and it is a string. When I do

rs.getString("type")

without using a input item I get the full string (rs is the resultSet). But when I do this:

<input name="class" type="text" value=<%=rs.getString("type")%>>

If the string has a space i.e. 'ABC DEF' then it will only have a value of 'ABC'. Or if the string is ''1' ABC' then the value is only '1'.

How can I get the full value to be in the value?


Solution

  • I believe your question is similar to the one asked in getString from ResultSet with spaces. You should perhaps try storing the result of rs.getString() in a variable and display it with quotes in your HTML. For example, if the string returned is 'ABC DEF', then it'd look like:

    <input name="class" type="text" value=ABC DEF>
    

    The proper way to do this would be

    <input name="class" type="text" value="ABC DEF">
    

    Your code is however very vulnerable to SQL injection. Please check the link I provided for a more complete answer to your question.