Connect groovy to sql server

I am trying to connect a simple groovy script to a DB.

code:

import groovy.sql.Sql class GroovySqlExample2{ static void main(String[] args) { def sql = Sql.newInstance("jdbc:sqlserver://MYSERVERIP", "uname", "pwd", "net.sourceforge.jtds.jdbc.Driver") sql.eachRow("select * from word"){ println it.spelling + " ${it.part_of_speech}" } } } 

I placed jtds-1.2.3.jar inside the C: \ groovy -1.6.3 \ lib folder, but the code above continues to complain:

 java.lang.ClassNotFoundException: net.sourceforge.jtds.jdbc.Driver 
+4
source share
3 answers

Make sure the environment variable GROOVY_HOME is set to c: \ groovy -1.6.3

+1
source

Use Grape and set systemClassLoader=true

 @Grapes( @Grab(group='net.sourceforge.jtds', module='jtds', version='1.3.1') ) @GrabConfig(systemClassLoader=true) import groovy.sql.* // http://jtds.sourceforge.net/faq.html#urlFormat def sql = Sql.newInstance("jdbc:jtds:sqlserver://MYSERVERIP", "uname", "pwd", "net.sourceforge.jtds.jdbc.Driver") sql.eachRow("select * from word"){ println it.spelling + " ${it.part_of_speech}" } 
+3
source

Download jtds-1.2.2.jar and add the line below in the groovy script to def sql.

 this.class.classLoader.rootLoader.addURL( new URL("file:/C:\\jtds-1.2.2.jar")) 
+2
source

All Articles