0

I would like to save my json data object into my Postgresql using jQuery(ajax call on post request) and Java after clicking Submit button, Please help me to do this ? Thanks in advance.

html:

(function() {
  var testValue;
  testValue = {};
  $('input[id=buttonId]').click(function() {
    var name;
    name = $(this).attr('name');
    testValue[name] = $(this).val();
  });
  $('#submit').click(function() {
    $('input[id=fileId]').each(function($i) {
      var name;
      name = $(this).attr('name');
      testValue[name] = $(this).val();
    });
    console.log(JSON.stringify(testValue));//it gives the json data object, after clicking on Submit button`[ex: {"firstButton":"button1","secondButton":"button2","file":"test.txt"} ]`
  });
})();

Postgresql table: create table savedata(id integer primary key, content json);

credentials:

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/dbname"
db.default.user=test
db.default.password=test

html:

<input type='button' value='button1' class='button' id="buttonId" name='firstButton'/>
<input type='button' value='button2' class='button' id="buttonId" name='secondButton'/>
<input type='file' name='file' id="fileId" value='file upload'/>
<input type='submit' value='submit' id='submit'>
Nag
  • 129
  • 2
  • 3
  • 10
  • Thanks @Thomas for reply, Yes, front-end can be either Javascript/AngularJS/jQuery and back-end is either Java/Scala and Postgresql database. – Nag Jan 18 '16 at 11:19
  • @Thomas There's nothing obscure about a javascript frontend with a java backend. In fact it's quite common. – Kayaman Jan 18 '16 at 11:49

1 Answers1

0

I would start by writing a HTTP server in java which accepts POST requests from javascript later. You would be smart to also add some form of authentication in it so not everybody can add records. You can also use a framwork which I would use.

As soon as a POST request comes in with JSON data and the required authorisation you can let Java parse the JSON to Insert statements for the database.

Now the Javascript part: you can best send JSON encoded data with ajax using jQuery. Use a POST request.

In this case it would look like using:

  • http://www.tutorialspoint.com/postgresql/postgresql_java.htm

  • How to parse a JSON and turn its values into an Array?

  • http://restx.io/docs/ref-core.html

    // Create a method that handles a POST request. In this case to "/message"
    @POST("/message/}")
    public Message insertInDb(Message msg // body param
                            ) {
        // Decode the JSON to an array object so you can loop it for insert statement(s)           
       JSONArray json = new JSONArray(msg);
        /////////////////////////////////////////////////////////////////////
      // Create the postgre connection
      Connection c = null;
      Statement stmt = null;
      try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/testdb",
            "manisha", "123");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully");
    
         stmt = c.createStatement();
    
         // Use the JSON data instead below for the INSERT statement:
         String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
               + "VALUES (1, 'Paul', 32, 'California', 20000.00 );";
         stmt.executeUpdate(sql);
    
    
         stmt.close();
         c.commit();
         c.close();
      } catch (Exception e) {
       return  e.getClass().getName()+": "+ e.getMessage();
    
      }
        return "success";
    }
    

jQuery

    //POST JSON data to the API
    $.post( "youdomain.com/message", JSON.stringify(yourDataObjectOrArray));
Community
  • 1
  • 1
online Thomas
  • 8,864
  • 6
  • 44
  • 85