研究了很多天,终于能够把数据库获取的数据成功转换成JsonObject对象
重要代码:
NoticeDaoImpl.java
try {
conn3 = DBUtil.getConnection();
st3 = conn3.createStatement();
rs3 = st3
.executequery("SELECT [noticeID],[range],[expiration],[Title],[info]"
+ ",[orgID],[depID],[usersID],[affix],[logDate]"
+ " FROM [WisdomOffice].[dbo].[Notice]");
result3.append("{'noticeList':[");
if (rs3 != null) {
while (rs3.next()) {
int ID = rs3.getInt(1);
String range = rs3.getString(2);
String expiration = rs3.getString(3);
String Title = rs3.getString(4);
String info = rs3.getString(5);
int orgID = rs3.getInt(6);
int depID = rs3.getInt(7);
int userID = rs3.getInt(8);
String affix = rs3.getString(9);
String logDate = rs3.getString(10);
if (info != null)
info = info.replaceAll("\\s","");
result3.append("{'noticeID':'" + ID + "','range':'" + range
+ "','expiration':'" + expiration + "','Title':'"
+ Title + "','info':'" + info + "','orgID':'"
+ orgID + "','depID':'" + depID + "','usersID':'"
+ userID + "','affix':'" + affix + "','logDate':'"
+ logDate + "'}");
if (!rs3.isLast()) {
result3.append(",");
}
}
}
result3.append("]}");
System.out.println(result3.toString());
} catch (sqlException e) {
// Todo auto-generated catch block
e.printstacktrace();
result3.append("{'operating':'查询出错了'}]}");
} finally {
DBUtil.freeDB(rs3,st3,conn3);
}
SbgStatisticsHandler.java
/**
* 查询所有
*
* @return
*/
public JsONObject handleIV() {
try {
System.out.println("11Try");
StringBuffer sb = impl.getNoticeAll();
JsONObject object9 = JsONObject.fromObject(sb.toString());
System.out.println(sb.toString());
return object9;
} catch (Exception e) {
System.out.println("erroratch");
e.printstacktrace();
String str = "{errorcode:'0',errormsg:'查询失败'}";
JsONObject object = JsONObject.fromObject(str);
return object;
}
}
TestServlet.java
@OverrIDe
protected voID service(httpServletRequest request,
httpServletResponse response) throws servletexception,IOException {
// Todo auto-generated method stub
response.setCharacterEnCoding("UTF-8");
response.setheader("content-type","text/HTML;charset=UTF-8");
PrintWriter writer = response.getWriter();
switch (4) {
case 0:// 查询公司名称
JsONObject JsonObject = handler.handle();
writer.write(JsonObject.toString());
break;
case 1:// 按ID查询
JsONObject object = handler.handleI(11);
writer.write(object.toString());
break;
case 2:// 按ID删除
JsONObject object1 = handler.handleII(188);
writer.write(object1.toString());
break;
case 3:// 按ID和userID更新
// object = handler.handleIII(189);
// writer = response.getWriter();
// writer.write(object.toString());
break;
case 4:// 查询所有
JsONObject object3 = handler.handleIV();
writer.write(object3.toString());
break;
}
}
在研究中遇到了一个问题:Json unterminated string at character
最后是因为我的sqlServer数据库中获取的数据里面有换行空格或制表符等特殊符号,我添加了这个判断后,就好了:
if (info != null)
info = info.replaceAll("\\s","");
2014.4.29
IDa
莹和视兴
总结以上是内存溢出为你收集整理的获取SqlServer数据库数据,转成JSON对象全部内容,希望文章能够帮你解决获取SqlServer数据库数据,转成JSON对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)