OpenCSV icon indicating copy to clipboard operation
OpenCSV copied to clipboard

Need more documentation or reference example

Open anjuls opened this issue 5 years ago • 0 comments

I am getting below error. Could you please help putting a sample program to use opencsv?

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleConnection;
import java.sql.DatabaseMetaData;


import com.opencsv.*;


public class fetchUptime {  
  /**
   *
   */

  private static final String HEADER = "SCAN_DATE,HOST_NAME,UPTIME";
  // The recommended format of a connection URL is the long format with the
  // connection descriptor.
  final static String DB_URL = "jdbc:oracle:thin:@xxxx:1521/xxxx";
  // For ATP and ADW - use the TNS Alias name along with the TNS_ADMIN when using
  // 18.3 JDBC driver
  // final static String
  // DB_URL="jdbc:oracle:thin:@wallet_dbname?TNS_ADMIN=/Users/test/wallet_dbname";
  // In case of windows, use the following URL
  // final static String
  // DB_URL="jdbc:oracle:thin:@wallet_dbname?TNS_ADMIN=C:\\Users\\test\\wallet_dbname";
  final static String DB_USER = "x";
  final static String DB_PASSWORD = "x";

  /*
   * The method gets a database connection using
   * oracle.jdbc.pool.OracleDataSource. It also sets some connection level
   * properties, such as,
   * OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH,
   * OracleConnection.CONNECTION_PROPERTY_THIN_NET_CHECKSUM_TYPES, etc., There are
   * many other connection related properties. Refer to the OracleConnection
   * interface to find more.
   */
  public static void main(String args[]) throws SQLException {
    Properties info = new Properties();
    info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
    info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);
    info.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "1000");

    OracleDataSource ods = new OracleDataSource();
    ods.setURL(DB_URL);
    ods.setConnectionProperties(info);

    // With AutoCloseable, the connection is closed automatically.
    try (OracleConnection connection = (OracleConnection) ods.getConnection()) {
      // Get the JDBC driver name and version
      DatabaseMetaData dbmd = connection.getMetaData();
      System.out.println("Driver Name: " + dbmd.getDriverName());
      System.out.println("Driver Version: " + dbmd.getDriverVersion());
      // Print some connection properties
      System.out.println("Default Row Prefetch Value is: " + connection.getDefaultRowPrefetch());
      System.out.println("Database Username is: " + connection.getUserName());
      System.out.println();
      // Perform a database operation
      printUptime(connection);
    }
  }

  /*
   * Displays first_name and last_name from the employees table.
   */
  public static void printUptime(Connection connection) throws SQLException {
    // Statement and ResultSet are AutoCloseable and closed automatically.
    try (Statement statement = connection.createStatement()) {
      try (ResultSet resultSet = statement
          .executeQuery("select scan_date, host_name,up_time from uptimes where rownum<10")) {
        // System.out.println("SCAN_DATE" + " " + "HOST_NAME" + " " + "UP_TIME");
        // System.out.println("---------------------");
        // while (resultSet.next())
        // System.out.println(resultSet.getString(1) + " "
        // + resultSet.getString(2) + " " + resultSet.getString(3));
        try {
          ResultSet2CSV(resultSet, "./uptimes.csv", HEADER, false);
          } catch (Exception e) {
            System.out.println("Error happened.");
          }    
      }
    }   
  } 

  public static int ResultSet2CSV(final ResultSet rs, final String fileName, final String header, final boolean aync) throws Exception {
    try (CSVWriter writer = new CSVWriter(fileName)) {
        //Define fetch size(default as 30000 rows), higher to be faster performance but takes more memory
        ResultSetHelperService.RESULT_FETCH_SIZE=10;
        //Define MAX extract rows, -1 means unlimited.
        ResultSetHelperService.MAX_FETCH_ROWS=200;
        writer.setAsyncMode(aync);
        int result = writer.writeAll(rs, true);
        return result - 1;
    }
}
}

   2019-07-16 00:43:14: 0 rows extracted, total: 0 rows, 0.00 MB, 0.001 secs on fetching.
Exception in thread "main" java.lang.AbstractMethodError: Method oracle/jdbc/driver/OracleResultSetImpl.getObject(ILjava/lang/Class;)Ljava/lang/Object; is abstract
        at oracle.jdbc.driver.OracleResultSetImpl.getObject(OracleResultSetImpl.java)
        at com.opencsv.ResultSetHelperService.getColumnValues(ResultSetHelperService.java:204)
        at com.opencsv.ResultSetHelperService.getColumnValues(ResultSetHelperService.java:159)
        at com.opencsv.CSVWriter.writeAll(CSVWriter.java:278)
        at com.opencsv.CSVWriter.writeAll(CSVWriter.java:244)
        at fetchUptime.ResultSet2CSV(fetchUptime.java:116)
        at fetchUptime.printUptime(fetchUptime.java:101)
        at fetchUptime.main(fetchUptime.java:84)

anjuls avatar Jul 15 '19 15:07 anjuls