【问题标题】:How to save json data in PostgreSQL using jQuery Post and Java?如何使用 jQuery Post 和 Java 在 PostgreSQL 中保存 json 数据?
【发布时间】:2016-04-23 12:54:40
【问题描述】:

我想在单击提交按钮后使用 jQuery(发布请求上的 ajax 调用)和 Java 将我的 json 数据对象保存到我的 Postgresql 中,请帮我这样做?提前致谢。

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 表:create table savedata(id integer primary key, content json);

凭证:

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'>

【问题讨论】:

  • 感谢@Thomas 的回复,是的,前端可以是 Javascript/AngularJS/jQuery,后端可以是 Java/Scala 和 Postgresql 数据库。
  • @Thomas 带有 java 后端的 javascript 前端一点也不晦涩。事实上,这很常见。

标签: java jquery json ajax postgresql


【解决方案1】:

我将首先在 java 中编写一个HTTP server,它稍后会接受来自 javascript 的 POST 请求。您还可以在其中添加某种形式的authentication,这样并不是每个人都可以添加记录。您也可以使用我会使用的framwork

只要 POST 请求带有 JSON 数据和所需的授权,您就可以让 Java 将 JSON 解析为数据库的 Insert 语句。

现在是 Javascript 部分:您最好使用 jQuery 发送带有 ajax 的 JSON 编码数据。使用 POST 请求。

在这种情况下,它看起来像 使用:

  • 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));

【讨论】:

    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2010-11-01
    • 2011-08-03
    • 1970-01-01
    相关资源
    最近更新 更多