Skip to content

Instantly share code, notes, and snippets.

@recursivecodes
Created March 30, 2022 19:13
Show Gist options
  • Select an option

  • Save recursivecodes/8a19fc0ca5ddb9eaa09296581fc18ddd to your computer and use it in GitHub Desktop.

Select an option

Save recursivecodes/8a19fc0ca5ddb9eaa09296581fc18ddd to your computer and use it in GitHub Desktop.
Function.java
@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