Search code examples
javadatabaseweb-servicesjerseyjersey-client

Passing a Database query as an argument from jersey client to jersey webservice to perfom DML


I have a jersey client which needs to perform DML operation on a remote database server. I have created jersey web service which takes argument as a string(i.e. query to be passed by client). I don't know how should I do it. Please help me. Thanks in advance.!

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.ws.rs.Path;

import oracle.sql.CLOB;

@Path("/insertupdate")
public class InsertUpdate {

/**
 * Inserts inclusion detail in Database
 * @param insertQuery
 * @param inclusionScript
 * 
 */
@Path("/insertInclusions")
public void insertInclusions(String insertQuery, String inclusionString) {

    DbConnection con = null;
    Connection conn = null;
    PreparedStatement insertSt = null;
    try {
        con = new DbConnection();
        conn = con.dbConnect();
        insertSt = conn.prepareStatement(insertQuery);
        CLOB tempClob = CLOB.createTemporary(conn, false,
                CLOB.DURATION_SESSION);
        tempClob.putChars(1, inclusionString.toCharArray());
        insertSt.setClob(1, tempClob);
        insertSt.execute();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            // insertSt.close();
            if (insertSt != null)
                insertSt.close();

        } catch (SQLException sqlExp) {
            sqlExp.printStackTrace();
        }
    }
}
}

Solution

  • First you need to correct your web service code to accept the params. If you are using GET, then you can use PathParam to accept your DB query strings. Something liek this:

    @Path("/insertInclusions")
    public void insertInclusions(@PathParam("insertQuery") String insertQuery, @PathParam("inclusionString") String inclusionString)
    

    In your jersey client code, you can simply add the params in the GET URL