PreparedStatement,using one parameter for multiple “?”

后端 未结 4 421
名媛妹妹
名媛妹妹 2021-01-24 13:08

I have a insert if not exists query as below.

BEGIN
    IF NOT EXISTS (SELECT * FROM tbl_sampleTable WHERE name = ? or subject = ?)
    BEGIN
        INSERT INTO         


        
4条回答
  •  感情败类
    2021-01-24 14:08

    You need to add some wrapper, without using Spring (NamedParameterJdbcTemplate) you can try other as HTTP-RPC framework

    The org.httprpc.sql.Parameters class provided by the HTTP-RPC framework brings named parameter support to JDBC. The parse() method of this class is used to create a Parameters instance from a JPA-like SQL query; for example:

    SELECT * FROM user WHERE first_name LIKE :pattern or last_name LIKE :pattern
    

    It takes a string or reader containing the query text as an argument:

     Parameters parameters = Parameters.parse(sqlReader);
    

    The getSQL() method of the Parameters class returns the processed query in standard JDBC syntax. This value can be used in a call to Connection#prepareStatement():

    PreparedStatement statement = connection.prepareStatement(parameters.getSQL());
    

    Parameter values are specified via the put() method:

    parameters.put("pattern", pattern);
    

    The values are applied to the statement via the apply() method:

    parameters.apply(statement);

提交回复
热议问题