简体   繁体   中英

Passing BIT in query as a parameter in Prepared Statement

I want to pass a bit as one of the parameters in Prepared Statement. My query should look like this:

query = select * from tbl_security_details('user',O::BIT)

I am framing the query as:

query = select * from tbl_security_details(?,?)
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1,"user")
ps.setString(2,"0::BIT")

However, this throws an error.

Can someone explain how I can pass 0::BIT from the prepare statement without it appending the single quote by itself and getting converted to String?

Write the prepared statement so that the cast is part of the query:

String query = "select * from tbl_security_details(?, ?::bit)";
java.sql.PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, "user");
ps.setString(2, "0");

That is necessary, because you can only pass a constant value to the prepared statement, not an SQL expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM