云数据库 GAUSSDB-示例:常用操作:示例3 常用数据类型使用示例

时间:2024-11-02 18:52:46

示例3 常用数据类型使用示例

//bit类型使用示例,注意此处bit类型取值范围[0,1]
Statement st = conn.createStatement();
String sqlstr = "create or replace function fun_1()\n" +
        "returns bit AS $$\n" +
        "select col_bit from t_bit limit 1;\n" +
        "$$\n" +
        "LANGUAGE SQL;";
st.execute(sqlstr);
CallableStatement c = conn.prepareCall("{ ? = call fun_1() }");
//注册输出类型,位串类型
c.registerOutParameter(1, Types.BIT);
c.execute();
//使用Boolean类型获取结果
System.out.println(c.getBoolean(1));

// money类型使用示例
// 表结构中包含money类型列的使用示例。
st.execute("create table t_money(col1 money)");
PreparedStatement pstm = conn.prepareStatement("insert into t_money values(?)");
// 使用PGobject赋值,取值范围[-92233720368547758.08,92233720368547758.07]
PGobject minMoney = new PGobject();
minMoney.setType("money");
minMoney.setValue("-92233720368547758.08");
pstm.setObject(1, minMoney);
pstm.execute();
// 使用PGMoney赋值,取值范围[-9999999.99,9999999.99]
pstm.setObject(1,new PGmoney(9999999.99));
pstm.execute();

// 函数返回值为money的使用示例。
st.execute("create or replace function func_money() " +
                "return money " +
                "as declare " +
                "var1 money; " +
                "begin " +
                " select col1 into var1 from t_money limit 1; " +
                " return var1; " +
                "end;");
CallableStatement cs = conn.prepareCall("{? = call func_money()}");
cs.registerOutParameter(1,Types.DOUBLE);
cs.execute();
cs.getObject(1);
support.huaweicloud.com/centralized-devg-v2-gaussdb/devg_03_0100.html