my dog learned polymorphism*
The moose likes JSP and the fly likes Retrieve records from MySQL using radio button  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Retrieve records from MySQL using radio button  " Watch "Retrieve records from MySQL using radio button  " New topic
Author

Retrieve records from MySQL using radio button

Devendra Bhati
Greenhorn

Joined: Dec 27, 2012
Posts: 19
Hello Friends,
I am new to JSP. I am having 4 cities as Radio buttons, I wants to fetch record of particular city by selecting radio button.
(ex. if i select Bangalore as city, then i will get all records from DB where city is Bangalore.)

I appreciate your active contribution. Please suggest me code or at least link to get things done. Thanks in Advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
What have you tried so far, or if you haven't tried anything - what ideas have you had?


Ping & DNS - my free Android networking tools app
Devendra Bhati
Greenhorn

Joined: Dec 27, 2012
Posts: 19
Dear Ulf Dittmer,

I was not trying. I have a code with the same logic but i was trying in struts framwork.

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shipping_order", "root", "root");
Statement statement = connection.createStatement();

String city = request.getParameter("city");

ResultSet rs = statement.executeQuery("select * from USER where city= '" + city + "'") ;

while(rs.next( )){
out.println("Sorry, could not find that city. ");
%>

<TABLE BORDER="1">
<TR>
<TH>User ID</TH>
<TH>City</TH>


</TR>


<TR>
<TD> User ID : <%= resultset.getString(1) %> </TD>
<TD> City : <%= resultset.getString(2) %> </TD>

</TR>

</TABLE>
<BR>
<%
}
}
%>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60752
    
  65

Some observations:
  • There should be no Java code in a JSP. That's a practice from over 11 years ago! Use the JSTL and EL in the JSP.
  • The database code should not be in the presentation layer. Move it to a model class.
  • resultsets should never be passed around. Copy the data into a Java collection and/or beans and close the resultset as fast as posssible.
  • Use a PreparedStatement or risk SQL Injection attacks.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Devendra Bhati
    Greenhorn

    Joined: Dec 27, 2012
    Posts: 19
    Thanks Bibeault,
    Will you please give me a sample code for my problem.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60752
        
      65

    No. Please try to write to the code yourself and post with any problems you are having. CodeRanch is NotACodeMill.
    Devendra Bhati
    Greenhorn

    Joined: Dec 27, 2012
    Posts: 19
    Okay! Please let me explain, i have no idea about it.
    "Filter option for city and post back when option changes"

    4 radio buttons showing respective cities and on selecting a particular radio button (city) it'll sort my records city wise from database.

    At least give me a link or explanation, i'll very thankful to you.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60752
        
      65

    General steps:
  • A change handler for the radio buttons detects when the values change.
  • The value of the radio button can be used to identify which city was selected.
  • An Ajax request is made to the server to obtain an HTML fragement containing the new display information.
  • The HTML fragment is injected into the DOM.


  • All but generating the fragment on the server is JavaScript. This will be much easier using jQuery than raw JavaScript.
    Arun Giridhar
    Ranch Hand

    Joined: Mar 10, 2012
    Posts: 145

    Bear Bibeault wrote:Some observations:
  • Use a PreparedStatement or risk SQL Injection attacks.

  • I really didn't get this one SQL Injection attacks ???!


    hate Professionalism
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60752
        
      65

    SQL Injection attacks

    Devendra Bhati
    Greenhorn

    Joined: Dec 27, 2012
    Posts: 19
    Hello Bibeault ,

    Thanks for suggesting me method to do. I done client side ajax call by jQuery to get data but server side couldn't get watevere i need to get responce in JSON...?


    Below my Java View and Controller codes.



    I am unable to know how orderList get city property (even i tried with request.getParameter("city")). P{lease help me next.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60752
        
      65

    That's an awful lot of code to look through.

    Your servlet URL look suspect. Is the servlet really being called? If os, is it returning the correct JSON? Is the content type set correctly?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Retrieve records from MySQL using radio button
     
    Similar Threads
    Radio button value with submit
    radio -- 91fm
    How to catch radio button value
    model for group of radio buttons