在mysql数据库中插入一行,它显示两次行

Ash*_*hok 0 java mysql spring

代码是在corejava,spring,mysql执行控制器类输出正确得到1,但我插入一行但在mysql数据库中插入两行是主要问题,如果下面的代码有任何问题,如果我们插入请帮忙它必须在mysql数据库中只显示一行.

在IOC中包含

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
 <property   name="url" value="jdbc:mysql://localhost/Springjdbc"></property>
 <property name="username" value="root"></property>
 <property name="password" value="ashok"></property>
 </bean>

  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
   </bean>

<bean id="custDao" class="com.pw.spring.dao.CustomerDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)

在dao类

     package com.pw.spring.dao;

     import java.io.File;
     import java.io.IOException;
     import java.sql.PreparedStatement;
     import java.sql.ResultSet;
     import java.sql.SQLException;
     import java.util.List;
     import java.util.Map;
     import java.util.Set;

     import javax.imageio.stream.FileImageInputStream;

     import org.springframework.jdbc.core.BatchPreparedStatementSetter;
     import org.springframework.jdbc.core.PreparedStatementSetter;
     import org.springframework.jdbc.core.RowMapper;
     import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
     import org.springframework.jdbc.core.namedparam.SqlParameterSource;
     import org.springframework.jdbc.core.simple.SimpleJdbcCall;
     import org.springframework.jdbc.core.support.JdbcDaoSupport;

     import com.mysql.jdbc.Blob;
     import com.pw.spring.dto.B1;
     import com.pw.spring.dto.CustomerDto;

     public class CustomerDao extends JdbcDaoSupport implements ICustomerDao {
     @Override
     public int blobinsert() {
    int in = 0;
    String s1 = "insert into a1(name,image) values (?,?);";
    in = getJdbcTemplate().update(s1, new PreparedStatementSetter() {

    @Override
    public void setValues(PreparedStatement arg0) throws SQLException {

        arg0.setString(1, "shiva");
        arg0.setObject(2, b2 ());
        arg0.executeUpdate();
    }

    private Object b2() {
        File   f1 = new File("E:\\seenu\\New folder\\Luminance.jpg");
        byte z [] = new byte[(int)f1.length()];

        try
        {


        FileImageInputStream f2 = new FileImageInputStream(f1);

        f2.read(z);
        }
        catch(IOException ex)
        {
        ex.printStackTrace();
        }

        return z;
    }

    });


    return in;
        }
        }
Run Code Online (Sandbox Code Playgroud)

在要执行的控制器类中

package com.pw.spring.controller;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pw.spring.dao.CustomerDao;

public class Nutt 
{
    public static void main(String[] args) 
    {
         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");      
         CustomerDao dao =(CustomerDao) context.getBean("custDao");
         int i = dao.blobinsert();
         System.out.println(i)
    }
 }
Run Code Online (Sandbox Code Playgroud)

table是模式springjdbc中的a1

create table a1 (name varchar(20),image BLOB);


output:1
Run Code Online (Sandbox Code Playgroud)

但是它在mysql数据库中插入两次而不是像插入一行一样

shiva blob
shiva blob
Run Code Online (Sandbox Code Playgroud)

可以帮助如何在代码中的mysql数据库中插入一行?谢谢你的回答

Kai*_*Kai 6

不要叫arg0.executeUpdate();setValues().此方法用于设置不执行语句的值.

JdbcTemplate.update() 将执行该声明.