How to use @queryparam when using array in Java

1.3k views Asked by At

I am using a restful web service and I was able to display database records using an array. But I am confused on how will I be able to display my desired record. I have here the class where the SQL query is being executed. I am using Advanced Rest Client google chrome application in testing the response and the output. How will I be able to query 'select * from taxi where taxi_plate_no='inputted data''? I am really confused on how will I be able to do it in an array. Please help me. Thank you! :(

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.ws.rs.QueryParam;

import com.taxisafe.objects.Objects;


public class DisplayArrayConnection
{


    public ArrayList<Objects> getDetails(Connection con) throws SQLException{
    ArrayList<Objects> taxiDetailsList = new ArrayList<Objects>();
    PreparedStatement stmt = con.prepareStatement("SELECT * FROM taxi");
    ResultSet rs = stmt.executeQuery();
    try
    {
        while(rs.next())
        {
            Objects detailsObject = new Objects();
            detailsObject.setTaxi_name(rs.getString("taxi_name"));
            detailsObject.setTaxi_plate_no(rs.getString("taxi_plate_no"));

            taxiDetailsList.add(detailsObject);

        }
    } catch (SQLException e)
    {       
        e.printStackTrace();
    }
    return taxiDetailsList;
    }
}
1

There are 1 answers

5
Gaurav On

@QueryParam is annotation used in rest webservices . And i think here you want to use parameter with your SQL query.

So using parameter in PreparedStatement use following code

     public class DisplayArrayConnection
         {

        public ArrayList<Objects> getDetails(Connection con,String taxiNumber) throws SQLException{
        ArrayList<Objects> taxiDetailsList = new ArrayList<Objects>();
        PreparedStatement stmt = con.prepareStatement("SELECT * FROM taxi WHERE taxi_plate_no= ?"); 
stmt.addString(1,taxiNumber);
        ResultSet rs = stmt.executeQuery();
        try
        {
            while(rs.next())
            {
                Objects detailsObject = new Objects();
                detailsObject.setTaxi_name(rs.getString("taxi_name"));
                detailsObject.setTaxi_plate_no(rs.getString("taxi_plate_no"));

                taxiDetailsList.add(detailsObject);

            }
        } catch (SQLException e)
        {       
            e.printStackTrace();
        }
        return taxiDetailsList;
        }

    }

Note: Use parameter taxiNumber or any other parameter you want to retrive data on that parameter

and use setString(position,value); to replace ? with that parameter