JavaBean to Fill DB2 Column with CSV data


/**
 * Created: 2012.04.21.5.56.PM
 * Build Results into DB2/NSF with CSV
 */
package com.dokoll.solutions.inc.cvs.trials;

/**
 * @author Dököll Solutions, Inc.
 * @version 2012.04.21.5.56.PM
 *
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.faces.context.FacesContext;

import com.dokoll.solutions.inc.developement.Utils.DB2Connector;

import lotus.domino.local.Database;
import lotus.domino.local.Document;

//...
//Start of Program
public class ReadCSVFileIntoDB2Bean
{
	
	//...
	//Button code
  public static void doGetCSVForDB2Data()
  {
	  //...
	  //Entering try catch
    try
    {
    	 Connection connection =  DB2Connector.getConnection();

      // create a Statement object to execute the query with
      //2012.04.21.6.02.PM
      //TO DO: Attempt using Prepared Statement here...
      Statement statement = connection.createStatement();

      // Select* from UserNewLineOutboundInformation.csv
      BufferedReader br = new BufferedReader(new FileReader("C:\\temp\\CSV_DATA\\BULK_CSV_COM_DATA_SPLIT_2012.csv"));
      String line;
      while ( (line=br.readLine()) != null)
      {
               String[] values = line.split(",");    //your seperator

               //Convert String to right type. Integer, double, date etc.
               statement.executeUpdate("INSERT INTO DB2ADMIN.NOTES_DOCS_RPT VALUES('"+values[0]+"','"+values[1]+"','"+values[2]+"','"+values[3]+"','"+values[4]+"','"+values[5]+"','"+values[6]+"','"+values[7]+"','"+values[8]+"','"+values[9]+"','"+values[10]+"')");
               //Use a PeparedStatemant, it´s easier and safer 
               
               System.out.println("Insert these in to DB2: " + values);
      }
      br.close();
      // clean up
      statement.close();
      connection.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
  
  
  
  }
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...