好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用JDBC批量保存数据(JdbcDaoSupport,JdbcTemplete)

import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; public class ConnectionUtil{ public static final ThreadLocal<Connection> connections = new ThreadLocal<Connection> (); public static Connection getConnection(DataSource dataSource) throws SQLException{   Connection c = connections. get ();   if ( null == c){     c = dataSource.getConnection();     connections.set(c);
   }   return c;   } }

spring的配置文件就不搞了下面是dao层的具体实现

 @Repository
@Scope(  "  prototype  "  )
  public   class   SyslogAlarmDao2 extends JdbcDaoSupport implements InitializingBean{
  @resource
    public   void   setDatasource(DataSource dataSource){
      this  .setDataSource(dataSource);
  }
    private   long  count =  0L  ;
  String sql1  =  ""  ;
  String sql2  =  ""  ;
  PreparedStatement ps1  =  null  ;
  PreparedStatement ps2  =  null  ;
  Connection c  =  null  ;
   /**
  这里有篇文章可以帮助理解下面这个注解 http://blog.csdn.net/yaerfeng/article/details/8447530
  */
  @PostConstruct //    public void set () throws SQLException{     sql1 = " sql " ; //省略,带问号的sql字符串     sql2 = " sql " ; //同上     c = ConnectionUtil.getConnection( this .getDataSource());     ps1 = c.prepareStatement(sql1);     ps2 = c.prepareStatement(sql2);
   /**
由于主键是手动控制的,所以需要查一下数据库中已经存在的id的最大值,然后从最大值+1处开始添加数据
alarm和syslogalarm的主键值相同,所以查一个就可以
   */     PreparedStatement ps3 = c.prepareStatement( " select max(id) from alarm2 " );     ResultSet rs = ps3.executeQuery();      while (rs.next()){      count = rs.getLong( 1 );     }   } public void executeBatch(SyslogAlarm sAlarm) throws SQLException{    if (sAlarm== null ||sAlarm.getAlarm == null ){     System. out .println( " input error " );      return ;   }   ps1.setLong( 1 ,count); //从sAlarm中取值填充到ps1的sql字符串中  
// ..............   ps2.setLong( 1 ,count);   //从sAlarm中取值填充到ps2的sql字符串中  
// ..........   ps1.addBatch();   ps2.addBatch();   System. out .println( " 调用了 " +count+ " 次 " );    if (count% 1000 == 0 ){ //为10的时候插入一万条需要4.061秒,为100插入一万数据需要1.403秒,为1000的时候插入一万条数据需要大概需要0.747秒时间     ps1.executeBatch();     ps2.executeBatch();     ps1.clearBatch();     ps2.clearBatch();   }    // ps1.executeBatch(); // 执行剩余sql    // ps2.executeBatch(); //同上 } }

测试

 //  Service shenglue 
 
@Test
  public   void   testBatchInsert(){
ApplicationContext ac  =  new  ClassPathXmlApplicationContext( "  spring xml path  "  );
  double  start =  System.currentTimeMillis();
SyslogAlarmService sas  = (SyslogAlarmService) ac.getBean( "  syslogAlarmService  "  );

  for ( int  i= 1 ;i< 10001 ;i++ ){//当为1000批处理时,插入一万数据需要一秒左右,十万数据7秒左右,20万数据13秒左右,50万数据31秒左右
Device d  =  new   Device();
d.setId(  1  );

Alarm alarm  =  new   Alarm();
alarm.setDevice(d);
  //  alarm.set 
 
SyslogAlarm sAlarm  =  new   SyslogAlarm();
sAlarm.setAlarm(alarm);
  //  sAlarm.set 
 
sas.batchInsert(sAlarm);

  double  end =  System.currentTimeMillis();
System.  out .println( "  执行了 :  " +(end-start)/ 1000 +  "   秒  "  );
}
} 

 

使用JDBC批量保存数据(JdbcDaoSupport,JdbcTemplete)

标签:

查看更多关于使用JDBC批量保存数据(JdbcDaoSupport,JdbcTemplete)的详细内容...

  阅读:59次