3

The problem is Code is working when rs.getString (1) is integer number, but in my case it's a varchar primary key , how to fix this.

function getId(id){
    var f=document.form;
    f.method="post";
    f.action='SelectBank1.jsp?id='+id;
    f.submit();
}

<%
Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select IdwhichIsVarchar, description from  SomeDataBase where IS_VISIBLE = 1 order by description  ");
%>

<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>

<td><input type="button" name="edit" value="Edit" style="background-color:green;font-weight:bold;color:white;" onclick="getId(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>

1 Answer 1

1

You need to wrap the value with quotes:

onclick="getId('<%=rs.getString(1)%>')"

Otherwise you will get such output for example:

onclick="getId(foo)"

And JavaScript will try to parse "foo" as variable thus fail. With the fix it will be:

onclick="getId('foo')"

And "foo" will be passed as a string to the function.

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

2 Comments

but when rs.getString(1) ... is 1 or 100, or whatever integer it's working , but when is abv_bg qwery or whatever varchar from db it's not working what quotes change
Numbers are passed as-is (as numeric value) thus don't need quotes around them. This is the same in most, if not all, programming languages pretty sure JSP is one of them as well.

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.