2016-01-18 69 views
0

我想保存我的json數據對象到我的Postgresql使用jQuery(ajax調用發佈請求)和Java後單擊提交按鈕,請幫我做到這一點?提前致謝。如何使用jQuery Post和Java在PostgreSQL中保存json數據?

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

感謝@Thomas的答覆,是的,前端可以是使用Javascript/AngularJS/jQuery和後端或者是的Java/Scala和PostgreSQL數據庫。 – Nag

+0

@Thomas關於具有java後端的JavaScript前端沒有什麼不明之處。其實這很常見。 – Kayaman

回答

0

我會寫一開始java中的,稍後接受來自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)); 
相關問題