java.sql.SQLException: No value specified for parameter 1 异常分析

本文分析了遇到`java.sql.SQLException: No value specified for parameter 1`错误的原因,该错误通常发生在尝试执行SQL查询时,没有正确设置查询中的参数占位符。解决方案是在调用`executeQuery()`方法前确保占位符已赋值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误源码 java.sql.SQLException: No value specified for parameter 1,因为执行executeQuery()先于对sql语句中的占位符赋值

    @Test
    public void test2() {
        InputStream is = null;
        FileOutputStream fos = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select id,name,email,birth,photo from customers where id = ?";
            ps = conn.prepareStatement(sql);
            
//            ps.setInt(1,23);
            
            rs = ps.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String email = rs.getString("email");
                Date birth = rs.getDate("birth");

                Customers customers = new Customers(id, name, email, birth);
                System.out.println(customers);
                //将Blob类型的字段下载下来,以文件的方式保存在本
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值