0

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

在国际奥委会包含

 <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>

在道班

     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;
        }
        }

在要执行的控制器类中

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)
    }
 }

表是模式springjdbc中的a1

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


output:1

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

shiva blob
shiva blob

可以帮助如何在代码中在mysql数据库中插入一行?提前感谢答案

4

2 回答 2

6

不要打arg0.executeUpdate();进来setValues()。此方法用于设置值而不是执行语句。

JdbcTemplate.update()将执行语句。

于 2013-03-20T15:14:04.897 回答
4

去掉arg0.executeUpdate();里面setValues

您执行插入语句两次:inin = getJdbcTemplate().update(..)和 inarg0.executeUpdate();

于 2013-03-20T15:16:02.510 回答