Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Retrieve records from MySQL using radio button

 
Devendra Bhati
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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • 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>
<%
}
}
%>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 64631
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 64631
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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.
     
    Arun Giridhar
    Ranch Hand
    Posts: 181
    • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 64631
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
     
    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
    Author and ninkuma
    Marshal
    Pie
    Posts: 64631
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic