Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31731 > unrolled thread

Can we convert a Java ResultSet/SqlRowSet from Database query directly to JSON array without Mapping it to a List<Type>?

Started bykaushal patel <kpt1991@gmail.com>
First post2015-05-22 01:21 -0700
Last post2015-05-22 21:15 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Can we convert a Java ResultSet/SqlRowSet from Database query directly to JSON array without Mapping it to a List<Type>? kaushal patel <kpt1991@gmail.com> - 2015-05-22 01:21 -0700
    Re: Can we convert a Java ResultSet/SqlRowSet from Database query directly to JSON array without Mapping it to a List<Type>? Arne Vajhøj <arne@vajhoej.dk> - 2015-05-22 21:15 -0400

#31731 — Can we convert a Java ResultSet/SqlRowSet from Database query directly to JSON array without Mapping it to a List<Type>?

Fromkaushal patel <kpt1991@gmail.com>
Date2015-05-22 01:21 -0700
SubjectCan we convert a Java ResultSet/SqlRowSet from Database query directly to JSON array without Mapping it to a List<Type>?
Message-ID<434cb251-ceeb-4a60-8205-caa8c8b7087f@googlegroups.com>
String sql = "select employee_id,first_name,last_name,email,salary from employees";

SqlRowSet  employees = null ;

try{
  employees = jdbcTemplate.queryForRowSet(sql);
}
catch(Exception e)
{
  e.printStackTrace();	
}
return employees ;

I want to know if there is any library present that can convert it to JSON.

I have tried with google GSON Library and Jackson Library but both requires first extraction of column to Type Class like Employee in this case.

If I am able to do this it can provide me a dynamic nature on my frontend like once we have 3 columns on frontend then we want 4 u just have to edit the query.

[toc] | [next] | [standalone]


#31733

FromArne Vajhøj <arne@vajhoej.dk>
Date2015-05-22 21:15 -0400
Message-ID<555fd4a6$0$291$14726298@news.sunsite.dk>
In reply to#31731
On 5/22/2015 4:21 AM, kaushal patel wrote:
> String sql = "select employee_id,first_name,last_name,email,salary from employees";
>
> SqlRowSet  employees = null ;
>
> try{
>    employees = jdbcTemplate.queryForRowSet(sql);
> }
> catch(Exception e)
> {
>    e.printStackTrace();	
> }
> return employees ;
>
> I want to know if there is any library present that can convert it to JSON.
>
> I have tried with google GSON Library and Jackson Library but both requires first extraction of column to Type Class like Employee in this case.

It is not hard to code yourself.

Quick attempt:

	private static String quoteIfNeeded(Object o) {
		if(o instanceof String) {
			return "\"" + o.toString() + "\"";
		} else if(o instanceof Integer) {
			return o.toString();
		} else {
			throw new IllegalArgumentException(o.getClass().getName() + " not 
supported");
		}
	}
	public static String resultSet2JSON(ResultSet rs) throws SQLException {
		ArrayList<String> colnams = new ArrayList<>();
		ResultSetMetaData rsmd = rs.getMetaData();
		for(int i = 0; i < rsmd.getColumnCount(); i++) {
			colnams.add(rsmd.getColumnName(i + 1));
		}
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		while(rs.next()) {
			sb.append("{");
			for(int i = 0; i < colnams.size(); i++) {
				if(i > 0) {
					sb.append(",");
				}
				sb.append(colnams.get(i));
				sb.append(":");
				Object o = rs.getObject(i + 1);
				sb.append(quoteIfNeeded(o));
			}
			sb.append("}");
		}
		sb.append("]");
		return sb.toString();
	}

Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web