PraparedStatement setString 参数报错

IDEA中,使用PraparedStatement的setString方法时,第一个参数>1,都会这样报错,请问是什么问题?

public CarCard checkUser(String username,String password){
            try{
                pstmt = ct.prepareStatement("select * from [CarCard] where CardID=? and CardPwd=?");
                        pstmt.setString(1,username);
                        pstmt.setString(2,password);
                        ResultSet rs = pstmt.executeQuery();
                        CarCard user = new CarCard();
                        while(rs.next()){
                        user.setCardID(rs.getString(1));
                        user.setCardPwd(rs.getString(2));
                        //....
                        return user;
                        }
                        return null;
            }catch(Exception e){
                e.printStackTrace();
                return null;
            }
        }

你写错了。。。

正常这么写是没问题的

把你的sql发出来。跟你的sql有关系

 

```

public void createPublisher(BeanPublisher p) throws BaseException {
    if (p.getPubid() == null || "".equals(p.getPubid()) || p.getPubid().length() > 20) {
        throw new BusinessException("出版社编号必须是1-20个字");
    }
    if (p.getPublisherName() == null || "".equals(p.getPublisherName()) || p.getPublisherName().length() > 50) {
        throw new BusinessException("出版社名称必须是1-50个字");
    }
    if (p.getAddress() == null || "".equals(p.getAddress()) || p.getAddress().length() > 100) {
        throw new BusinessException("出版地址必须是1-100个字");
    }


    Connection conn = null;
    try {
        conn = DBUtil.getConnection();
        String sql = "select * from BeanPublisher where pubid=?";
        java.sql.PreparedStatement pst = conn.prepareStatement(sql);
        pst.setString(1, p.getPubid());
        java.sql.ResultSet rs = pst.executeQuery();
        if (rs.next()) throw new BusinessException("出版社编号已经被占用");
        rs.close();
        pst.close();
        sql = "select * from BeanPublisher where publisherName=?";
        pst = conn.prepareStatement(sql);
        pst.setString(1, p.getPublisherName());
        rs = pst.executeQuery();
        if (rs.next()) throw new BusinessException("出版社名称已经存在");
        rs.close();
        pst.close();
        sql = "insert into BeanPublisher(pubid,publisherName,address) values(?,?,?)";
        pst = conn.prepareStatement(sql);
        pst.setString(1, p.getPubid());
        pst.setString(2, p.getPublisherName());
        pst.setString(3, p.getAddress());
        pst.execute();
        pst.close();
    } catch (SQLException e) {
        e.printStackTrace();
        throw new DbException(e);
    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}

```

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632