Query CSV File from Java - Backing Bean

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

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

//...
//Start of Program
public class ReadCSVFileBean
{
	
	//...
	//Button code
  public static void doGetCSVConsoleData()
  {
	  //...
	  //Entering try catch
    try
    {
      // load the driver into memory
      Class.forName("org.relique.jdbc.csv.CsvDriver");

      // create a connection. The first command line parameter is assumed to
      //  be the directory in which the .csv files are held
      Connection conn = DriverManager.getConnection("jdbc:relique:csv:C:\\temp\\CSV_DATA\\");

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

      // Select* from UserNewLineOutboundInformation.csv
      ResultSet results = stmt.executeQuery("SELECT * FROM UserNewLineOutboundInformation");

      // dump out the results
      while (results.next())
      {
    	  
    	  //TO DO: Write a separate JavaBean to build setters for variables
    	  //that can used in Xpages...
    	String SiteName = results.getString("SiteName");
    	String SiteNumber = results.getString("SiteNumber");
    	//Load to Console
        System.out.println("SiteName " + " SiteNumber");
        System.out.println(SiteName + " " + SiteNumber);
      }

      // clean up
      results.close();
      stmt.close();
      conn.close();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }//end of program...
  
  
}





Query CSV file into Java Application, add results to console.  To be honest, not too much testing on this one, play around with it, see what else you can do...  Will not post an Xpage this time, you can add under a button/link as you wish.  

Note: If you build a JavaAgent, you will not need the jdbc jar file, but from Project Explorer, you need the jar in the CLASSPATH, and it should be referenced via build path...  Also seems to work well having the jar in the /lib/ext directory in Notes.

Java
Köll S Cherizard
April 21, 2012 7:41 PM
Rating
155

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