【问题标题】:Exception in prepared statement准备好的语句中的异常
【发布时间】:2014-11-03 06:52:01
【问题描述】:

以下是代码。

     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     conn = DriverManager.getConnection("jdbc:odbc:cse");
     //Statement stmt;
     ResultSet rset;
     //stmt = conn.createStatement();

     String sql = " Select * from registration where id=?";
     PreparedStatement pst = conn.prepareStatement(sql);
     pst.setString(1, "101");
     rset = pst.executeQuery(sql);

     while (rset.next()) {
         arr.add(rset.getInt("id"));
         arr.add(rset.getString("first"));
         arr.add(rset.getString("last"));
         arr.add(rset.getInt("age"));
    }

  System.out.println(arr);
  pst.close();
  conn.close();

对于上述我得到“错误:java.sql.SQLException:驱动程序不支持此功能”。可能是什么问题?

【问题讨论】:

  • Exception 指向哪一行?
  • 你试过pst.executeQuery()吗?无需传递 SQL 字符串。
  • 并非所有数据库都支持问号,这背后是什么数据库?

标签: java sql jdbc prepared-statement


【解决方案1】:

您误用了PreparedStatement 接口。使用PreparedStatements 时,您应该准备带有查询的语句,绑定所有必要的参数,然后不使用任何 SQL 执行它 - 这将导致语句执行之前准备好的 SQL 语句:

 String sql = "Select * from registration where id=?";
 PreparedStatement pst = conn.prepareStatement(sql);
 pst.setString(1, "101");
 rset = pst.executeQuery(); // Note - No args in the executeQuery call

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多