Sql query with bind variables execution in Jdbc

前端 未结 5 1760
庸人自扰
庸人自扰 2021-01-15 17:28

I have a sql query like this.

 select \"DEPT\".\"DEPTNO\" as \"DEPTNO1\",
\"DEPT\".\"DNAME\" as \"DNAME1\",
\"DEPT\".\"LOC\" as \"LOC1\",
\"EMP\".\"COMM\" as         


        
5条回答
  •  鱼传尺愫
    2021-01-15 18:14

    replace :deptno in your query with a ?.

    and instead of instantiating statement use the following:

    PreparedStatement stmt=con.prepareStatement(query);
    
    stmt.setInt(1,deptno); //1 is for the first question mark
    

    where deptno holds the value for which you want to execute the query.

    Through PrepredStatement interface we can use parametrized query which is compiled only once and has performance advantage in comparison to the Statement interface.

提交回复
热议问题