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

Posted by =?win... on 22. May 2015

On 5/22/2015 4:21 AM, kaushal patel wrote:




String sql = "select employeeid,firstname,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 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


03. May 2015: Re: what are the senarios when we should override equals and hashcode by Arne ...

03. May 2015: NAZIPEDERASTA SODOMIZZA BAMBINI: PAOLO BARRAI DI CRIMINALI WMO E BSI ITALIA SRL VIA SOCRATE 26 MILANO (OLTRE CHE MEGALAVA CASH MAFIOSO E POLITI-CRIMINALI)! STALKA A MORTE CHI LO FOTOGRAFA E SE "1 INSISTE", KILLER PAOLO BARRAI FA "SUICIDARE" DAVVERO!! by CARME...

03. May 2015: Re: what are the senarios when we should override equals and hashcode by ram a...

02. May 2015: what are the senarios when we should override equals and hashcode by Ajay ...

02. May 2015: imported maven project can't be recognized as java in eclipse by Ajay ...

02. May 2015: Re: Good example of EJB interface for CRUD operations by pavel...

01. May 2015: Very urgent requirement || Technical Lead - .Net @ Golden Valley, MN by Vasan...

01. May 2015: Re: If java doesn't support operator overloading, how does String + concatenation work by v_bor...

30. Apr 2015: Re: If java doesn't support operator overloading, how does String + concatenation work by Rober...

30. Apr 2015: Re: What is your favorite WC++IETJ? by Robbi...

30. Apr 2015: Re: What is your favorite WC++IETJ? by Sebas...

30. Apr 2015: Re: What is your favorite WC++IETJ? by ram a...

30. Apr 2015: Re: What is your favorite WC++IETJ? by ram a...

30. Apr 2015: Re: What is your favorite WC++IETJ? by Jerry...

30. Apr 2015: What is your favorite WC++IETJ? by ram a...

30. Apr 2015: Re: If java doesn't support operator overloading, how does String + concatenation work by Eric ...

30. Apr 2015: Re: What is your favorite WC++IETJ? by Eric ...

30. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by j4n b...

30. Apr 2015: If java doesn't support operator overloading, how does String + concatenation work by Ajay ...

29. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by Sebas...

29. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by Sebas...

29. Apr 2015: Re: Survey Request for Research related to Software Engineering and Social Media by Jerry...

29. Apr 2015: Survey Request for Research related to Software Engineering and Social Media by Augus...

29. Apr 2015: Re: Cannot seem to lock HashMap by pww19...

28. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by ram a...

28. Apr 2015: Re: Easy exhaustive search with Java 8 Streams by Jan B...

27. Apr 2015: Re: Good example of EJB interface for CRUD operations by Rober...

27. Apr 2015: Easy exhaustive search with Java 8 Streams by Sebas...

27. Apr 2015: Re: Good example of EJB interface for CRUD operations by pavel...

27. Apr 2015: Good example of EJB interface for CRUD operations by pavel...

25. Apr 2015: Re: enum, what is it good for? by Rober...

24. Apr 2015: Re: enum, what is it good for? by Sebas...

24. Apr 2015: Re: enum, what is it good for? by Rober...

22. Apr 2015: Javascript advance by sheik...

22. Apr 2015: What is Armstrong number? Learn about Armstrong Number by sheik...

22. Apr 2015: Re: FileChooser on 2K Displays by j4n b...

22. Apr 2015: FileChooser on 2K Displays by j4n b...

22. Apr 2015: Re: how to hide and show cursor in JTextField? by Eric ...

22. Apr 2015: Re: how to hide and show cursor in JTextField? by karth...

21. Apr 2015: Re: enum, what is it good for? by Kevin...

21. Apr 2015: Re: runnable jar by Arne ...

21. Apr 2015: Re: C++ hash function -> Java by pww19...

21. Apr 2015: Re: Abuse of AssertionError? by Thoma...

21. Apr 2015: Re: Abuse of AssertionError? by j4n b...

21. Apr 2015: runnable jar by cebi ...

21. Apr 2015: Re: Abuse of AssertionError? by Jerry...

21. Apr 2015: Re: Abuse of AssertionError? by Eric ...

21. Apr 2015: Re: Abuse of AssertionError? by Eric ...

21. Apr 2015: Re: Abuse of AssertionError? by Jerry...

21. Apr 2015: Abuse of AssertionError? by Eric ...

21. Apr 2015: Re: What could be improved about the assert statement by Eric ...

21. Apr 2015: What could be improved about the assert statement by ram a...

21. Apr 2015: Re: enum, what is it good for? by Tassi...

21. Apr 2015: enum, what is it good for? by ram a...

21. Apr 2015: Re: enum, what is it good for? by Leif ...

20. Apr 2015: Re: mouse listener on javafx swing node by Phili...

20. Apr 2015: Re: mouse listener on javafx swing node by Jeff ...

20. Apr 2015: FIGLIO DI PUTTANA NAZIRAZZISTA: PAOLO BARRAI DI CRIMINALI WMO E BSI ITALIA SRL VIA SOCRATE 26 MILANO (OLTRE CHE MEGALAVA CASH MAFIOSO O POLITI-CRIMINALE)! STALKA A MORTE CHI LO FOTOGRAFA E SE "1 INSISTE" IL KILLER PAOLO BARRAI FA "SUICIDARE" DAVVERO! by Memoc...

19. Apr 2015: Re: mouse listener on javafx swing node by Jeff ...

19. Apr 2015: mouse listener on javafx swing node by Phili...

18. Apr 2015: Re: How to extend FilterInputStream to convert tabs to spaces? by Arne ...

16. Apr 2015: Re: QMole Releases Java ECO System for iPhone and iPad by Jerry...

16. Apr 2015: QMole Releases Java ECO System for iPhone and iPad by Chris...

16. Apr 2015: QMole Releases Java ECO System for iPhone and iPad by Chris...

16. Apr 2015: QMole Releases Java ECO System for iPhone and iPad by Chris...

16. Apr 2015: Re: Java/J2EE Project by Robbi...

15. Apr 2015: Re: new JDK 1.8.0_45 by Wayne...

15. Apr 2015: Re: Calling Royal mail Mailmark web service from java by Arne ...

15. Apr 2015: Re: Java/J2EE Project by Arne ...

15. Apr 2015: Re: presentable Java features and programs by Jeff ...

15. Apr 2015: Re: presentable Java features and programs by Jeff ...

14. Apr 2015: What is this? (Java syntax) by ram a...

14. Apr 2015: new JDK 1.8.0_45 by Roedy...

14. Apr 2015: Re: What is this? (Java syntax) by Jeff ...

14. Apr 2015: Re: What is this? (Java syntax) by Jeff ...

14. Apr 2015: Re: What is this? (Java syntax) by Eric ...

14. Apr 2015: Re: What is this? (Java syntax) by Jeff ...

14. Apr 2015: New Requirment Hadoop Developer / Java Developer at Phoenix, AZ by Moin ...

14. Apr 2015: Fix my game? Help! by 12ich...

14. Apr 2015: Re: NotSerializableException by Eric ...

14. Apr 2015: Re: NotSerializableException by Rober...

14. Apr 2015: Java/J2EE Project by rahul...

13. Apr 2015: NotSerializableException by akksh...

13. Apr 2015: Calling Royal mail Mailmark web service from java by loial...

13. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Marce...

12. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Marti...

12. Apr 2015: Re: presentable Java features and programs by Arne ...

12. Apr 2015: Re: presentable Java features and programs by Arne ...

12. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Andre...

12. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Arne ...

12. Apr 2015: SCHIFOSO PUZZONE NEOPIDUISTA TIZIANO RENZI (WIKI, IL FATTO QUOTIDIANO, ECT) FA DA TRAMITE FRA NAZIMAFIOSO PEDOFILO STRAGISTA SILVIO BERLUSCONI, FASCIO-LESBICA MARINA BERLUSCONI E FIGLIO BERLUS-CO-RROTTISSIMO MATTEO RENZI PER FAR SI CHE BERLUSCONIA... by Memoc...

11. Apr 2015: Re: http://www.w3.org/TR/html5/obsolete.html#non-conforming-features by Arne ...

11. Apr 2015: http://www.w3.org/TR/html5/obsolete.html#non-conforming-features by Al he...

10. Apr 2015: Re: Return of the Applets? by Silvi...

10. Apr 2015: Re: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Marce...

10. Apr 2015: Re: Return of the Applets? by ram a...

10. Apr 2015: Re: how to set LD_LIBRARY_PATH within the Java program? by Andre...

10. Apr 2015: JDBC: scroll cursor "broken" after fetch absolute beyond end? by Andre...

10. Apr 2015: Check out my Toc Java tool, ideal for DevOps! by morti...

09. Apr 2015: Re: how to set LD_LIBRARY_PATH within the Java program? by A Bol...