JDBC连接Oracle数据库,执行如下操作时报错“ ORA-01027: 在数据定义操作中不允许对变量赋”

      statement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

      String view= "create view v_test as " +
           "select kh from dm_mpg_test " +
           "where nyr=?"+
           " with read only ";
      PreparedStatement pst=con.prepareStatement(view);
      pst.setString(1, "20081001");
      pst.executeUpdate();

这个问题其实是动态SQL的问题,在数据定义语言DDL中不能绑定变量,该问题和PL/SQL中DBMS_SQL执行DDL语句时不能bind变量一致。

然而下面操作是可以的,
1.在sql预编译前加入变量
      String nyr= "20081001";
      String view= "create view v_test as " +
           "select kh from dm_mpg_test " +
           "where nyr="+nyr+
           " with read only ";
      PreparedStatement pst=con.prepareStatement(view);
      pst.executeUpdate();
2.非DDL,而是DML
      String dq= "select * from dm_mpg_test where nyr=?";
      PreparedStatement pst=con.prepareStatement(view);
      pst.setString(1, "20081001");
      ResultSet r=pst.executeQuery();