Search in sources :

Example 61 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project voltdb by VoltDB.

the class TestJDBC method testDecimal.

public void testDecimal() {
    String ddl = "create table test (cash decimal default 23.587);";
    String dml = "insert into test values (123.45678911111);";
    String query = "select * from test;";
    Connection dbconn;
    try {
        Class.forName("org.hsqldb_voltpatches.jdbcDriver");
        dbconn = DriverManager.getConnection("jdbc:hsqldb:mem:x1", "sa", "");
        dbconn.setAutoCommit(true);
        dbconn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        Statement stmt = dbconn.createStatement();
        stmt.execute(ddl);
        SQLWarning warn = stmt.getWarnings();
        if (warn != null)
            System.out.println("warn: " + warn.getMessage());
        assertTrue(warn == null);
        long ucount = stmt.executeUpdate(dml);
        assertTrue(ucount == 1);
        ResultSet rs = stmt.executeQuery(query);
        assertTrue(rs != null);
        ResultSetMetaData rsmd = rs.getMetaData();
        assertTrue(rsmd != null);
        assertTrue(rsmd.getColumnCount() == 1);
        /*System.out.printf("Typename %s, Type %d, Precision %s, Scale %d, Classname %s\n",
                    rsmd.getColumnTypeName(1),
                    rsmd.getColumnType(1),
                    rsmd.getPrecision(1),
                    rsmd.getScale(1),
                    rsmd.getColumnClassName(1));*/
        boolean success = rs.next();
        assertTrue(success);
        BigDecimal x = rs.getBigDecimal(1);
        assertNotNull(x);
        //System.out.printf("Value: %.10f\n", x.doubleValue());
        BigDecimal expected = new BigDecimal(123.4567);
        assertTrue(x.subtract(expected).abs().doubleValue() < .01);
        try {
            stmt.execute("SHUTDOWN;");
        } catch (Exception e) {
        }
        ;
        dbconn.close();
        System.gc();
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : SQLWarning(java.sql.SQLWarning) ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) BigDecimal(java.math.BigDecimal)

Example 62 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project voltdb by VoltDB.

the class TestJDBC method testTinyInt.

public void testTinyInt() {
    String ddl = "create table test (cash tinyint default 0);";
    String dml = "insert into test values (123);";
    String query = "select * from test;";
    Connection dbconn;
    try {
        Class.forName("org.hsqldb_voltpatches.jdbcDriver");
        dbconn = DriverManager.getConnection("jdbc:hsqldb:mem:x1", "sa", "");
        dbconn.setAutoCommit(true);
        dbconn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        Statement stmt = dbconn.createStatement();
        stmt.execute(ddl);
        SQLWarning warn = stmt.getWarnings();
        if (warn != null)
            System.out.println("warn: " + warn.getMessage());
        assertTrue(warn == null);
        long ucount = stmt.executeUpdate(dml);
        assertTrue(ucount == 1);
        ResultSet rs = stmt.executeQuery(query);
        assertTrue(rs != null);
        ResultSetMetaData rsmd = rs.getMetaData();
        assertTrue(rsmd != null);
        assertTrue(rsmd.getColumnCount() == 1);
        /*System.out.printf("Typename %s, Type %d, Precision %s, Scale %d, Classname %s\n",
                    rsmd.getColumnTypeName(1),
                    rsmd.getColumnType(1),
                    rsmd.getPrecision(1),
                    rsmd.getScale(1),
                    rsmd.getColumnClassName(1));*/
        boolean success = rs.next();
        assertTrue(success);
        int x = rs.getInt(1);
        assertTrue(x == 123);
        try {
            stmt.execute("SHUTDOWN;");
        } catch (Exception e) {
        }
        ;
        dbconn.close();
        System.gc();
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
}
Also used : SQLWarning(java.sql.SQLWarning) ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 63 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project voltdb by VoltDB.

the class DatabaseManager method formatResultSet.

void formatResultSet(ResultSet r) {
    if (r == null) {
        String[] g = new String[1];
        g[0] = "Result";
        gResult.setHead(g);
        g[0] = "(empty)";
        gResult.addRow(g);
        return;
    }
    try {
        ResultSetMetaData m = r.getMetaData();
        int col = m.getColumnCount();
        String[] h = new String[col];
        for (int i = 1; i <= col; i++) {
            h[i - 1] = m.getColumnLabel(i);
        }
        gResult.setHead(h);
        while (r.next()) {
            for (int i = 1; i <= col; i++) {
                try {
                    h[i - 1] = r.getString(i);
                    if (r.wasNull()) {
                        h[i - 1] = "(null)";
                    }
                } catch (SQLException e) {
                }
            }
            gResult.addRow(h);
        }
        r.close();
    } catch (SQLException e) {
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException)

Example 64 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project MSEC by Tencent.

the class DBUtil method findMoreRefResult.

//查询数据库,返回所有满足条件的记录, 使用java反射机制,将记录映射到java类T
public <T> ArrayList<T> findMoreRefResult(String sql, List<Object> params, Class<T> cls) throws Exception {
    ArrayList<T> list = new ArrayList<T>();
    int index = 1;
    pstmt = connection.prepareStatement(sql);
    if (params != null && !params.isEmpty()) {
        for (int i = 0; i < params.size(); i++) {
            pstmt.setObject(index++, params.get(i));
        }
    }
    resultSet = pstmt.executeQuery();
    ResultSetMetaData metaData = resultSet.getMetaData();
    int cols_len = metaData.getColumnCount();
    while (resultSet.next()) {
        T resultObject = cls.newInstance();
        for (int i = 0; i < cols_len; i++) {
            String cols_name = metaData.getColumnName(i + 1);
            Object cols_value = resultSet.getObject(cols_name);
            if (cols_value == null) {
                cols_value = "";
            }
            Field field = cls.getDeclaredField(cols_name);
            //��javabean�ķ���Ȩ��
            field.setAccessible(true);
            field.set(resultObject, cols_value);
        }
        list.add(resultObject);
    }
    resultSet.close();
    pstmt.close();
    return list;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Field(java.lang.reflect.Field)

Example 65 with ResultSetMetaData

use of java.sql.ResultSetMetaData in project MSEC by Tencent.

the class DBUtil method findSimpleResult.

//查询数据库,只返回一条记录,该记录的字段保存在Map里返回,字段名作为key,字段值作为value
public Map<String, Object> findSimpleResult(String sql, List<Object> params) throws SQLException {
    Map<String, Object> map = new HashMap<String, Object>();
    int index = 1;
    pstmt = connection.prepareStatement(sql);
    if (params != null && !params.isEmpty()) {
        for (int i = 0; i < params.size(); i++) {
            pstmt.setObject(index++, params.get(i));
        }
    }
    resultSet = pstmt.executeQuery();
    ResultSetMetaData metaData = resultSet.getMetaData();
    int col_len = metaData.getColumnCount();
    if (resultSet.next()) {
        for (int i = 0; i < col_len; i++) {
            String cols_name = metaData.getColumnName(i + 1);
            Object cols_value = resultSet.getObject(cols_name);
            if (cols_value == null) {
                cols_value = "";
            }
            map.put(cols_name, cols_value);
        }
    }
    resultSet.close();
    pstmt.close();
    return map;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData)

Aggregations

ResultSetMetaData (java.sql.ResultSetMetaData)449 ResultSet (java.sql.ResultSet)279 SQLException (java.sql.SQLException)125 Statement (java.sql.Statement)123 Test (org.junit.Test)109 PreparedStatement (java.sql.PreparedStatement)107 ArrayList (java.util.ArrayList)78 Connection (java.sql.Connection)68 HashMap (java.util.HashMap)44 DatabaseMetaData (java.sql.DatabaseMetaData)41 Map (java.util.Map)31 LinkedHashMap (java.util.LinkedHashMap)22 List (java.util.List)18 IOException (java.io.IOException)15 BigDecimal (java.math.BigDecimal)12 HashSet (java.util.HashSet)12 ParameterMetaData (java.sql.ParameterMetaData)10 Properties (java.util.Properties)10 KnownFailure (dalvik.annotation.KnownFailure)9 Field (java.lang.reflect.Field)9