是否可以在jsp中将String转换为ResultSet?

时间:2016-02-17 06:44:34

标签: java jsp

如何将List转换为结果集。是否可以进行此转换?

1 个答案:

答案 0 :(得分:-1)

我想你想要一个例子......试试这个代码

public class MyResultSet {

    public ResultSet getResultSet(List<String> headers, List<List<Object>> data) throws Exception {

        // validation
        if (headers == null || data == null) {
            throw new Exception("null parameters");
        }

        if (headers.size() != data.size()) {
            throw new Exception("parameters size are not equals");
        }

        // create a mock result set
        MockResultSet mockResultSet = new MockResultSet("myResultSet");

        // add header
        for (String string : headers) {         
            mockResultSet.addColumn(string);
        }

        // add data
        for (List<Object> list : data) {
            mockResultSet.addRow(list);
        }

        return mockResultSet;
    }
}


public class MyResultSetTest {

    private Logger logger = Logger.getLogger(MyResultSetTest.class);

    public MyResultSetTest() {
    }

    @Test
    public void testMyResultSet() throws Exception {
        MyResultSet myResultSet = new MyResultSet();
        List<String> headers = new ArrayList<String>();
        headers.add("id");
        headers.add("name");
        headers.add("salary");
        headers.add("age");

        List<List<Object>> data = new ArrayList<List<Object>>();
        for (int i = 0; i < 4; i++) {
            List<Object> objects = new ArrayList<Object>();
            objects.add(new Integer(i));
            objects.add("name " + i);
            objects.add(new Double(i));
            objects.add(new Integer(20 + i));
            data.add(objects);
        }

        ResultSet rs = myResultSet.getResultSet(headers, data);
        while (rs.next()) {
            logger.debug(rs.getString("id"));
            logger.debug(rs.getString(2));
            logger.debug(rs.getDouble("salary"));
            logger.debug(rs.getInt(4));
            logger.debug("------------------------");
        }

    }
}