preparedStatement.setObject的第一个参数必须是数字吗?

为什么这样传进去会报错?

您好,preparedStatement是jdbc的标准类,它的作用主要是为占位符问号来设置相应的参数值,因此他的方法第一个参数必须是数字。如果您使用spring框架,可以使用NamedParameterStatement这个类来做一些类型替换,如下所示:

img


另外如果您使用了mybatis或者mybatis-plus等这样的框架也是支持使用字符替代占位符的,形如下面代码:

<insert id="insertJobLog" parameterType="JobLog">
         insert into sys_job_log(
             <if test="jobLogId != null and jobLogId != 0">job_log_id,</if>
             <if test="jobName != null and jobName != ''">job_name,</if>
             <if test="jobGroup != null and jobGroup != ''">job_group,</if>
             <if test="invokeTarget != null and invokeTarget != ''">invoke_target,</if>
             <if test="jobMessage != null and jobMessage != ''">job_message,</if>
             <if test="status != null and status != ''">status,</if>
             <if test="exceptionInfo != null and exceptionInfo != ''">exception_info,</if>
             create_time
         )values(
             <if test="jobLogId != null and jobLogId != 0">#{jobLogId},</if>
             <if test="jobName != null and jobName != ''">#{jobName},</if>
             <if test="jobGroup != null and jobGroup != ''">#{jobGroup},</if>
             <if test="invokeTarget != null and invokeTarget != ''">#{invokeTarget},</if>
             <if test="jobMessage != null and jobMessage != ''">#{jobMessage},</if>
             <if test="status != null and status != ''">#{status},</if>
             <if test="exceptionInfo != null and exceptionInfo != ''">#{exceptionInfo},</if>
             now()
         )
    </insert>
```xml