日期:2014-05-20  浏览次数:20821 次

哪位高手帮指点一下 关于PreparedStatement删除数据的
报错信息:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid call Statement method: {0}
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at ch03.NewsTitleDB3.main(NewsTitleDB3.java:18)


JAVA源代码:package ch03;
import java.util.*;
import java.sql.*;
public class NewsTitleDB3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
  Connection con=null;
  PreparedStatement pstmt=null;
  try{
  con=ConnectionManager.getConnection();
  String sql="delete from FirstLevelTitle where id=?";
  pstmt=con.prepareStatement(sql);
  pstmt.setString(2,"军事");
  int row=pstmt.executeUpdate(sql);
  System.out.println("成功删除了"+row+"行数据!");
  }catch(SQLException e){
  e.printStackTrace();
  }finally{
  ConnectionManager.closeStatement(pstmt);
  ConnectionManager.closeConnection(con);
  }
}

}


------解决方案--------------------
pstmt.setString(2,"军事");改成 pstmt.setString(1,"军事");试试
Java code

package ch03;
import java.util.*;
import java.sql.*;
public class NewsTitleDB3 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
  Connection con=null;
  PreparedStatement pstmt=null;
  try{
  con=ConnectionManager.getConnection();
  String sql="delete from FirstLevelTitle where id=?";
  pstmt=con.prepareStatement(sql);
  pstmt.setString(1,"军事");
  int row=pstmt.executeUpdate(sql);
  System.out.println("成功删除了"+row+"行数据!");
  }catch(SQLException e){
  e.printStackTrace();
  }finally{
  ConnectionManager.closeStatement(pstmt);
  ConnectionManager.closeConnection(con);
  }
}

}