尝试在数据库中查询数据并在datatables中异步显示时总是报错。有人帮我看下吗,好像是这个json的格式出问题,我看了firebug,应该是servlet返回的json数据格式问题,但因为新手,就是看不出哪里出了问题,有人帮忙看下吗?
先看下这个字符串拼接有问题吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | String datastr="{ "data":["; datastr = datastr + "{"stuNumber":""+stuNumber+""," + ""stuName":""+stuName+""," + ""stuNamed":""+stuNamed+""," + ""stuSex":""+stuSex+""," + ""stuNation":""+stuNation+""," + ""stuBirth":""+stuBirth+""," + ""stuNative":""+stuNative+""," + ""stuRegious":""+stuRegious+""," + ""stuPolitical":""+stuPolitical+""," + ""stuHealth":""+stuHealth+""," + ""stuSpecial":""+stuSpecial+""," + ""stuResidence":""+stuResidence+""," + ""stuSchoolRoll":""+stuSchoolRoll+""," + ""regiousSeat":""+regiousSeat+""," + ""stuID":""+stuID+""," + ""stuAddress":""+stuAddress+""," + ""stuMobile":""+stuMobile+""," + ""stuEmail":""+stuEmail+""," + ""stuEntranceWay":""+stuEntranceWay+""," + ""stuAlone":""+stuAlone+""," + ""stuMartyr":""+stuMartyr+""," + ""stuSingle":""+stuSingle+""," + ""stuFarmer":""+stuFarmer+""," + ""stuEnteranceDate":""+stuEnteranceDate+""," + ""stuClass":""+stuClass+""},"; } datastr=datastr.substring(0,datastr.length()-1); datastr=datastr+"]}"; |
下面是完整的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | The JQuery-datatables alert errors all time. this is the servlet: package servlet; import bean.Student; import dao.StudentDAO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.SQLException; /** * * */ public class GetStuInfoServlet extends HttpServlet { private String stuNumber; private String stuName; private String stuNamed; private String stuSex; private String stuNation; private String stuBirth; private String stuNative; private String stuRegious; private String stuPolitical; private String stuHealth; private String stuSpecial; private String stuResidence; private String stuSchoolRoll; private String regiousSeat; private String stuID; private String stuAddress; private String stuMobile; private String stuEmail; private String stuEntranceWay; private String stuAlone; private String stuMartyr; private String stuSingle; private String stuFarmer; private String stuEnteranceDate; private String stuClass; String datastr="{ "data":[";//ajax json public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{ request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("application/json; charset=utf-8"); String classNumbers = null; String stuNumbers = null; ResultSet resultSet = null; classNumbers = request.getParameter("classNumber"); StudentDAO studentDAO = new StudentDAO(); Student student = null; PrintWriter out = response.getWriter(); out.flush(); try { resultSet = studentDAO.getStudentByClassNumber(classNumbers); } catch (Exception e) { e.printStackTrace(); } resultSet = studentDAO.getStudentByStuNumber(stuNumber); resultSet = studentDAO.getStudentByClassNumber(classNumbers); try { while (resultSet != null & resultSet.next()){ this.stuNumber = resultSet.getString("stu_number"); this.stuName = resultSet.getString("stu_name"); this.stuNamed = resultSet.getString("stu_named"); this.stuSex = resultSet.getString("stu_sex"); this.stuNation = resultSet.getString("stu_nation"); this.stuBirth = resultSet.getString("stu_birth"); this.stuNative = resultSet.getString("stu_native"); this.stuRegious = resultSet.getString("stu_religious"); this.stuPolitical = resultSet.getString("stu_political"); this.stuHealth = resultSet.getString("stu_health"); this.stuSpecial = resultSet.getString("stu_special"); this.stuResidence = resultSet.getString("stu_residence"); this.stuSchoolRoll = resultSet.getString("stu_school_roll"); this.regiousSeat = resultSet.getString("regious_seat"); this.stuID = resultSet.getString("stu_id"); this.stuAddress = resultSet.getString("stu_adress"); this.stuMobile = resultSet.getString("stu_mobile"); this.stuEmail = resultSet.getString("stu_email"); this.stuEntranceWay = resultSet.getString("stu_entrance_way"); this.stuAlOne= resultSet.getString("stu_alone"); this.stuMartyr = resultSet.getString("stu_martyr"); this.stuSingle = resultSet.getString("stu_single"); this.stuFarmer = resultSet.getString("stu_farmer"); this.stuEnteranceDate = resultSet.getString("stu_enterance_date"); this.stuClass = resultSet.getString("stu_class"); if (stuNumber==null){stuNumber="";} if (stuName==null){stuName="";} if (stuNamed==null){stuNamed="";} if (stuSex==null){stuSex="";} if (stuNation==null){stuNation="";} if (stuBirth==null){stuBirth="";} if (stuNative==null){stuNative="";} if (stuRegious==null){stuRegious="";} if (stuPolitical==null){stuPolitical="";} if (stuHealth==null){stuHealth="";} if (stuSpecial==null){stuSpecial="";} if (stuResidence==null){stuResidence="";} if (stuSchoolRoll==null){stuSchoolRoll="";} if (regiousSeat==null){regiousSeat="";} if (stuID ==null){stuID ="";} if (stuAddress==null){stuAddress="";} if (stuMobile==null){stuMobile="";} if (stuEmail==null){stuEmail="";} if (stuEntranceWay==null){stuEntranceWay="";} if (stuAlOne==null){stuAlOne="";} if (stuMartyr==null){stuMartyr="";} if (stuSingle==null){stuSingle="";} if (stuFarmer==null){stuFarmer="";} if (stuEnteranceDate==null){stuEnteranceDate="";} if (stuClass==null){stuClass="";} datastr = datastr + "{"stuNumber":""+stuNumber+""," + ""stuName":""+stuName+""," + ""stuNamed":""+stuNamed+""," + ""stuSex":""+stuSex+""," + ""stuNation":""+stuNation+""," + ""stuBirth":""+stuBirth+""," + ""stuNative":""+stuNative+""," + ""stuRegious":""+stuRegious+""," + ""stuPolitical":""+stuPolitical+""," + ""stuHealth":""+stuHealth+""," + ""stuSpecial":""+stuSpecial+""," + ""stuResidence":""+stuResidence+""," + ""stuSchoolRoll":""+stuSchoolRoll+""," + ""regiousSeat":""+regiousSeat+""," + ""stuID":""+stuID+""," + ""stuAddress":""+stuAddress+""," + ""stuMobile":""+stuMobile+""," + ""stuEmail":""+stuEmail+""," + ""stuEntranceWay":""+stuEntranceWay+""," + ""stuAlone":""+stuAlone+""," + ""stuMartyr":""+stuMartyr+""," + ""stuSingle":""+stuSingle+""," + ""stuFarmer":""+stuFarmer+""," + ""stuEnteranceDate":""+stuEnteranceDate+""," + ""stuClass":""+stuClass+""},"; } } catch (SQLException e) { e.printStackTrace(); }finally { datastr=datastr.substring(0,datastr.length()-1); datastr=datastr+"]}"; } out.flush(); out.write(datastr); out.close(); } public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ this.doPost(request,response); } } And the studentDAO: package dao; import bean.Student; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; /** * Created by Emitor on 16-12-7. */ public class StudentDAO { private Connection conn ; private Student student = null; public void initConnection() throws Exception { Class.forName("com.mysql.jdbc.Driver"); cOnn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysystem","yupei","yupei"); } public void closeConnection() throws Exception { conn.close(); } public ResultSet getStudentByClassNumber(String classNumber)throws Exception{ initConnection(); String sql = "SELECT * FROM stu_info WHERE stu_class = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,classNumber); ResultSet resultSet = ps.executeQuery(); ps.close(); closeConnection(); return resultSet; } public ResultSet getStudentByStuNumber(String stuNumber)throws Exception{ initConnection(); String sql = "SELECT * FROM stu_info WHERE stu_number = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,stuNumber); ResultSet resultSet = ps.executeQuery(); ps.close(); closeConnection(); return resultSet; } } I try to use firebug to find the problem, it seems like not the right Json response. But I am really a new guy in Json. so I feel so confused now. Anyone help me to find the bug, please. Thanks a lot in advance. This is the jsp: <%@ page cOntentType="text/html;charset=UTF-8" language="java" %>
|