Created
March 30, 2022 19:13
-
-
Save recursivecodes/8a19fc0ca5ddb9eaa09296581fc18ddd to your computer and use it in GitHub Desktop.
Function.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Singleton | |
| public class Function extends OciFunction { | |
| @Inject | |
| DataSource dataSource; | |
| @ReflectiveAccess | |
| public String handleRequest() throws SQLException, JsonProcessingException { | |
| Connection conn = dataSource.getConnection(); | |
| Statement statement = conn.createStatement(); | |
| ResultSet resultSet = statement.executeQuery("select id, first_name, last_name from users"); | |
| return new ObjectMapper().writeValueAsString(convertResultSetToList(resultSet)); | |
| } | |
| private List<HashMap<String,Object>> convertResultSetToList(ResultSet rs) throws SQLException { | |
| ResultSetMetaData md = rs.getMetaData(); | |
| int columns = md.getColumnCount(); | |
| List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>(); | |
| while (rs.next()) { | |
| HashMap<String,Object> row = new HashMap<String, Object>(columns); | |
| for(int i=1; i<=columns; ++i) { | |
| row.put(md.getColumnName(i),rs.getObject(i)); | |
| } | |
| list.add(row); | |
| } | |
| return list; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment