Search code examples
javaweb-servicesibm-cloud

create new record into DB using java RESTful web service


Here's my POST method. It doesn't work. The SQL is tested and it works. I never use POST method to insert data into a database before. DB is running fine. And the project is on bluemix.

    @POST
    @Path("/add")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes("application/xml")
    public String addManufacturer(Manufacturer manufacturer) {

        PreparedStatement query = null;
        Connection conn = null;
        int MANUFACTURERID = 4;
        String MANUFACTURERNAME = "BOOM2";

        // TODO
        try {
            conn = db.DBConn().getConnection();
            String insertQuery = "Insert into MANUFACTURER (MANUFACTURERID,MANUFACTURERNAME) values ('"
                    + MANUFACTURERID + "','" + MANUFACTURERNAME + "')";
            query = conn.prepareStatement(insertQuery);
            query.executeQuery();
            query.close();

            // String query=
            // "Insert into users (id,username,password) values('"+id+"','"+Username+"','"+Password+"')";
            // PreparedStatement stm = con.prepareStatement(query);
            // stm.executeUpdate(query);
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (conn != null){/*conn.close();*/}
        }
        return "New record inserted";
    }

Anything i missed?


Solution

  • You have defined a POST method so you must send a HTTP POST request to the server rather than HTTP GET request (putting the URL in a web browser just sends a GET request).

    Please look at using something like Postman for Chrome or RestClient for Firefox to be able to send HTTP POST (and other methods) to your app.