List of usage examples for javax.sql.rowset RowSetMetaDataImpl RowSetMetaDataImpl
RowSetMetaDataImpl
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); CachedRowSet crs = null;/*from w ww .j av a 2s . co m*/ RowSetMetaData rsMD = new RowSetMetaDataImpl(); rsMD.setColumnCount(2); rsMD.setColumnName(1, "id"); rsMD.setColumnType(1, Types.VARCHAR); rsMD.setColumnName(2, "name"); rsMD.setColumnType(2, Types.VARCHAR); // sets the designated column's table name, if any, to the given String. rsMD.setTableName(1, "survey"); rsMD.setTableName(2, "survey"); // use a custom made RowSetMetaData object for CachedRowSet object crs = new CachedRowSetImpl(); crs.setMetaData(rsMD); crs.moveToInsertRow(); crs.updateString(1, "1111"); crs.updateString(2, "alex"); crs.insertRow(); crs.moveToInsertRow(); crs.updateString(1, "2222"); crs.updateString(2, "jane"); crs.insertRow(); crs.moveToCurrentRow(); crs.acceptChanges(conn); conn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getHSQLConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,name varchar);"); st.executeUpdate("create view surveyView as (select * from survey);"); st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')"); st.executeUpdate("insert into survey (id,name ) values (2,'anotherValue')"); CachedRowSet crs = null;/*from w ww .j a va 2s . c om*/ RowSetMetaData rsMD = new RowSetMetaDataImpl(); rsMD.setColumnCount(2); rsMD.setColumnName(1, "id"); rsMD.setColumnType(1, Types.VARCHAR); rsMD.setColumnName(2, "name"); rsMD.setColumnType(2, Types.VARCHAR); // sets the designated column's table name, if any, to the given String. rsMD.setTableName(1, "survey"); rsMD.setTableName(2, "survey"); // use a custom made RowSetMetaData object for CachedRowSet object crs = new CachedRowSetImpl(); crs.setMetaData(rsMD); crs.moveToInsertRow(); crs.updateString(1, "1111"); crs.updateString(2, "alex"); crs.insertRow(); crs.moveToInsertRow(); crs.updateString(1, "2222"); crs.updateString(2, "jane"); crs.insertRow(); // if you want to commit changes from a CachedRowSet // object to your desired datasource, then you must // create a Connection object. // //conn = getHSQLConnection(); // moves the cursor to the remembered cursor position, usually // the current row. This method has no effect if the cursor is // not on the insert row. crs.moveToCurrentRow(); // when the method acceptChanges() is executed, the CachedRowSet // object's writer, a RowSetWriterImpl object, is called behind the // scenes to write the changes made to the rowset to the underlying // data source. The writer is implemented to make a connection to // the data source and write updates to it. crs.acceptChanges(conn); conn.close(); }
From source file:com.novartis.opensource.yada.format.ResultSetResultJSONConverter.java
/** * Converts data from a {@link java.sql.ResultSet} into a {@link JSONArray} containing * one {@link JSONObject} per row//ww w . j av a2 s . c o m * @param rs the result set containing the data to convert to JSON * @return a json array containing the data * @throws SQLException when iteration or access to {@code rs} fails */ protected JSONArray getJSONRows(ResultSet rs) throws SQLException { JSONArray rows = new JSONArray(); ResultSetMetaData rsmd = rs.getMetaData(); if (rsmd == null) rsmd = new RowSetMetaDataImpl(); List<String> convertedResult = new ArrayList<>(); while (rs.next()) { JSONObject row = new JSONObject(); String colValue; for (int i = 1; i <= rsmd.getColumnCount(); i++) { String origColName = rsmd.getColumnName(i); if (!origColName.toLowerCase().equals(JDBCAdaptor.ROWNUM_ALIAS)) { boolean harmonize = isHarmonized(); boolean prune = harmonize ? ((JSONObject) this.harmonyMap).has(Harmonizer.PRUNE) && ((JSONObject) this.harmonyMap).getBoolean(Harmonizer.PRUNE) : false; String col = origColName; if (harmonize) { if (((JSONObject) this.harmonyMap).has(origColName)) { col = ((JSONObject) this.harmonyMap).getString(origColName); } else if (prune) { col = ""; } } //TODO handle empty result set more intelligently // OLD WAY adds headers to empty object when rs is empty if (!"".equals(col)) { if (null == rs.getString(origColName) || NULL.equals(rs.getString(origColName))) { colValue = NULL_REPLACEMENT; } else { colValue = rs.getString(origColName); } row.put(col, colValue); } } } rows.put(row); convertedResult.add(row.toString()); } if (rows.length() > 0) { for (String key : JSONObject.getNames(rows.getJSONObject(0))) { getYADAQueryResult().addConvertedHeader(key); } getYADAQueryResult().getConvertedResults().add(convertedResult); } return rows; }
From source file:org.apache.hadoop.hive.jdbc.storagehandler.AtsdDBRecordReader.java
private ResultSet replaceDotsInColumnNames(ResultSet resultSet) throws SQLException { ResultSetMetaData metaData = resultSet.getMetaData(); int columnCount = metaData.getColumnCount(); if (columnCount > 0) { CachedRowSetImpl crs = new CachedRowSetImpl(); crs.populate(resultSet);//from w ww . j a va 2s . c o m RowSetMetaDataImpl rwsm = new RowSetMetaDataImpl(); rwsm.setColumnCount(columnCount); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnName(i); if (columnName.contains(".")) { columnName = columnName.replaceAll("\\.", "\\$"); } rwsm.setColumnName(i, columnName); rwsm.setColumnLabel(i, metaData.getColumnLabel(i)); rwsm.setCatalogName(i, metaData.getCatalogName(i)); rwsm.setColumnType(i, metaData.getColumnType(i)); rwsm.setColumnTypeName(i, metaData.getColumnTypeName(i)); rwsm.setSchemaName(i, metaData.getSchemaName(i)); rwsm.setTableName(i, metaData.getTableName(i)); } crs.setMetaData(rwsm); return crs; } return resultSet; }
From source file:Main.java
public void readData(RowSetInternal caller) throws SQLException { System.out.println("--- CustomRowSetReader: begin. ---"); if (caller == null) { System.out.println("CustomRowSetReader: caller is null."); return;/*from w w w. j av a2 s .co m*/ } CachedRowSet crs = (CachedRowSet) caller; // CachedRowSet crs = (CachedRowSet) caller.getOriginal(); RowSetMetaData rsmd = new RowSetMetaDataImpl(); rsmd.setColumnCount(3); rsmd.setColumnType(1, Types.VARCHAR); rsmd.setColumnType(2, Types.INTEGER); rsmd.setColumnType(3, Types.VARCHAR); rsmd.setColumnName(1, "col1"); rsmd.setColumnName(2, "col2"); rsmd.setColumnName(3, "col3"); crs.setMetaData(rsmd); System.out.println("CustomRowSetReader: crs.setMetaData( rsmd );"); crs.moveToInsertRow(); crs.updateString(1, "StringCol11"); crs.updateInt(2, 1); crs.updateString(3, "StringCol31"); crs.insertRow(); System.out.println("CustomRowSetReader: crs.insertRow() 1"); crs.updateString(1, "StringCol12"); crs.updateInt(2, 2); crs.updateString(3, "StringCol32"); crs.insertRow(); System.out.println("CustomRowSetReader: crs.insertRow() 2"); crs.moveToCurrentRow(); crs.beforeFirst(); displayRowSet(crs); crs.beforeFirst(); // crs.acceptChanges(); System.out.println("CustomRowSetReader: end."); }
From source file:com.novartis.opensource.yada.format.ResultSetResultXMLConverter.java
/** * Constructs an xml fragment of {@code ROW} elements with the result set data * @param rs the result set containing the data to be converted * @return a {@link DocumentFragment} containing the result data wrapped in XML * @throws SQLException when {@code rs} cannot be iterated or accessed *///from www . j a va 2s. c o m private DocumentFragment getXMLRows(ResultSet rs) throws SQLException { DocumentFragment rows = this.doc.createDocumentFragment(); ResultSetMetaData rsmd = rs.getMetaData(); if (rsmd == null) rsmd = new RowSetMetaDataImpl(); while (rs.next()) { Element row = this.doc.createElement(ROW); String colValue; for (int i = 1; i <= rsmd.getColumnCount(); i++) { String colName = rsmd.getColumnName(i); if (!colName.toLowerCase().equals(JDBCAdaptor.ROWNUM_ALIAS)) { String col = isHarmonized() && ((JSONObject) this.harmonyMap).has(colName) ? ((JSONObject) this.harmonyMap).getString(colName) : colName; if (null == rs.getString(colName) || NULL.equals(rs.getString(colName))) { colValue = NULL_REPLACEMENT; } else { colValue = rs.getString(colName); } Element column = this.doc.createElement(col); Text value = this.doc.createTextNode(colValue); column.appendChild(value); row.appendChild(column); } } rows.appendChild(row); } return rows; }
From source file:com.novartis.opensource.yada.format.ResultSetResultDelimitedConverter.java
/** * Converts columns of data in a {@link java.sql.ResultSet} to collection * of {@link List} objects containing values and stored in the current * {@link YADAQueryResult#getConvertedResults()} structure. * //from ww w . j a va2s .co m * @param rs * the result set to convert * @throws SQLException * when {@link ResultSet} or {@link ResultSetMetaData} iteration * fails */ protected void getDelimitedRows(ResultSet rs) throws SQLException { JSONObject h = (JSONObject) this.harmonyMap; ResultSetMetaData rsmd = rs.getMetaData(); if (rsmd == null) // TODO What happens to headers when rsmd is null, or // resultSet is empty? rsmd = new RowSetMetaDataImpl(); int colCount = rsmd.getColumnCount(); boolean hasYadaRnum = rsmd.getColumnName(colCount).toLowerCase().equals(JDBCAdaptor.ROWNUM_ALIAS); // handle headers // TODO How to suppress headers? for (int j = 1; j <= colCount; j++) { String colName = rsmd.getColumnName(j); if (!hasYadaRnum || !colName.toLowerCase().equals(JDBCAdaptor.ROWNUM_ALIAS)) { String col = colName; if (isHarmonized()) { if (h.has(colName)) { col = h.getString(colName); } } getYADAQueryResult().addConvertedHeader(this.wrap(col)); } } List<List<String>> convertedResult = new ArrayList<>(); while (rs.next()) { List<String> resultsRow = new ArrayList<>(); String colValue; for (int j = 1; j <= colCount; j++) { String colName = rsmd.getColumnName(j); if (!hasYadaRnum || !colName.toLowerCase().equals(JDBCAdaptor.ROWNUM_ALIAS)) { if (null == rs.getString(colName) || "null".equals(rs.getString(colName))) { colValue = NULL_REPLACEMENT; } else { colValue = this.wrap(rs.getString(colName)); } resultsRow.add(colValue); } } convertedResult.add(resultsRow); } getYADAQueryResult().getConvertedResults().add(convertedResult); }