使用结果集检索整行

2022-09-02 10:33:19

是否可以在不为每列调用 getInt(..) getString(..) 的情况下检索整行?

我有多个线程,每个胎面都需要将结果写入一些线程安全集合。
我希望能够将行直接写入此集合,然后分析此集合的成员并根据列类型检索值。


答案 1

您可以构建一个像这样的类,它将sql数据类型与java数据类型进行映射:

class Row
{
    public Map <Object,Class> row;
    public static Map <String, Class> TYPE;

    static
    {
        TYPE = new HashMap<String, Class>();

        TYPE.put("INTEGER", Integer.class);
        TYPE.put("TINYINT", Byte.class);
        TYPE.put("SMALLINT", Short.class);
        TYPE.put("BIGINT", Long.class);
        TYPE.put("REAL", Float.class);
        TYPE.put("FLOAT", Double.class);
        TYPE.put("DOUBLE", Double.class);
        TYPE.put("DECIMAL", BigDecimal.class);
        TYPE.put("NUMERIC", BigDecimal.class);
        TYPE.put("BOOLEAN", Boolean.class);
        TYPE.put("CHAR", String.class);
        TYPE.put("VARCHAR", String.class);
        TYPE.put("LONGVARCHAR", String.class);
        TYPE.put("DATE", Date.class);
        TYPE.put("TIME", Time.class);
        TYPE.put("TIMESTAMP", Timestamp.class);
        // ...
    }
    
    public Row ()
    {
        row = new HashMap<Object,Class>();
    }
    
    public <T> void add (T data)
    {
        row.put(data, data.getClass());
    }

    public void add (Object data, String sqlType)
    {
        add((Row.TYPE.get(sqlType)) data);
    }

    public static void formTable (ResultSet rs, ArrayList<Row> table)
    {
        if (rs == null) return;
    
        ResultSetMetaData rsmd = rs.getMetaData();
    
        int NumOfCol = rsmd.getColumnCount();
    
        while (rs.next())
        {
            row = new Row ();
        
            for(int i = 1; i <= NumOfCol; i++)
            {
                row.add(rs.getObject(i), rsmd.getColumnTypeName(i));
            }

            table.add(row);
        }
    }
}

你可以像这样使用它:

List<Row> table = new ArrayList<Row>();

Row row = null;

ResultSet rs = st.executeQuery("SELECT * FROM table_name");

Row.formTable(rs, table);

然后,您可以检索字段并将其转换为各自的数据类型:

for (Row row : table)
{

    for (Object data : row.row.getKeySet())
    {
        System.out.print(" > " + ((row.row.get(data) data));
    }
    
    System.out.println();

}

答案 2

表示为列表的行:

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Adam Dziedzic
 * 
 */
public class Row {
    public List<Entry<Object, Class>> row;
    public static Map<String, Class> TYPE;

    static {
        TYPE = new HashMap<String, Class>();

        TYPE.put("INTEGER", Integer.class);
        TYPE.put("TINYINT", Byte.class);
        TYPE.put("SMALLINT", Short.class);
        TYPE.put("BIGINT", Long.class);
        TYPE.put("REAL", Float.class);
        TYPE.put("FLOAT", Double.class);
        TYPE.put("DOUBLE", Double.class);
        TYPE.put("DECIMAL", BigDecimal.class);
        TYPE.put("NUMERIC", BigDecimal.class);
        TYPE.put("BOOLEAN", Boolean.class);
        TYPE.put("CHAR", String.class);
        TYPE.put("VARCHAR", String.class);
        TYPE.put("LONGVARCHAR", String.class);
        TYPE.put("DATE", Date.class);
        TYPE.put("TIME", Time.class);
        TYPE.put("TIMESTAMP", Timestamp.class);
        TYPE.put("SERIAL",Integer.class);
        // ...
    }

    public Row() {
        row = new ArrayList<Entry<Object, Class>>();
    }

    public <T> void add(T data) {
        row.add(new AbstractMap.SimpleImmutableEntry<Object,Class>(data, data.getClass()));
    }

    public void add(Object data, String sqlType) {
        Class castType = Row.TYPE.get(sqlType.toUpperCase());
        try {
            this.add(castType.cast(data));
        } catch (NullPointerException e) {
            e.printStackTrace();
            Logger lgr = Logger.getLogger(Row.class.getName());
            lgr.log(Level.SEVERE, e.getMessage()+" Add the type "+sqlType+" to the TYPE hash map in the Row class.", e);
            throw e;
        }
    }

    public static void formTable(ResultSet rs, List<Row> table)
            throws SQLException {
        if (rs == null)
            return;

        ResultSetMetaData rsmd;
        try {
            rsmd = rs.getMetaData();

            int NumOfCol = rsmd.getColumnCount();

            while (rs.next()) {
                Row current_row = new Row();

                for (int i = 1; i <= NumOfCol; i++) {
                    current_row.add(rs.getObject(i), rsmd.getColumnTypeName(i));
                }

                table.add(current_row);
            }
        } catch (SQLException e) {
            throw e;
        }
    }
}

用法:

List<Row> table = new ArrayList<Row>();

ResultSet rs = st.executeQuery("SELECT * FROM table_name");

Row.formTable(rs, table);

for (Row row : table)
{
    for (Entry<Object, Class> col: row.row)
    {
        System.out.print(" > " + ((col.getValue()).cast(col.getKey())));
    }
    System.out.println();
}

推荐