📄 jdbcdisplayutil.java
字号:
/* Derby - Class org.apache.derby.tools.JDBCDisplayUtil Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.tools;import java.io.PrintStream;import java.io.PrintWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.sql.Connection;import java.sql.SQLException;import java.sql.SQLWarning;import java.sql.Statement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.Types;import java.util.Properties;import java.util.Enumeration;import java.util.Vector;import org.apache.derby.iapi.tools.i18n.LocalizedResource;import org.apache.derby.impl.tools.ij.ijException;/** This class contains utility methods for displaying JDBC objects and results. <p> All of the methods are static. The output stream to write to is always passed in, along with the JDBC objects to display. @author ames */public class JDBCDisplayUtil { // used to control display static final private int MINWIDTH = 4; static private int maxWidth = 128; static public boolean showSelectCount = false; static { // initialize the locale support functions to default value of JVM LocalizedResource.getInstance(); } //----------------------------------------------------------------- // Methods for initialization resource bundle and codeset's output /** * init method - will init the class to support a locale and * codeset based on the derby.ui.locale and derby.ui.codeset * properties if exists or using the default values from the JVM. */ static public boolean init() { return (LocalizedResource.getInstance() != null); } /** * init method - will init the class to support a locale and * codeset based on the derby.ui.locale properties and on the * given codeset if exists or using the default values from the JVM. */ public static boolean init(String codeset) { return init(codeset, null); } /** * init method - will init the class to support a locale and * codeset based on the given codeset and locale. * If the parameters are null it will try to init use derby.ui.locale * and derby.ui.codeset properties if exists or using the default * values from the JVM. */ public static boolean init(String pCodeset, String pLocale) { LocalizedResource.getInstance().init(pCodeset, pLocale,null); return true; } //----------------------------------------------------------------- // Methods for displaying and checking errors /** Print information about the exception to the given PrintWriter. For non-SQLExceptions, does a stack trace. For SQLExceptions, print a standard error message and walk the list, if any. @param out the place to write to @param e the exception to display */ static public void ShowException(PrintWriter out, Throwable e) { if (e == null) return; if (e instanceof SQLException) ShowSQLException(out, (SQLException)e); else e.printStackTrace(out); } /** Print information about the SQL exception to the given PrintWriter. Walk the list of exceptions, if any. @param out the place to write to @param e the exception to display */ static public void ShowSQLException(PrintWriter out, SQLException e) { String errorCode; if (Boolean.getBoolean("ij.showErrorCode")) { errorCode = LocalizedResource.getMessage("UT_Error0", LocalizedResource.getNumber(e.getErrorCode())); } else { errorCode = ""; } while (e!=null) { String p1 = mapNull(e.getSQLState(),LocalizedResource.getMessage("UT_NoSqlst")); String p2 = mapNull(e.getMessage(),LocalizedResource.getMessage("UT_NoMessa")); out.println(LocalizedResource.getMessage("UT_Error012", p1, p2,errorCode)); doTrace(out, e); e=e.getNextException(); } } /** Print information about the SQL warnings for the connection to the given PrintWriter. Walks the list of exceptions, if any. @param out the place to write to @param theConnection the connection that may have warnings. */ static public void ShowWarnings(PrintWriter 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 /** @param out the place to write to @param warning the SQLWarning */ static public void ShowWarnings(PrintWriter out, SQLWarning warning) { while (warning != null) { String p1 = mapNull(warning.getSQLState(),LocalizedResource.getMessage("UT_NoSqlst_7")); String p2 = mapNull(warning.getMessage(),LocalizedResource.getMessage("UT_NoMessa_8")); out.println(LocalizedResource.getMessage("UT_Warni01", p1, p2)); warning = warning.getNextWarning(); } } /** Print information about the SQL warnings for the ResultSet to the given PrintWriter. Walk the list of exceptions, if any. @param out the place to write to @param rs the ResultSet that may have warnings on it */ static public void ShowWarnings(PrintWriter 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 /** Print information about the SQL warnings for the Statement to the given PrintWriter. Walks the list of exceptions, if any. @param out the place to write to @param s the Statement that may have warnings on it */ static public void ShowWarnings(PrintWriter 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 //----------------------------------------------------------------- // Methods for displaying and checking results // REMIND: make this configurable... static final private int MAX_RETRIES = 0; /** Pretty-print the results of a statement that has been executed. If it is a select, gathers and prints the results. Display partial results up to the first error. If it is not a SELECT, determine if rows were involved or not, and print the appropriate message. @param out the place to write to @param stmt the Statement to display @param conn the Connection against which the statement was executed @exception SQLException on JDBC access failure */ static public void DisplayResults(PrintWriter out, Statement stmt, Connection conn ) throws SQLException { indent_DisplayResults( out, stmt, conn, 0); } static private void indent_DisplayResults (PrintWriter 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 /** @param out the place to write to @param count the update count to display @param indentLevel number of tab stops to indent line */ static void DisplayUpdateCount(PrintWriter out, int count, int indentLevel ) { if (count == 1) { indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_1RowInserUpdatDelet")); } else if (count >= 0) { indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_0RowsInserUpdatDelet", LocalizedResource.getNumber(count))); } else { indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_StateExecu")); } } /** @param out the place to write to @param rs the ResultSet to display @param conn the Connection against which the ResultSet was retrieved @exception SQLException on JDBC access failure */ static public void DisplayResults(PrintWriter out, ResultSet rs, Connection conn) throws SQLException { indent_DisplayResults( out, rs, conn, 0); } static private void indent_DisplayResults (PrintWriter 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, LocalizedResource.getMessage("UT_1RowSelec")); } else if (numberOfRowsSelected >= 0) { out.println(); indentedPrintLine( out, indentLevel, LocalizedResource.getMessage("UT_0RowsSelec", LocalizedResource.getNumber(numberOfRowsSelected))); } } DisplayNestedResults(out, nestedResults, conn, indentLevel ); nestedResults = null; } /** @param out the place to write to @param nr the vector of results @param conn the Connection against which the ResultSet was retrieved @param indentLevel number of tab stops to indent line @exception SQLException thrown on access error */ static private void DisplayNestedResults(PrintWriter out, Vector nr, Connection conn, int indentLevel ) throws SQLException { if (nr == null) return; String b=LocalizedResource.getMessage("UT_JDBCDisplayUtil_16"); String oldString="0"; for (int i=0; i < nr.size(); i++) { LocalizedResource.OutputWriter().println(); //just too clever to get the extra +s String t = Integer.toString(i); if (t.length() > oldString.length()) { oldString = t; b=b+LocalizedResource.getMessage("UT_JDBCDisplayUtil_17"); } LocalizedResource.OutputWriter().println(b); LocalizedResource.OutputWriter().println(LocalizedResource.getMessage("UT_Resul0", LocalizedResource.getNumber(i))); LocalizedResource.OutputWriter().println(b); indent_DisplayResults(out, (ResultSet) nr.elementAt(i), conn, indentLevel); } } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -