从java类返回列表到servlet

时间:2015-07-23 09:03:06

标签: java servlets gson

我有一个包含三列employee的表eid,ename,esalaryselectclass.java从数据库test检索数据,并在名为rows的列表中填充值,然后我把名单归还。

在我的servlet selectclass中,我创建了java类的实例,并尝试从类(我将列表中)返回的列表rows检索到json但是它不起作用:它显示cannot find symbol of variable rows

我该怎么解决? 的servlet     包com.mvc.servlets;

import com.google.gson.Gson;
import com.mvc.javaclassbeans.employeeselect;
import com.mvc.javaclasses.selectclass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "selectservlet", urlPatterns = {"/selectservlet"})
public class selectservlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();


       try {
           selectclass sclass=  new selectclass();

         Gson gson = new Gson();
        String json = gson.toJson(sclass.selectfunction(obj));
        out.print(json);

       }catch (SQLException ex) {
            Logger.getLogger(selectservlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        out.print("sucess");
    }
    catch (Exception ex) {
            Logger.getLogger(myservlet.class.getName()).log(Level.SEVERE, null, ex);
        }
}

2 个答案:

答案 0 :(得分:1)

使用GSON库。这是示例代码

String json = new Gson().toJson(list);

试一试

将您的Java代码重写为

package com.mvc.javaclasses;

    import com.google.gson.Gson;
    import com.mvc.javaclassbeans.employee;
    import com.mvc.javaclassbeans.employeeselect;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import org.json.*;


    public class selectclass {

        PreparedStatement ps;
        public String query;
        Statement stmt = null;

        public String selectfunction(employeeselect ob) throws ClassNotFoundException, SQLException {

                dbconnector dbc = new dbconnector();
                Connection connection = dbc.Open();

                List rows = new ArrayList();
                Map row = null;

                try {
                    ResultSet resultSet = stmt.executeQuery(query);
                    ResultSetMetaData metaData = resultSet.getMetaData();
                    int numColumns = metaData.getColumnCount();

                    while (resultSet.next()) {
                        row = new HashMap();
                        for (int i = 1; i < numColumns + 1; i++) {
                            row.put(metaData.getColumnName(i), resultSet.getObject(i));
                        }
                        rows.add(row);
                    }

                    resultSet.close();
                } catch (Exception e) {

                }
                String json = new Gson().toJson(rows);
                  return json;


               // return true;
            }


        }

和servlet

import com.mvc.javaclassbeans.employeeselect;
import com.mvc.javaclasses.selectclass;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet(name = "selectservlet", urlPatterns = {"/selectservlet"})
public class selectservlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
     PrintWriter out = response.getWriter();


       try {
           selectclass sclass=  new selectclass();


        String json =sclass.selectfunction(obj);
        out.print(json);

       }catch (SQLException ex) {
            Logger.getLogger(selectservlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        out.print("sucess");
    }
    catch (Exception ex) {
            Logger.getLogger(myservlet.class.getName()).log(Level.SEVERE, null, ex);
        }
}

答案 1 :(得分:0)

而不是

    String json = gson.toJson(sclass.rows);

这样做

    String json = gson.toJson(sclass.selectfunction(obj));