View Full Version : JSP codes seems not executed correctly
tllcll
08-29-2005, 09:15 AM
Hi,
I would like to check with you how JSP codes residing in js function works.
How can I get the 2nd select stmt executed based on the values retrieve from 1st select statement and the conditions pass in to this js function
How can I get the select statement to execute only when a condition is met.
The following is the codes. It is not executing the 2nd select statement. when I didnt put in the 2nd select stmt, i manage to execute codes in that portion
script
====
function test(obj, obj2) {
<%
sql_query = "SELECT a1, a2 , a3, a4, a5 "+
" FROM table_a ";
try {
rset = db.execSQL(sql_query);
}
catch(SQLException e){
System.err.println ("Error in query " +e);
}
%>
var po_ln_fnd = false
<% while (rset.next()) {
j_a1 = rset.getString("a1");
j_a2 = rset.getString("a2");
j_a3 = rset.getString("a3");
%>
if ((eval(obj2.value)== '<%=j_a1%>')) {
if ((obj.value == '<%=j_a2%>') ) {
<% j_a4 = rset.getString("a4");
j_a5 = rset.getString("a5");
sql_query = "SELECT b2, b3, b4 "+
" FROM table_b "+
" WHERE b1 = '"+j_a3+"' ";
try {
rset = db.execSQL(sql_query);
}
catch (SQLException e) {
System.err.println("Error in query " +e);
}
while (rset.next()) {
j_b2 = rset.getString("b2");
j_b3 = rset.getString("b3");
j_b4 = rset.getString("b4");
}
%>
}
}
<%}>
}
nikkiH
08-29-2005, 05:01 PM
You have rset redefined inside the loop that uses it from the first query. You can't do that. Either that or you didn't realize you were missing the end bracket. If you want a second query inside the first, make a new resultset object.
Format your code with the code blocks. It's nearly impossible to read like that. Use the little pound sign (#) up there to enclose code in [ code ] [ /code ] blocks so it keeps formatting.
Java code executes on the server. Javascript executes on the client. Not quite sure what you were asking, but it almost seems like you're trying to get the JSP to execute based on javaSCRIPT values. It doesn't work like that. You can write out dynamic javascript with JSP, but the JSP is done by the time the javascript is executed over on the client.
tllcll
08-30-2005, 03:18 AM
Thanks for the reply.
I actually wanted to executed a second query inside the first query, I'm not sure what is the proper way of doing so. I need to get the value from the screen input to excute the 1st query, then based on the value retrieved in 1st query, I need to execute the 2nd query based on these values, and so on.
I'm not sure how to perform the following as indicated: pls help:
"Format your code with the code blocks. It's nearly impossible to read like that. Use the little pound sign (#) up there to enclose code in [ code ] [ /code ] blocks so it keeps formatting."
I',m trying to get the JSP to execute based on javaSCRIPT values. how do I write out dynamic javascript with JSP as mentioned.
How do I go about, if I require to perform operation based on the screen input, then execute multiple queries based on values inside the 1st, 2nd, etc query.
Pls help
nikkiH
08-30-2005, 03:30 PM
I actually wanted to executed a second query inside the first query, I'm not sure what is the proper way of doing so. I need to get the value from the screen input to excute the 1st query, then based on the value retrieved in 1st query, I need to execute the 2nd query based on these values, and so on.
Similar to what you did, but you need a new resultset object or you're overwriting the current one. You wouldn't re-use a loop counter inside a nested loop, either. Same principal. The resultset is your "counter" (rs.next()) here.
ResultSet rs1, rs2;
// more code
while (rs1.next())
{
// more code ...
rs2 = db.execSQL(sql_query);
while (rs2.next())
{
// more code for rs2 ...
}
}
I'm not sure how to perform the following as indicated: pls help:
"Format your code with the code blocks. It's nearly impossible to read like that. Use the little pound sign (#) up there to enclose code in [ code ] [ /code ] blocks so it keeps formatting."
See what I did up there?
When you post, there's an editor. At the top is a pound sign (#) button that when you click it, it surrounds highlighted code in BBCode [ code ]code here[ /code ] (no spaces -- I have to post that with spaces so it doesn't actually make a code block).
See here (http://www.phpbb.com/phpBB/faq.php?mode=bbcode)for more on BBCode.
(as an aside, I use Firefox and have the BBCode extension, so I don't use the buttons...)
I',m trying to get the JSP to execute based on javaSCRIPT values. how do I write out dynamic javascript with JSP as mentioned.
How do I go about, if I require to perform operation based on the screen input, then execute multiple queries based on values inside the 1st, 2nd, etc query.
You need to pass the value to a JSP page (method unimportant, most common would be as URL param or form submit). Then you can run whatever query you want as pure JSP code and output whatever you want. But the javascript you output will only be executed after the java code has finished executing and the response has been written to the client.
This is very basic client/server architecture. If you don't understand that, you're going to have a lot of problems later. I'd suggest reading up on it before proceeding.
Also, this is very simple JSP. If you're just starting out, that's fine, but if you get into anything with any substance, you better get used to beans and tag libraries. Embedding SQL into the pages like this is beginner-only. Struts and JSTL are your friends. ;)
And a properties file for the connections, etc.
This should look a lot more like this:
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<sql:setDataSource
var="example"
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:ORCL"
user="scott"
password="tiger"
/>
<sql:query var="myTableA">
SELECT a1, a2 , a3, a4, a5 FROM table_a
</sql:query>
<script type="text/javascript">
function test(obj, obj2) {
var po_ln_fnd = false;
<c:forEach var="row" items="${myTableA.rows}">
<c:forEach var="column" items="${row}">
<c:out value="${column.a1}" />
<c:out value="${column.a2}" />
<c:out value="${column.a3}" />
<c:out value="${column.a4}" />
</c:forEach>
</c:forEach>
// and so on
}
</script>
Now, if you want javascript to conditionally do something based on DB values, you can code it, but you have to run all the necessary queries first and write it out as javascript arrays or some such. You cannot embed conditional queries that only run as a result of javascript values. It just doesn't work that way. If you want that sort of thing, you need to go with AJAX or such. But not like you have it.
tllcll
09-01-2005, 04:09 AM
Hi, thanks for your reply.
1) I have tried to replace rset with your suggestion but give me error - the poage does not display at alll - objext null
2) for this item, i dont quite understand, would you possibly explained, how can I use the value from the 1st text textbox of the 15row values as the condition for the 2nd textbox to check the value and then retrieve some other values from other table and get it displayed to the particular text box on the form
I may not reply so soon - away for few days
nikkiH
09-01-2005, 05:16 PM
1. I didn't give you full code. I gave you the idea. You still need to make a new ResultSet object for the second one and so on.
2. Not at all like this. Look into AJAX.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.