1 2 Previous Next 19 Replies Latest reply on Jan 16, 2009 2:01 PM by 807589

    Casting string to integer

    807589
      I am trying to cast string from get form method

      <%! String surveyid;%>
      <% surveyid= request.getParameter("surveyid");%>
      <%!long myid;%>
      <% myid=Integer.parseInt(surveyid); %>
      <%=myid%>

      i can get the output but when i am trying to fetch data from database from following code
      ResultSet resultset =
      statement.executeQuery("select * from questionsquery where survey_id="myid) ;
      thanks
        • 1. Re: Casting string to integer
          807589
          Have you got a question?
          • 2. Re: Casting string to integer
            807589
            So what happens (assuming the missing + is in the actual code).
            • 3. Re: Casting string to integer
              807589
              i tried ' "+myid+" ' but not working
              • 4. Re: Casting string to integer
                807589
                myid
                • 5. Re: Casting string to integer
                  807589
                  plus myid plus
                  • 6. Re: Casting string to integer
                    807589
                    everything and i m stuck
                    • 7. Re: Casting string to integer
                      thomas_behr
                      You seriously need to learn [How To Ask Questions The Smart Way|http://www.catb.org/~esr/faqs/smart-questions.html].
                      • 8. Re: Casting string to integer
                        807589
                        nezzy wrote:
                        i tried ' "+myid+" ' but not working
                        There's no more irritating phrase on this board than "Not Working" it tells us nothing. There are so many flavours of "not working", and you don't really tell us where you put this dubious bit of text.
                        • 9. Re: Casting string to integer
                          807589
                          i am fetching data from database and putting the value of id which is autonumber field in access into session variable.then on next page i am pulling it out from session variable to use that id in my select criteria.i cast it into int but type mismatch error from apache.i want to pull information from database.
                          <%! String surveyid;%>
                          <% surveyid= request.getParameter("surveyid");%>
                          <%!long myid;%>
                          <% myid=Integer.parseInt(surveyid); %>
                          <%=myid%>
                          <% 
                                      Connection connection = DriverManager.getConnection(
                                          "jdbc:odbc:osa", "userName", "password");
                          
                                      Statement statement = connection.createStatement() ;
                                      ResultSet resultset = 
                                          statement.executeQuery("select * from questionsquery") ; //HERE I WANT TO USE myid variable
                                  %>
                          thanks
                          • 10. Re: Casting string to integer
                            807589
                            What's wrong with doing this?
                            statement.executeQuery("select * from questionsquery" + surveyid);
                            Also, code like this should preferably not be in a JSP.
                            • 11. Re: Casting string to integer
                              masijade
                              codingMonkey wrote:
                              What's wrong with doing this?
                              statement.executeQuery("select * from questionsquery" + surveyid);
                              Also, code like this should preferably not be in a JSP.
                              1. Should be a PreparedStatement to prevent SQL injection attacks and to prevent mistakes (such as point #2)
                              2. There is no space after "questionsquery", so if "surveyid" equals, say "5", then you will end up with "questionsquery5" (aside from the fact that there is no where clause there, at all).
                              3. Should not be done as a scriptlet in a JSP, in the first place. Create a Bean.
                              • 12. Re: Casting string to integer
                                807589
                                masijade. wrote:
                                codingMonkey wrote:
                                What's wrong with doing this?
                                statement.executeQuery("select * from questionsquery" + surveyid);
                                Also, code like this should preferably not be in a JSP.
                                1. Should be a PreparedStatement to prevent SQL injection attacks and to prevent mistakes (such as point #2)
                                2. There is no space after "questionsquery", so if "surveyid" equals, say "5", then you will end up with "questionsquery5" (aside from the fact that there is no where clause there, at all).
                                3. Should not be done as a scriptlet in a JSP, in the first place. Create a Bean.
                                Remind me not to answer questions when I'm tired.

                                @OP
                                What masijade said. Ignore my previous reply.
                                • 13. Re: Casting string to integer
                                  807589
                                  what wrong with that pls help me thanks
                                  exception 
                                  
                                  org.apache.jasper.JasperException: An exception occurred processing JSP page /startsurvey.jsp at line 29
                                  
                                  26: 
                                  27:             Statement statement = connection.createStatement() ;
                                  28:             ResultSet resultset = 
                                  29:                 statement.executeQuery(" select * from questionsquery where survey_id" + surveyid) ; 
                                  30:         %>
                                  
                                  
                                  root cause 
                                  
                                  javax.servlet.ServletException: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
                                       org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
                                       org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
                                       org.apache.jsp.startsurvey_jsp._jspService(startsurvey_jsp.java:118)
                                       org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
                                       javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                                       org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
                                       org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
                                       org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
                                       javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
                                  thanks
                                  • 14. Re: Casting string to integer
                                    807589
                                    what wrong with that pls help me thanks
                                    Read reply #13. It tells you what's wrong.

                                    ~
                                    1 2 Previous Next