java 打印ResultSet中的数据沿着列名

a64a0gku  于 2023-01-15  发布在  Java
关注(0)|答案(9)|浏览(134)

我正在通过Java从SQL数据库中检索列名。我知道我也可以从ResultSet中检索列名。因此我有以下SQL查询

select column_name from information_schema.columns where table_name='suppliers'

问题是我不知道如何从ResultSet中获取列名,我的代码是

public void getAllColumnNames() throws Exception{

String sql = "SELECT column_name from information_schema.columns where table_name='suppliers'";

PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rs = ps.executeQuery(sql);
 // extract values from rs

}
siv3szwd

siv3szwd1#

ResultSet resultSet = statement.executeQuery("SELECT * from foo");
ResultSetMetaData rsmd = resultSet.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (resultSet.next()) {
    for (int i = 1; i <= columnsNumber; i++) {
        if (i > 1) System.out.print(",  ");
        String columnValue = resultSet.getString(i);
        System.out.print(columnValue + " " + rsmd.getColumnName(i));
    }
    System.out.println("");
}

参考:Printing the result of ResultSet

zz2j4svz

zz2j4svz2#

1)使用Statement代替PreparedStatement
2)在ResultSet中执行查询后,在rs.getString()的帮助下将值提取为:

Statement st=cn.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next())
{
    rs.getString(1); //or rs.getString("column name");
}
fd3cxomn

fd3cxomn3#

进一步用作

rs.getString(1);
rs.getInt(2);

1、2为表的列号,根据列的数据类型设置int或string

vpfxa7rd

vpfxa7rd4#

对于那些想要更好版本的结果集打印为util类的人,这对打印结果集真的很有帮助,并且可以从一个util做很多事情...感谢Hami Torun!
在这个类中,printResultSet以通用的方式使用ResultSetMetaData,请看一看。

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

public final class DBTablePrinter {

    /**
     * Column type category for 

    CHAR
, 

    VARCHAR
* and similar text columns.
     */
    public static final int CATEGORY_STRING = 1;
    /**
     * Column type category for 

    TINYINT
, 

    SMALLINT
,
     * 

    INT
and 

    BIGINT
columns.
     */
    public static final int CATEGORY_INTEGER = 2;
    /**
     * Column type category for 

    REAL
, 

    DOUBLE
,
     * and 

    DECIMAL
columns.
     */
    public static final int CATEGORY_DOUBLE = 3;
    /**
     * Column type category for date and time related columns like
     * 

    DATE
, 

    TIME
, 

    TIMESTAMP
etc.
     */
    public static final int CATEGORY_DATETIME = 4;
    /**
     * Column type category for 

    BOOLEAN
columns.
     */
    public static final int CATEGORY_BOOLEAN = 5;
    /**
     * Column type category for types for which the type name
     * will be printed instead of the content, like 

    BLOB
,
     * 

    BINARY
, 

    ARRAY
etc.
     */
    public static final int CATEGORY_OTHER = 0;
    /**
     * Default maximum number of rows to query and print.
     */
    private static final int DEFAULT_MAX_ROWS = 10;
    /**
     * Default maximum width for text columns
     * (like a 

    VARCHAR
) column.
     */
    private static final int DEFAULT_MAX_TEXT_COL_WIDTH = 150;

    /**
     * Overloaded method that prints rows from table 

    tableName
* to standard out using the given database connection
     * 

    conn
. Total number of rows will be limited to
     * {@link #DEFAULT_MAX_ROWS} and
     * {@link #DEFAULT_MAX_TEXT_COL_WIDTH} will be used to limit
     * the width of text columns (like a 

    VARCHAR
column).
     *
     * @param conn      Database connection object (java.sql.Connection)
     * @param tableName Name of the database table
     */
    public static void printTable(Connection conn, String tableName) {
        printTable(conn, tableName, DEFAULT_MAX_ROWS, DEFAULT_MAX_TEXT_COL_WIDTH);
    }

    /**
     * Overloaded method that prints rows from table 

    tableName
* to standard out using the given database connection
     * 

    conn
. Total number of rows will be limited to
     * 

    maxRows
and
     * {@link #DEFAULT_MAX_TEXT_COL_WIDTH} will be used to limit
     * the width of text columns (like a 

    VARCHAR
column).
     *
     * @param conn      Database connection object (java.sql.Connection)
     * @param tableName Name of the database table
     * @param maxRows   Number of max. rows to query and print
     */
    public static void printTable(Connection conn, String tableName, int maxRows) {
        printTable(conn, tableName, maxRows, DEFAULT_MAX_TEXT_COL_WIDTH);
    }

    /**
     * Overloaded method that prints rows from table 

    tableName
* to standard out using the given database connection
     * 

    conn
. Total number of rows will be limited to
     * 

    maxRows
and
     * 

    maxStringColWidth
will be used to limit
     * the width of text columns (like a 

    VARCHAR
column).
     *
     * @param conn              Database connection object (java.sql.Connection)
     * @param tableName         Name of the database table
     * @param maxRows           Number of max. rows to query and print
     * @param maxStringColWidth Max. width of text columns
     */
    public static void printTable(Connection conn, String tableName, int maxRows, int maxStringColWidth) {
        if (conn == null) {
            System.err.println("DBTablePrinter Error: No connection to database (Connection is null)!");
            return;
        }
        if (tableName == null) {
            System.err.println("DBTablePrinter Error: No table name (tableName is null)!");
            return;
        }
        if (tableName.length() == 0) {
            System.err.println("DBTablePrinter Error: Empty table name!");
            return;
        }
        if (maxRows 
     * ResultSet to standard out using {@link #DEFAULT_MAX_TEXT_COL_WIDTH}
     * to limit the width of text columns.
     *
     * @param rs The 

    ResultSet
to print
     */
    public static void printResultSet(ResultSet rs) {
        printResultSet(rs, DEFAULT_MAX_TEXT_COL_WIDTH);
    }

    /**
     * Overloaded method to print rows of a 
     * ResultSet to standard out using 

    maxStringColWidth
* to limit the width of text columns.
     *
     * @param rs                The 

    ResultSet
to print
     * @param maxStringColWidth Max. width of text columns
     */
    public static void printResultSet(ResultSet rs, int maxStringColWidth) {
        try {
            if (rs == null) {
                System.err.println("DBTablePrinter Error: Result set is null!");
                return;
            }
            if (rs.isClosed()) {
                System.err.println("DBTablePrinter Error: Result Set is closed!");
                return;
            }
            if (maxStringColWidth  columns = new ArrayList(columnCount);

            // List of table names. Can be more than one if it is a joined
            // table query
            List tableNames = new ArrayList(columnCount);

            // Get the columns and their meta data.
            // NOTE: columnIndex for rsmd.getXXX methods STARTS AT 1 NOT 0
            for (int i = 1; i  maxStringColWidth) {
                                value = value.substring(0, maxStringColWidth - 3) + "...";
                            }
                            break;
                    }

                    // Adjust the column width
                    c.setWidth(value.length() > c.getWidth() ? value.length() : c.getWidth());
                    c.addValue(value);
                } // END of for loop columnCount
                rowCount++;

            } // END of while (rs.next)

            /*
            At this point we have gone through meta data, get the
            columns and created all Column objects, iterated over the
            ResultSet rows, populated the column values and adjusted
            the column widths.
            We cannot start printing just yet because we have to prepare
            a row separator String.
             */

            // For the fun of it, I will use StringBuilder
            StringBuilder strToPrint = new StringBuilder();
            StringBuilder rowSeparator = new StringBuilder();

            /*
            Prepare column labels to print as well as the row separator.
            It should look something like this:
            +--------+------------+------------+-----------+  (row separator)
            | EMP_NO | BIRTH_DATE | FIRST_NAME | LAST_NAME |  (labels row)
            +--------+------------+------------+-----------+  (row separator)
             */

            // Iterate over columns
            for (Column c : columns) {
                int width = c.getWidth();

                // Center the column label
                String toPrint;
                String name = c.getLabel();
                int diff = width - name.length();

                if ((diff % 2) == 1) {
                    // diff is not divisible by 2, add 1 to width (and diff)
                    // so that we can have equal padding to the left and right
                    // of the column label.
                    width++;
                    diff++;
                    c.setWidth(width);
                }

                int paddingSize = diff / 2; // InteliJ says casting to int is redundant.

                // Cool String repeater code thanks to user102008 at stackoverflow.com

                String padding = new String(new char[paddingSize]).replace("\0", " ");

                toPrint = "| " + padding + name + padding + " ";
                // END centering the column label

                strToPrint.append(toPrint);

                rowSeparator.append("+");
                rowSeparator.append(new String(new char[width + 2]).replace("\0", "-"));
            }

            String lineSeparator = System.getProperty("line.separator");

            // Is this really necessary ??
            lineSeparator = lineSeparator == null ? "\n" : lineSeparator;

            rowSeparator.append("+").append(lineSeparator);

            strToPrint.append("|").append(lineSeparator);
            strToPrint.insert(0, rowSeparator);
            strToPrint.append(rowSeparator);

            StringJoiner sj = new StringJoiner(", ");
            for (String name : tableNames) {
                sj.add(name);
            }

            String info = "Printing " + rowCount;
            info += rowCount > 1 ? " rows from " : " row from ";
            info += tableNames.size() > 1 ? "tables " : "table ";
            info += sj.toString();

            System.out.println(info);

            // Print out the formatted column labels
            System.out.print(strToPrint.toString());

            String format;

            // Print out the rows
            for (int i = 0; i 
     * Integers should not be truncated so column widths should
     * be adjusted without a column width limit. Text columns should be
     * left justified and can be truncated to a max. column width etc...

* 

* See also: 
     * java.sql.Types
     *
     * @param type Generic SQL type
     * @return The category this type belongs to
     */
    private static int whichCategory(int type) {
        switch (type) {
            case Types.BIGINT:
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.INTEGER:
                return CATEGORY_INTEGER;

            case Types.REAL:
            case Types.DOUBLE:
            case Types.DECIMAL:
                return CATEGORY_DOUBLE;

            case Types.DATE:
            case Types.TIME:
            case Types.TIME_WITH_TIMEZONE:
            case Types.TIMESTAMP:
            case Types.TIMESTAMP_WITH_TIMEZONE:
                return CATEGORY_DATETIME;

            case Types.BOOLEAN:
                return CATEGORY_BOOLEAN;

            case Types.VARCHAR:
            case Types.NVARCHAR:
            case Types.LONGVARCHAR:
            case Types.LONGNVARCHAR:
            case Types.CHAR:
            case Types.NCHAR:
                return CATEGORY_STRING;

            default:
                return CATEGORY_OTHER;
        }
    }

    /**
     * Represents a database table column.
     */
    private static class Column {

        /**
         * Column label.
         */
        private String label;

        /**
         * Generic SQL type of the column as defined in
         * 
         * java.sql.Types
         * .
         */
        private int type;

        /**
         * Generic SQL type name of the column as defined in
         * 
         * java.sql.Types
         * .
         */
        private String typeName;

        /**
         * Width of the column that will be adjusted according to column label
         * and values to be printed.
         */
        private int width = 0;

        /**
         * Column values from each row of a 

    ResultSet
.
         */
        private List values = new ArrayList();

        /**
         * Flag for text justification using 

    String.format
.
         * Empty string (

    ""
) to justify right,
         * dash (

    -
) to justify left.
         *
         * @see #justifyLeft()
         */
        private String justifyFlag = "";

        /**
         * Column type category. The columns will be categorised according
         * to their column types and specific needs to print them correctly.
         */
        private int typeCategory = 0;

        /**
         * Constructs a new 

    Column
with a column label,
         * generic SQL type and type name (as defined in
         * 
         * java.sql.Types
         * )
         *
         * @param label    Column label or name
         * @param type     Generic SQL type
         * @param typeName Generic SQL type name
         */
        public Column(String label, int type, String typeName) {
            this.label = label;
            this.type = type;
            this.typeName = typeName;
        }

        /**
         * Returns the column label
         *
         * @return Column label
         */
        public String getLabel() {
            return label;
        }

        /**
         * Returns the generic SQL type of the column
         *
         * @return Generic SQL type
         */
        public int getType() {
            return type;
        }

        /**
         * Returns the generic SQL type name of the column
         *
         * @return Generic SQL type name
         */
        public String getTypeName() {
            return typeName;
        }

        /**
         * Returns the width of the column
         *
         * @return Column width
         */
        public int getWidth() {
            return width;
        }

        /**
         * Sets the width of the column to 

    width
*
         * @param width Width of the column
         */
        public void setWidth(int width) {
            this.width = width;
        }

        /**
         * Adds a 

    String
representation (

    value
)
         * of a value to this column object's {@link #values} list.
         * These values will come from each row of a
         * 
         * ResultSet
         *  of a database query.
         *
         * @param value The column value to add to {@link #values}
         */
        public void addValue(String value) {
            values.add(value);
        }

        /**
         * Returns the column value at row index 

    i
.
         * Note that the index starts at 0 so that 

    getValue(0)
* will get the value for this column from the first row
         * of a 
         * ResultSet.
         *
         * @param i The index of the column value to get
         * @return The String representation of the value
         */
        public String getValue(int i) {
            return values.get(i);
        }

        /**
         * Returns the value of the {@link #justifyFlag}. The column
         * values will be printed using 

    String.format
and
         * this flag will be used to right or left justify the text.
         *
         * @return The {@link #justifyFlag} of this column
         * @see #justifyLeft()
         */
        public String getJustifyFlag() {
            return justifyFlag;
        }

        /**
         * Sets {@link #justifyFlag} to 

    "-"
so that
         * the column value will be left justified when printed with
         * 

    String.format
. Typically numbers will be right
         * justified and text will be left justified.
         */
        public void justifyLeft() {
            this.justifyFlag = "-";
        }

        /**
         * Returns the generic SQL type category of the column
         *
         * @return The {@link #typeCategory} of the column
         */
        public int getTypeCategory() {
            return typeCategory;
        }

        /**
         * Sets the {@link #typeCategory} of the column
         *
         * @param typeCategory The type category
         */
        public void setTypeCategory(int typeCategory) {
            this.typeCategory = typeCategory;
        }
    }
}

这是scala的版本......它会以一种通用的方式打印列名和数据......

def printQuery(res: ResultSet): Unit = {
    val rsmd = res.getMetaData
    val columnCount = rsmd.getColumnCount
    var rowCnt = 0
    val s = StringBuilder.newBuilder
    while (res.next()) {

      s.clear()
      if (rowCnt == 0) {
        s.append("| ")
        for (i <- 1 to columnCount) {
          val name = rsmd.getColumnName(i)
          s.append(name)
          s.append("| ")
        }
        s.append("\n")
      }
      rowCnt += 1
      s.append("| ")
      for (i <- 1 to columnCount) {
        if (i > 1)
          s.append(" | ")
        s.append(res.getString(i))
      }
      s.append(" |")
      System.out.println(s)
    }
    System.out.println(s"TOTAL: $rowCnt rows")
  }
0h4hbjxa

0h4hbjxa5#

看一下documentation。您犯了以下错误。首先,ps.executeQuery()没有任何参数。相反,您将SQL查询传递给它。
其次,关于预准备语句,如果要传递任何参数,就必须使用?符号。

setXXX(index, value)

这里 xxx 代表数据类型。

31moq8wy

31moq8wy6#

你可以使用TableSaw library,这是一个很棒的Java数据科学/数据框架库,代码很简单:

System.out.println(Table.read().db(resultSet).print());
xn1cxnb4

xn1cxnb47#

对于您尝试执行的操作,您可以使用Statement代替PreparedStatement。您的代码可以修改为-

String sql = "SELECT column_name from information_schema.columns where table_name='suppliers';";

Statement s  = connection.createStatement();
ResultSet rs = s.executeQuery(sql);

希望这个有用。

91zkwejq

91zkwejq8#

具有rowMapper实现的解决方案
存储库类:

jdbcTemplate.query(sqlRequest, new ResponseQuikRowMapper())

行Map器类:

public class ResponseQuikRowMapper implements RowMapper<Map<String, String>> {

@Override
@SneakyThrows
public Map<String, String> mapRow(ResultSet rs, int rowNum) {
    return IntStream.range(1, rs.getMetaData().getColumnCount())
            .mapToObj(columnNumber -> getColumnName(rs, columnNumber))
            .collect(Collectors.toMap(column -> column, value -> getColumnValue(rs, value)));
}

@SneakyThrows
private String getColumnName(ResultSet rs, Integer columnNumber) {
    return rs.getMetaData().getColumnName(columnNumber);
}

@SneakyThrows
private String getColumnValue(ResultSet rs, String column) {
    return Optional.ofNullable(rs.getString(column)).orElse("");
}

HashMap它是一个简单的矩阵,其中key是列名,value是值:)

r6l8ljro

r6l8ljro9#

如果您正在使用Spring Data ,可以尝试以下操作:

var mapped = new ColumnMapRowMapper().mapRow(resultSet, rowNum);
log.debug("Result={}", mapped);

相关问题