错误:SQLException:参数索引超出范围(1>参数数量,为0)

时间:2019-06-10 05:07:29

标签: java jdbc prepared-statement

即使上面准备好的语句的语法是正确的,我仍然遇到以下错误:

  

java.sql.SQLException:参数索引超出范围(1>参数数量,为0)

预期结果是以下代码必须返回表的值

try{
        conn = DatabaseMain.getConnection();
        String instqury = "Select quality_name,quality_size,quality_weight,quality_spec from quality where quality_code = '?'";
        pst1 = conn.prepareStatement(instqury);
        pst1.setString(1, quality);

        rs1 = pst1.executeQuery();
        rs1.next();
        name.setText(String.valueOf(rs1.getString("quality_name")));

    }
    catch(Exception e){
        e.printStackTrace();
        System.out.println("Error in setting labels for Machine View");
    }

2 个答案:

答案 0 :(得分:2)

请勿将占位符放在单引号之间

String instqury = 
    "Select quality_name,quality_size,quality_weight,quality_spec " + 
    "from quality where quality_code = ?";
                                       ^-- here

答案 1 :(得分:1)

尝试在占位符?中删除单引号吗?查询正在将其读取为提供的值。