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("j_b2");
j_b3 = rset.getString("j_b3");
j_b4 = rset.getString("j_b4");
}
%>
}
}
<%}>
}

Dani AI

Generated

Short answer: the JSP scriptlets run on the server while the page is being generated — long before any browser-side JavaScript if/conditions run. That means any <% ... %> blocks (including SQL runs) are executed during page rendering, not “when the JS if becomes true” in the browser. In this thread embedded server-side queries inside a client-side function, which produces static JS that contains server values; the server still executes the nested SQL at generation time. Also note ’s catch: confirm ResultSet.getString(...) uses the actual column names (e.g. "b2") and not fabricated names like "j_b2".

Quick troubleshooting checklist:

  • Check server logs for the SQLException stack trace and print the actual SQL + parameter values so failures are visible.
  • Don’t reuse the outer ResultSet for the inner query — use a separate Statement/ResultSet (or close the outer one first). Reassigning the same rset will clobber the outer loop.
  • Remove unnecessary eval(...) and avoid comparing client variables to server values expecting server-side evaluation.
  • When embedding server values into JS, escape quotes and special characters to avoid JS syntax errors.

Recommended approaches (safer and clearer):

  • Pre-build a JS data structure on the server (if dataset is small) and let client JS filter it.
  • Or, perform an on-demand AJAX request to a servlet/JSP endpoint that returns JSON for the selected b1. Example client snippet:
    function loadBRows(b1) {
    fetch('/getB?b1=' + encodeURIComponent(b1))
      .then(r => r.json())
      .then(rows => { /* populate second <select> */ })
      .catch(err => console.error(err));
    }

    Server-side: use a servlet/DAO and PreparedStatement to query table_b and emit JSON (and close resources in finally/try-with-resources).

Best practices: move DB code out of JSP (use servlets/DAOs or JSTL/EL), use parameterized queries to avoid injection, always close DB resources, and log errors. This will make the behavior predictable and allow the second query to run only when the client actually requests it.

Recommended Answers

All 2 Replies

sql_query = "SELECT b2, b3, b4 "+
" FROM table_b "+
" WHERE b1 = '"+j_a3+"' ";

...

while (rset.next()) {
j_b2 = rset.getString("j_b2");
j_b3 = rset.getString("j_b3");
j_b4 = rset.getString("j_b4");

Just an eyeball look here....

your calling tables b2, b3 and b4 in your select statement. Then you are requesting values j-b2, etc...

Might be the problem.

Regards,

Nate

sorry, the codes should look like this , but still dont get the result
while (rset.next()) {
j_b2 = rset.getString("b2");
j_b3 = rset.getString("b3");
j_b4 = rset.getString("b4");
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.