📄 jdbcdisplayutil.java
字号:
Fetch the next row of the result set, and if it exists format and display a banner and the row. @param out the place to write to @param rs the ResultSet in use @param conn the Connection against which the ResultSet was retrieved @exception SQLException on JDBC access failure */ static public void DisplayNextRow(PrintWriter out, ResultSet rs, Connection conn ) throws SQLException { indent_DisplayNextRow( out, rs, conn, 0 ); } static private void indent_DisplayNextRow(PrintWriter out, ResultSet rs, Connection conn, int indentLevel ) throws SQLException { Vector nestedResults; // autocommit must be off or the nested cursors // are closed when the outer statement completes. if (!conn.getAutoCommit()) nestedResults = new Vector(); else nestedResults = null; checkNotNull(rs, "ResultSet"); ResultSetMetaData rsmd = rs.getMetaData(); checkNotNull(rsmd, "ResultSetMetaData"); // Only print stuff out if there is a row to be had. if (rs.next()) { int rowLen = indent_DisplayBanner(out, rsmd, indentLevel); DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel ); } else { indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow")); } ShowWarnings(out, rs); DisplayNestedResults(out, nestedResults, conn, indentLevel ); nestedResults = null; } // DisplayNextRow /** Display the current row of the result set along with a banner. Assume the result set is on a row. @param out the place to write to @param rs the ResultSet in use @param conn the Connection against which the ResultSet was retrieved @exception SQLException on JDBC access failure */ static public void DisplayCurrentRow(PrintWriter out, ResultSet rs, Connection conn ) throws SQLException { indent_DisplayCurrentRow( out, rs, conn, 0 ); } static private void indent_DisplayCurrentRow(PrintWriter out, ResultSet rs, Connection conn, int indentLevel ) throws SQLException { Vector nestedResults; if (rs == null) { indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_NoCurreRow_19")); return; } // autocommit must be off or the nested cursors // are closed when the outer statement completes. if (!conn.getAutoCommit()) nestedResults = new Vector(); else nestedResults = null; ResultSetMetaData rsmd = rs.getMetaData(); checkNotNull(rsmd, "ResultSetMetaData"); int rowLen = indent_DisplayBanner(out, rsmd, indentLevel); DisplayRow(out, rs, rsmd, rowLen, nestedResults, conn, indentLevel ); ShowWarnings(out, rs); DisplayNestedResults(out, nestedResults, conn, indentLevel ); nestedResults = null; } // DisplayNextRow /** Print a banner containing the column labels separated with '|'s and a line of '-'s. Each field is as wide as the display width reported by the metadata. @param out the place to write to @param rsmd the ResultSetMetaData to use @exception SQLException on JDBC access failure */ static public int DisplayBanner(PrintWriter out, ResultSetMetaData rsmd ) throws SQLException { return indent_DisplayBanner( out, rsmd, 0 ); } static private int indent_DisplayBanner(PrintWriter out, ResultSetMetaData rsmd, int indentLevel ) throws SQLException { StringBuffer buf = new StringBuffer(); int numCols = rsmd.getColumnCount(); int rowLen; // do some precalculation so the buffer is allocated only once // buffer is twice as long as the display length plus one for a newline rowLen = (numCols - 1); // for the column separators for (int i=1; i <= numCols; i++) { rowLen += Math.min(maxWidth, Math.max((rsmd.isNullable(i) == ResultSetMetaData.columnNoNulls)? 0 : MINWIDTH, LocalizedResource.getInstance().getColumnDisplaySize(rsmd, i))); } buf.ensureCapacity(rowLen); // get column header info // truncate it to the column display width // add a bar between each item. for (int i=1; i <= numCols; i++) { if (i>1) buf.append('|'); String s = rsmd.getColumnLabel(i); int w = Math.min(maxWidth, Math.max(((rsmd.isNullable(i) == ResultSetMetaData.columnNoNulls)? 0 : MINWIDTH), LocalizedResource.getInstance().getColumnDisplaySize(rsmd, i))); if (s.length() < w) { buf.append(s); // try to paste on big chunks of space at a time. int k = w - s.length(); for (; k >= 64; k -= 64) buf.append( " "); for (; k >= 16; k -= 16) buf.append(" "); for (; k >= 4; k -= 4) buf.append(" "); for (; k > 0; k--) buf.append(' '); } else if (s.length() > w) { if (w > 1) buf.append(s.substring(0,w-1)); if (w > 0) buf.append('&'); } else { buf.append(s); } } buf.setLength(Math.min(rowLen, 1024)); indentedPrintLine( out, indentLevel, buf); // now print a row of '-'s for (int i=0; i<Math.min(rowLen, 1024); i++) buf.setCharAt(i, '-'); indentedPrintLine( out, indentLevel, buf); buf = null; return rowLen; } // DisplayBanner /** Print one row of a result set, padding each field to the display width and separating them with '|'s @param out the place to write to @param rs the ResultSet to use @param rsmd the ResultSetMetaData to use @param rowLen @param nestedResults @param conn @param indentLevel number of tab stops to indent line @exception SQLException thrown on JDBC access failure */ static private void DisplayRow(PrintWriter out, ResultSet rs, ResultSetMetaData rsmd, int rowLen, Vector nestedResults, Connection conn, int indentLevel ) throws SQLException { StringBuffer buf = new StringBuffer(); buf.ensureCapacity(rowLen); int numCols = rsmd.getColumnCount(); int i; // get column header info // truncate it to the column display width // add a bar between each item. for (i=1; i <= numCols; i++){ if (i>1) buf.append('|'); String s; switch (rsmd.getColumnType(i)) { default: s = LocalizedResource.getInstance().getLocalizedString(rs, rsmd, i ); break; case org.apache.derby.iapi.reference.JDBC20Translation.SQL_TYPES_JAVA_OBJECT: case Types.OTHER: { Object o = rs.getObject(i); if (o == null) { s = "NULL"; } else if (o instanceof ResultSet && nestedResults != null) { s = LocalizedResource.getMessage("UT_Resul0_20", LocalizedResource.getNumber(nestedResults.size())); nestedResults.addElement(o); } else { try { s = rs.getString(i); } catch (SQLException se) { // oops, they don't support refetching the column s = o.toString(); } } } break; } if (s==null) s = "NULL"; int w = Math.min(maxWidth, Math.max((rsmd.isNullable(i) == ResultSetMetaData.columnNoNulls)? 0 : MINWIDTH, LocalizedResource.getInstance().getColumnDisplaySize(rsmd, i))); if (s.length() < w) { StringBuffer fullS = new StringBuffer(s); fullS.ensureCapacity(w); for (int k=s.length(); k<w; k++) fullS.append(' '); s = fullS.toString(); } else if (s.length() > w) // add the & marker to know it got cut off s = s.substring(0,w-1)+"&"; buf.append(s); } indentedPrintLine( out, indentLevel, buf); } // DisplayRow /** Check if an object is null, and if it is, throw an exception with an informative parameter about what was null. The exception is a run-time exception that is internal to ij. @param o the object to test @param what the information to include in the error if it is null */ public static void checkNotNull(Object o, String what) { if (o == null) { throw ijException.objectWasNull(what); } } // checkNotNull /** Map the string to the value if it is null. @param s the string to test for null @param nullValue the value to use if s is null @return if s is non-null, s; else nullValue. */ static public String mapNull(String s, String nullValue) { if (s==null) return nullValue; return s; } /** If the property ij.exceptionTrace is true, display the stack trace to the print stream. Otherwise, do nothing. @param out the output stream to write to @param e the exception to display */ static public void doTrace(PrintWriter out, Exception e) { if (Boolean.getBoolean("ij.exceptionTrace")) { e.printStackTrace(out); out.flush(); } } static public void setMaxDisplayWidth(int maxDisplayWidth) { maxWidth = maxDisplayWidth; } static private void indentedPrintLine( PrintWriter out, int indentLevel, String text ) { indent( out, indentLevel ); out.println( text ); } static private void indentedPrintLine( PrintWriter out, int indentLevel, StringBuffer text ) { indent( out, indentLevel ); out.println( text ); } static private void indent( PrintWriter out, int indentLevel ) { for ( int ictr = 0; ictr < indentLevel; ictr++ ) { out.print( " " ); } } // ================ static public void ShowException(PrintStream out, Throwable e) { if (e == null) return; if (e instanceof SQLException) ShowSQLException(out, (SQLException)e); else e.printStackTrace(out); } static public void ShowSQLException(PrintStream out, SQLException e) { String errorCode; if (Boolean.getBoolean("ij.showErrorCode")) { errorCode = " (errorCode = " + e.getErrorCode() + ")"; } else { errorCode = ""; } while (e!=null) { out.println("ERROR "+mapNull(e.getSQLState(),"(no SQLState)")+": "+ mapNull(e.getMessage(),"(no message)")+errorCode); doTrace(out, e); e=e.getNextException(); } } static public void ShowWarnings(PrintStream out, Connection theConnection) { try { // GET CONNECTION WARNINGS SQLWarning warning = null; if (theConnection != null) { ShowWarnings(out, theConnection.getWarnings()); } if (theConnection != null) { theConnection.clearWarnings(); } } catch (SQLException e) { ShowSQLException(out, e); } } // ShowWarnings static public void ShowWarnings(PrintStream out, SQLWarning warning) { while (warning != null) { out.println("WARNING "+ mapNull(warning.getSQLState(),"(no SQLState)")+": "+ mapNull(warning.getMessage(),"(no message)")); warning = warning.getNextWarning(); } } static public void ShowWarnings(PrintStream out, ResultSet rs) { try { // GET RESULTSET WARNINGS SQLWarning warning = null; if (rs != null) { ShowWarnings(out, rs.getWarnings()); } if (rs != null) { rs.clearWarnings(); } } catch (SQLException e) { ShowSQLException(out, e); } } // ShowResultSetWarnings
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -