使用的是JBUILDER 9和SQLSERVER 2000,O/S为WIN2000 PRO现在有一个表publishers(pub_id varchar(4), pub_name(40), city varchar(20), state varchar(2), country varchar(30), URL varchar(80));
其中pub_id为主键PreparedStatement updatePublisher = connection.preparedStatement("UPDATE publishers SET
pub_name = ?, city = ?, state = ?, country = ?, URL = ?
WHERE pub_id = ?");updatePublisher.setString(1, aPublisher.getPuplisherName());
updatePublisher.setString(2, aPublisher.getCity());
updatePublisher.setString(3, aPublisher.getState());
updatePublisher.setString(4, aPublisher.getCountry());
updatePublisher.setString(5, aPublisher.getURL());
//下面这句抛出了SQLException异常
updatePublisher.setString(6, aPublisher.getPublisherID());测试前面5个语句均正常,getPublisherID() 返回String类型结果,
且经过测试getPublisherID()返回了一个非空的结果为什么会抛出SQLException,如何解决?谢谢

解决方案 »

  1.   

    抛出的是SQLException异常
    我在紧接updatePublisher.setString(6, aPublisher.getPublisherID());
    和catch(SQLException e){}中设了两个输出
    结果是紧接updatePublisher.setString(6, aPublisher.getPublisherID());没有输出
    而catch中的输出却出现了
      

  2.   

    最好把具体的SQLException信息贴出来。
    SQLException封装所有的数据库异常,只有从具体stack trace信息才能看出具体的错误,这样大家才能帮你。
      

  3.   

    如果你在sqlserver数据库中建立表示,将主键的属性设置成了IDENTITY的话,数据库是不会让你插入该字段的值的!
      

  4.   

    IDENTITY好像只能是数值类型吧?
    楼主的是字符类型,应该不存在这个问题。有两年没用sqlserver了,不知道记忆是否正确。
      

  5.   

    可能是你aPublisher.getPublisherID()返回的值的长度大于varchar(4), 或者看看你的表结构,以及因为pub_id是主键,是否存在重复主键的问题。
      

  6.   

    to  zhutouzip(Speak out!-shyboy) 我没有把主键设置为IDENTITY下面是SQLException的错误信息
    java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid parameter binding(s).
    at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
    at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
    at com.microsoft.jdbc.base.BasePreparedStatement.validateParameterIndex(Unknown Source)
    at com.microsoft.jdbc.base.BasePreparedStatement.setObjectInternal(Unknown Source)
    at com.microsoft.jdbc.base.BasePreparedStatement.setString(Unknown Source)
    at bookmanagement.SQLPublisherDataAccess.updatePublisher(SQLPublisherDataAccess.java:141)
      

  7.   

    还有我觉得不应该是重复主键的问题
    我看过原表中的pub_id都是不一样的
    还有我这些标的数据都是从SQL SERVER 2000的pubs数据库中导过来的
      

  8.   

    我做的实验显示,
    --create table myNewTable(
    --MemID int IDENTITY,
    --MemName varchar(10)
    --)
    --select * from myNewTable
    --insert into myNewTable values('Tiger')
    --update myNewTable set MemName='Laohu' where MemID=1
    更新记录的时候是可以读取IDENTITY字段的值的(update myNewTable set MemName='Laohu' where MemID=1语句工作正常),只是在IDENTITY_INSERT 不为 ON 时我们不能人为的显式给出IDENTITY字段的值!
      

  9.   

    fjpan2002(apan) 的"可能是你aPublisher.getPublisherID()返回的值的长度大于varchar(4), "观点我证实了一下,是对的哦!楼主看看去!