Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Retrieve records from MySQL using radio button

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43015
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far, or if you haven't tried anything - what ideas have you had?
 
Devendra Bhati
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
<%
}
}
%>
 
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  •  
    Devendra Bhati
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Bibeault,
    Will you please give me a sample code for my problem.
     
    Bear Bibeault
    Sheriff
    Posts: 67389
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No. Please try to write to the code yourself and post with any problems you are having. CodeRanch is NotACodeMill.
     
    Devendra Bhati
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 67389
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Ranch Hand
    Posts: 188
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:Some observations:

  • Use a PreparedStatement or risk SQL Injection attacks.

  • I really didn't get this one SQL Injection attacks ???!
     
    Bear Bibeault
    Sheriff
    Posts: 67389
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
     
    Devendra Bhati
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Sheriff
    Posts: 67389
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    No thanks. We have all the government we need. This tiny ad would like you to leave now:
    Two software engineers solve most of the world's problems in one K&R sized book
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic