nath nath - 1 year ago 159
Java Question

Import CSV file to oracle DB

Is there a simple Java library or approach that will take a SQL query and load data in a CSV file to oracle database. Pls help

Answer Source

For similar tasks I usually use Groovy scripts as it's really easy and quick to write and runs on the JVM off course. example:

import groovy.sql.Sql

def file1 = new File(/C:\Documents and Settings\USER\Desktop\Book1.csv/)
def reader = new FileReader(file1)

def sql = Sql.newInstance("jdbc:oracle:thin:@XXXXXX:XXXX:XXX", "SCHEMA",
      "USER", "oracle.jdbc.driver.OracleDriver")

reader.each { line ->
   fields =  line.split(';')
   sql.executeInsert("insert into YOUR_TABLE values(${fields[0]},${fields[1]},${fields[2]})")

It's a basic example, if you have double quotes and semi columns in your csv you will probably want to use something like OpenCSV to handle that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download