代碼是在corejava,春天,mysql執行控制器類輸出是 得到1正確, 但我插入一行,但在MySQL數據庫兩行插入它是主要問題,如果在下面的代碼中有任何 問題,請幫助我們插入一行它只在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>
在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;
}
}
要執行
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)
}
}
表是在模式A1 springjdbc
create table a1 (name varchar(20),image BLOB);
output:1
但它是在MySQL數據庫中插入兩次行,而不是一行將像
shiva blob
shiva blob
可以幫助如何在代碼中插入一行在mysql數據庫? 高級感謝您的回答
非常感謝baraky – Ashok 2013-03-20 15:49:19