0

I'm trying to save csv file data into my mysql database table using groovy language. I'm new to this language, can anyone please help me in this?? below is piece of code I have written yet.


import groovy.sql.Sql

def sql = Sql.newInstance("jdbc:mysql://localhost:3306/csvdb",
  "root", "", "com.mysql.jdbc.Driver")
def users = sql.dataSet("USERS")
new File("C:\Users\lllha\Desktop\users.csv").splitEachLine(",") {fields ->
 users.add(
    first_name: fields[0],
    last_name: fields[1],
    age: field[2],
    gender: field[3],
    email: fields[4],
    phone: fields[5]
  )
}
3
  • 1
    Upload CSV to MySQL server if it is not local for MySQL now, then use LOAD DATA query. Transferring the data via the client is not reasonable in most cases. Commented Mar 31, 2021 at 10:26
  • What is your question? How does your code fail? Please add an actual problem statement. Commented Mar 31, 2021 at 17:39
  • Look at this example: stackoverflow.com/a/36884925/3355860 Commented Apr 1, 2021 at 11:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.