0

I am working on a project using Spring. I am trying to fetch a set of data from the database and the value will be displayed on textboxes, but instead of giving me String, I keep encountered NaN (Not A Number). Here is part of the source code :

$( "#dialogFormSalesOrder" ).dialog({
                       autoOpen: false,
                       height: 300,
                       width: 600,
                       modal: true,
                       buttons: {
                          "Pick": function() {

                            //var bValid = true;
                            //allFields.removeClass( "ui-state-error" );

                            $(idSalesOrder).val($("#rdbSalesOrder:checked").val());


                        //window.location.replace("managedelivery.htm");
                           <% 

                               Connection connection = null;
                               String driverName = "com.mysql.jdbc.Driver";
                               Class.forName(driverName);
                               String serverName = "localhost:3306";
                               String mydatabase = "versedb";
                               String url = "jdbc:mysql://" + serverName +  "/" + mydatabase; // a JDBC url

                                String username = "root";
                                String password = "";
                                connection = DriverManager.getConnection(url, username,password);

                                Statement stmt = connection.createStatement();
                                ResultSet rs = stmt.executeQuery("Select idProduct, quantity from vrs_tsodetail where idSO='SO101'");

                                while(rs.next())

                                {%>



                                        $("#warehouse tbody" + "").append(
                                            "<tr>" +
                                            '<td><input id="idProduct" name="idProduct" value="' + <% String s = rs.getString(1).toString();  %> + '" /></td>' +
                                            '<td><input id="idQuantity" name="idQuantity" value="' + <%  s = rs.getString(2); %> + '" /></td>' +
                                            '<td><input id="idUnit" name="idUnit" value="cccccc" /></td>' + "</tr>"
                                        );

                                <%}%>




                            $( this ).dialog("close");
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    },
                    close: function() {
                        //allFields.val( "" ).removeClass( "ui-state-error" );

                    }
Wandy Wijayanto
  • 83
  • 1
  • 2
  • 11
  • Using root with no password to access your DB .....Bad idea. Do you have a live example? – Alistair Laing Oct 12 '11 at 10:32
  • @AlistairLaing what is live example? I am just continuing someone's program, and he used no password to the DB. – Wandy Wijayanto Oct 12 '11 at 10:33
  • live example is an example others can access to see your code in action. You can still login as root set a password create another user with less access than root. This woudl increase your applications secruity. – Alistair Laing Oct 12 '11 at 10:39
  • isn't the resultset zero-based? so getString(0) for the id ? – Manuel van Rijn Oct 12 '11 at 10:40
  • I would also write a script and simply do an ajax request to return the html that you want to use rather than looping through all your rows and placing all the html on the page if the user is not going to use it. Also it would work better than append multiple html together. – Alistair Laing Oct 12 '11 at 10:41
  • @Manuel no, the resultset starts from getString(1) if I am not mistaken – Wandy Wijayanto Oct 12 '11 at 10:45
  • @AlistairLaing great advice. I will use AJAX. But I am still curious what is the cause of NaN my program keep giving me – Wandy Wijayanto Oct 12 '11 at 10:45
  • @Wandy I'vr not used jsp but from other languages its usually zero based. – Alistair Laing Oct 12 '11 at 10:56

2 Answers2

0

<% String s = rs.getString(1).toString(); %> does not output anything. It is just a script-let (codeblock).

To output something to the page, you would do

<%= rs.getString(1).toString() %>

Notice the equals ("=") sign. That translates the block to a out.print(...) statement.

On a side-note, I hope that is just test-code you pasted in your question. That much logic (and particularly opening db connections) in a JSP is not pretty...

pap
  • 27,064
  • 6
  • 41
  • 46
0

Here's the extract of relevance from your code:

$("#warehouse tbody" + "").append(
    "<tr>" +
    '<td><input id="idProduct" name="idProduct" value="' + <% String s = rs.getString(1).toString();  %> + '" /></td>' +
    '<td><input id="idQuantity" name="idQuantity" value="' + <%  s = rs.getString(2); %> + '" /></td>' +
    '<td><input id="idUnit" name="idUnit" value="cccccc" /></td>' + "</tr>"
);

There are 2 problems here (there are actually more serious problems with this code in general, but they do not actually affect the functionality):

  1. You are not printing anything to the response.
  2. You're expecting that Java and JavaScript runs in sync.

To solve problem 1, you need to use <%= %> instead of assigning them to a String in a <% %> and then totally ignoring it.

$("#warehouse tbody" + "").append(
    "<tr>" +
    '<td><input id="idProduct" name="idProduct" value="' + <%= rs.getString(1) %> + '" /></td>' +
    '<td><input id="idQuantity" name="idQuantity" value="' + <%= rs.getString(2) %> + '" /></td>' +
    '<td><input id="idUnit" name="idUnit" value="cccccc" /></td>' + "</tr>"
);

To solve problem 2, you need to remove those JavaScript string concatenations on Java/JSP-printed values. You should see Java/JSP as a HTML/JS code generator. Otherwise they will be treated as names of existing JavaScript variables. Assuming that 1st column returns "foo" and 2nd column returns "bar", this would only end up in the generated JS code like so (rightclick page in browser, do View Source to see it yourself):

$("#warehouse tbody" + "").append(
    "<tr>" +
    '<td><input id="idProduct" name="idProduct" value="' + foo + '" /></td>' +
    '<td><input id="idQuantity" name="idQuantity" value="' + bar + '" /></td>' +
    '<td><input id="idUnit" name="idUnit" value="cccccc" /></td>' + "</tr>"
);

But you don't have those variables definied anywhere in JS code, right? They're undefined. You need to inline it in the JS code instead:

$("#warehouse tbody" + "").append(
    "<tr>" +
    '<td><input id="idProduct" name="idProduct" value="<%= rs.getString(1) %>" /></td>' +
    '<td><input id="idQuantity" name="idQuantity" value="<%= rs.getString(2) %>" /></td>' +
    '<td><input id="idUnit" name="idUnit" value="cccccc" /></td>' + "</tr>"
);

This way it ends up in valid JS code like so:

$("#warehouse tbody" + "").append(
    "<tr>" +
    '<td><input id="idProduct" name="idProduct" value="foo" /></td>' +
    '<td><input id="idQuantity" name="idQuantity" value="bar" /></td>' +
    '<td><input id="idUnit" name="idUnit" value="cccccc" /></td>' + "</tr>"
);
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555