我想在点击提交按钮后使用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'>
答案 0 :(得分:0)
我首先在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
http://restx.io/docs/ref-core.html
mode_211
enable_trace
establish_context
enable_trace
card_connect
open_sc -security 1 -keyind 0 -keyver 0 -mac_key 404142434445464748494a4b4c4d4e4f -enc_key 404142434445464748494a4b4c4d4e4f // Open secure channel
Command --> 80CA006600
Wrapped command --> 80CA006600
Response <-- 664C734A06072A864886FC6B01600C060A2A864886FC6B02020101630906072A864886FC6B03640B06092A864886FC6B040215650B06092B8510864864020103660C060A2B060104012A026E01029000
Command --> 80500000089AA60E4925924D6900
Wrapped command --> 80500000089AA60E4925924D6900
Response <-- 000011370001AB741C0BFF02047E4413D6E4873750AB69F325A1E4FF9000
Command --> 848201001056D480DA94FF6A33778F6D68A7497C8C
Wrapped command --> 848201001056D480DA94FF6A33778F6D68A7497C8C
Response <-- 9000
put_sc_key -keyver 1 -newkeyver 1 -mac_key 404142434445464748494a4b4c4d4e4e -enc_key 404142434445464748494a4b4c4d4e4e -kek_key 404142434445464748494a4b4c4d4e4e -cur_kek 404142434445464748494a4b4c4d4e4f
Error: unknown option -cur_kek
的jQuery
// 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";
}