怎样用java实现csv中数据批量导入数据库
请高人指点。

解决方案 »

  1.   

    // 取得所有字段名
    String[] headers = firstLine.split(",",""); 
    String sql = " from tableName Where  1 = 0 ";
    for( int i =0 ; i < headers.length ; i++){
       sql = ","+sql + headers[i];
    }sql = " Select "+ sql.substring(1) ;Statement stmt  = conn .createStatement( 
      ResultSet.TYPE_SCROLLABLE_INSENSITIVE
    , ResultSet.CONCURRENT_UPDATABLE );ResultSet rs = stmt.executeQuery(sql);while( ( line = fileReader.readLine())!=null 
          && line.trim().length()>0 ){
        rs.moveToInsertRow();
     
       String[] values = line.split(",");
      for( int i =0 ; i < headers.length ; i++)
         rs.updateObject(i,values[i]);   rs.insertRow();
    }
       
      

  2.   

    thank you ,
    正在实践中,等做完了,给您送礼。
      

  3.   

    当执行rs.moveToInsertRow();会出以下错误。com.mysql.jdbc.NotUpdatable: Result Set not updatable.This result set must come from a statement that was created with a result set type of ResultSet.CONCUR_UPDATABLE, the query must select only one table, and must select all primary keys from that table. See the JDBC 2.1 API Specification, section 5.6 for more details.
      

  4.   

    csv中数据批量导入数据库  是啥意思俺不懂(呵呵,新手)
    不过如果是从一个文件批量导入到数据库的话,这倒可以交给数据库系统来做,也就是说用一个SQL语句,即  bulk insert table_name from file_path with (terminatefield ',')然后用一个Statement的executeUpdate方法就行了,
    说明一下,上面的SQL语句可能有语法或单词拼写错误,因为记不太清了,大致就这样,可以查相关SQL语法手册.还有文件路径可用File对象的getAbsolutePath()获得.