【发布时间】:2014-05-09 10:12:52
【问题描述】:
我想获取所有表行并将它们添加到 hashmap 数组中,然后将它们转换为 json 对象以使用 ajax 和 jquery 发送到 jsp 页面。我在 jsp 页面中显示内容时遇到问题。 我已经设法编写了下面的代码,需要帮助来纠正/改进它,我很困惑如何在 jsp 页面中显示它。我是 jquery 和 json 的初学者。
try {
String res = request.getParameter("hello");
String user = "";
String pass = "";
String port = "";
String dbname = "";
String host = "";
String driver = "oracle.dbc.driver.OracleDriver";
Connection con;
PreparedStatement ps;
ResultSet rs;
String json = null;
String url = "jdbc:oracle:thin:@"+host+":"+port+":"+dbname;
Map<String,String> map = new HashMap<String,String>();
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,user,pass);
ps = con.prepareStatement("select * from employee");
rs = ps.executeQuery();
while(rs.next()){
map.put("name", rs.getString(1));
map.put("id", rs.getString(2));
map.put("salary",rs.getString(3));
list.add(map);
}
json = new Gson().toJson(list);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
}catch(Exception e){
}
} finally {
out.close();
}
index.jsp
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("#push").click(function(){
var value = $("#t").val();
$.post('jsonServlet',{hello:value},function(data){
// ??
// ??
// ??
});
});
});
</script>
</head>
<body style="position: absolute;min-height: 700px;min-width: 1300px">
<form>
<input type="button" value="PushMe" id="push">
<input type="text" id="t" value="my data">
<div style="position: absolute;height: 50%;width: 60%;" id="myDiv">
</div>
</form>
</body>
在jsp页面需要帮助才能在div中显示hash map的内容... 稍后我将使用文本框更改 div 以存储各个列的值,以便用户可以修改表的内容并再次更新表的行。
需要帮助
【问题讨论】:
标签: java javascript jquery json jsp