DEV Community

leo
leo

Posted on

JDBC development application program based on openGauss (common data type usage example)

JDBC development application program based on openGauss (common data type usage example)

//Example of using the bit type, note that the value range of the bit type here is [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( ) }");
//Register output type, bit string type
c.registerOutParameter(1, Types.BIT);
c.execute();
//Use Boolean type to get the result
System.out.println(c.getBoolean(1) );

// Money type usage example
// The table structure contains usage examples of money type columns.
st.execute("create table t_money(col1 money)");
PreparedStatement pstm = conn.prepareStatement("
// Use PGobject to assign value, value range [-92233720368547758.08, 92233720368547758.07]
PGobject minMoney = new PGobject();
minMoney.setType("money");
minMoney.setValue("-92233720368547758.08set")
ney1, pstm ;
pstm.execute();
// Use PGMoney to assign value, value range [-9999999.99,9999999.99]
pstm.setObject(1,new PGmoney(9999999.99));
pstm.execute();

// The return value of the function is money example.
st.execute("create or replace function func_money() " +
"return money " +
"as declare " +
"var1 money; " +
"begin " +
" select col1 into var1 from t_money limit 1; "

"end;");
CallableStatement cs = conn.prepareCall("{? = call func_money()}");
cs.registerOutParameter(1,Types.DOUBLE);
cs.execute();
cs.getObject(1);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)