3

I need to get scalar value with DBUtils, does someone know how to get it? Currently I do:

    String sql = "SELECT count(*) FROM t1 WHERE cod = ?";
    final QueryRunner run = new QueryRunner(new AppDataSource());
    ScalarHandler scalar = new ScalarHandler();
    long count = -1l;
    try{
        Object[] params =
        {
                code
        };
        count = ((Long) run.query(sql, scalar, params)).longValue();
    }
    catch(SQLException e){
        e.printStackTrace();
    }
    System.out.println(count);

but result is still -1. When I test it with db util I get right number.

1ac0
  • 2,875
  • 3
  • 33
  • 47

2 Answers2

2

I hope the code below can help you, ScalarHandler just picks up one column value.

public Object queryColumnVaue(String sql,String columnName) throws SQLException { 
     ScalarHandler<Object> kh = new ScalarHandler<Object>(columnName);
     return query(sql,kh);
 }
Dan H
  • 17,654
  • 1
  • 27
  • 22
zg_spring
  • 349
  • 1
  • 10
1

Hmm, nothing is better than rename standard scheme name "public" (PostgreSQL) and than not using it in SQL, this is the correct way:

String sql = "SELECT count(*) FROM \"MySchema\".t1 WHERE cod = ?";

Hope to someone will help.

1ac0
  • 2,875
  • 3
  • 33
  • 47