Sql query with bind variables execution in Jdbc

前端 未结 5 1822
庸人自扰
庸人自扰 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 17:55

    The variable DeptNo must be bound to a value before you execute the statement like below.

    DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
    Statement statment = dbConnection.createStatement();
    //Bind deptno to a value
    statment.setParameter("DeptNo",5);
    result = statment.execute(query);    
    

    You must set values for all the variables in your prepared statement othwerise you cannot execute the statement. If you receive the query to execute itself as an input then you should also get the parameters and its values also as input. Something like below

    public  executeQuery(String queryStr, Map params) {
         //Code to create connecitno and statment from queryStr.
         //Bind deptno to a value
         for(int i=0;i

提交回复
热议问题