📄 jdbcdisplayutil.java
字号:
static public void ShowWarnings(PrintStream out, Statement s) { try { // GET STATEMENT WARNINGS SQLWarning warning = null; if (s != null) { ShowWarnings(out, s.getWarnings()); } if (s != null) { s.clearWarnings(); } } catch (SQLException e) { ShowSQLException(out, e); } } // ShowStatementWarnings static public void DisplayResults(PrintStream out, Statement stmt, Connection conn ) throws SQLException { indent_DisplayResults( out, stmt, conn, 0); } static private void indent_DisplayResults (PrintStream out, Statement stmt, Connection conn, int indentLevel) throws SQLException { checkNotNull(stmt, "Statement"); ResultSet rs = stmt.getResultSet(); if (rs != null) { indent_DisplayResults(out, rs, conn, indentLevel); rs.close(); // let the result set go away } else { DisplayUpdateCount(out,stmt.getUpdateCount(), indentLevel); } ShowWarnings(out,stmt); } // DisplayResults static void DisplayUpdateCount(PrintStream out, int count, int indentLevel ) { if (count == 1) { indentedPrintLine( out, indentLevel, "1 row inserted/updated/deleted"); } else if (count >= 0) { indentedPrintLine( out, indentLevel, count+" rows inserted/updated/deleted"); } else { indentedPrintLine( out, indentLevel, "Statement executed."); } } static public void DisplayResults(PrintStream out, ResultSet rs, Connection conn) throws SQLException { indent_DisplayResults( out, rs, conn, 0); } static private void indent_DisplayResults (PrintStream out, ResultSet rs, Connection conn, int indentLevel) throws SQLException { ResultSetMetaData rsmd = rs.getMetaData(); checkNotNull(rsmd, "ResultSetMetaData"); Vector nestedResults; int numberOfRowsSelected = 0; // autocommit must be off or the nested cursors // are closed when the outer statement completes. if (!conn.getAutoCommit()) nestedResults = new Vector(); else nestedResults = null; int len = indent_DisplayBanner(out,rsmd, indentLevel); // When displaying rows, keep going past errors // unless/until the maximum # of errors is reached. boolean doNext = true; int retry = 0; while (doNext) { try { doNext = rs.next(); if (doNext) { DisplayRow(out, rs, rsmd, len, nestedResults, conn, indentLevel); ShowWarnings(out, rs); numberOfRowsSelected++; } } catch (SQLException e) { // REVISIT: might want to check the exception // and for some, not bother with the retry. if (++retry > MAX_RETRIES) throw e; else ShowSQLException(out, e); } } if (showSelectCount == true) { if (numberOfRowsSelected == 1) { out.println(); indentedPrintLine( out, indentLevel, "1 row selected"); } else if (numberOfRowsSelected >= 0) { out.println(); indentedPrintLine( out, indentLevel, numberOfRowsSelected + " rows selected"); } } DisplayNestedResults(out, nestedResults, conn, indentLevel ); nestedResults = null; } static private void DisplayNestedResults(PrintStream out, Vector nr, Connection conn, int indentLevel ) throws SQLException { if (nr == null) return; String s="+ ResultSet #"; String b="++++++++++++++++"; String oldString="0"; for (int i=0; i < nr.size(); i++) { System.out.println(); //just too clever to get the extra +s String t = Integer.toString(i); if (t.length() > oldString.length()) { oldString = t; b=b+"+"; } System.out.println(b); System.out.println(s+i+" +"); System.out.println(b); indent_DisplayResults(out, (ResultSet) nr.elementAt(i), conn, indentLevel); } } static public void DisplayNextRow(PrintStream out, ResultSet rs, Connection conn ) throws SQLException { indent_DisplayNextRow( out, rs, conn, 0 ); } static private void indent_DisplayNextRow(PrintStream 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 static public void DisplayCurrentRow(PrintStream out, ResultSet rs, Connection conn ) throws SQLException { indent_DisplayCurrentRow( out, rs, conn, 0 ); } static private void indent_DisplayCurrentRow(PrintStream 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 static public int DisplayBanner(PrintStream out, ResultSetMetaData rsmd ) throws SQLException { return indent_DisplayBanner( out, rsmd, 0 ); } static private int indent_DisplayBanner(PrintStream 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, rsmd.getColumnDisplaySize(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), rsmd.getColumnDisplaySize(i))); if (s.length() < w) { // build a string buffer to hold the whitespace StringBuffer blanks = new StringBuffer(s); blanks.ensureCapacity(w); // try to paste on big chunks of space at a time. for (int k=blanks.length()+64; k<=w; k+=64) blanks.append( " "); for (int k=blanks.length()+16; k<=w; k+=16) blanks.append(" "); for (int k=blanks.length()+4; k<=w; k+=4) blanks.append(" "); for (int k=blanks.length(); k<w; k++) blanks.append(' '); buf.append(blanks); // REMIND: could do more cleverness, like keep around // past buffers to reuse... } 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 static private void DisplayRow(PrintStream 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 = rs.getString(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 = "ResultSet #"+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, rsmd.getColumnDisplaySize(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 static public void doTrace(PrintStream out, Exception e) { if (Boolean.getBoolean("ij.exceptionTrace")) { e.printStackTrace(out); out.flush(); } } static private void indentedPrintLine( PrintStream out, int indentLevel, String text ) { indent( out, indentLevel ); out.println( text ); } static private void indentedPrintLine( PrintStream out, int indentLevel, StringBuffer text ) { indent( out, indentLevel ); out.println( text ); } static private void indent( PrintStream out, int indentLevel ) { for ( int ictr = 0; ictr < indentLevel; ictr++ ) { out.print( " " ); } } // ==========================}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -