boardtc (8) [Avatar] Offline
#1
I am using the groovy plug-in in with the latest Eclipse and have added a groovy nature.

On pg 328 it says "your database vendor provides its own implementation of the javax.sql.DataSource interface"

I am using MySQl. After extensive searching I have managed to locate
com.mysql.jdbc.jdbc2.optional.MysqlDataSource
is this it?

With the code on this page (source.database =, etc), I get a red dot under source.database saying syntax error on tokens misplaced syntax.

import groovy.sql.Sql;
import javax.sql.DataSource;
import com.mysql.jdbc.*;

com.mysql.jdbc.jdbc2.optional.MysqlDataSource
source.database = 'jdbc:database:mydatabase'

Is this (pg 32smilie java or groovy? How can I get Eclipse to recognise it?

Thanks, Tom.
fjanon (62) [Avatar] Offline
#2
Re: Setting up MySql datasource
Hi Tom,

I am sure that someone more qualified will answer when Europe wakes up... In the mean time: the code in the book p328 is Groovy code (first clue: there is no ';' at the end of the lines).

The code you included looks more like Java code for the imports (optional ';' with the imports) but is Groovy code.

Have you put the jar that contains the com.mysql.jdbc.jdbc2.optional.MysqlDataSource in your classpath? The how about writing the code as shown in GinA and substitute the mySQL name in it?

// Make sure the jar in on the classpath
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource

source = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource()
source.database = 'jdbc:database:mydatabase'
db = new groovy.sql.Sql(source)
if (!db) println "Error getting DB connection"

Fred
boardtc (8) [Avatar] Offline
#3
Re: Setting up MySql datasource
Thanks Fred.

> The code you included looks more like Java code for
> the imports (optional ';' with the imports) but is
> Groovy code.
>
> // Make sure the jar in on the classpath
> import com.mysql.jdbc.jdbc2.optional.MysqlDataSource

I have added mysql-connector-java-5.0.4-bin.jar to the project and that includes the above path.

source = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource()
source.database = 'jdbc:mysql:'

Eclipse red lines Source as well as the missing colon at the end. Previously, I put the imports in to try and make the red go away but they are now gone! I have added a groovy nature, what else do I have to do to make this recognised as groovy rather than Java?
fjanon (62) [Avatar] Offline
#4
Re: Setting up MySql datasource
The Eclipse plugin sometimes shows red icons at the beginning of Groovy lines even when the code is fine, compiles fine and runs fine. In my experience, forcing rebuilding the project sometimes solves the problem.

I had some issues adding Groovy files directly in an existing project and I solved it by deleting the groovy file, right clicking on the project then new > other > Groovy > Groovy class (using the Groovy nature to create the file instead of adding the file directly in the project). Then pasting the code back in that file.

Hopefully you'll get it working. Remember, even if the code is shown in error, sometimes it still compiles and runs fine. I currently have a mixed Java-Groovy project that exhibits that behavior and the application runs fine.
boardtc (8) [Avatar] Offline
#5
Re: Setting up MySql datasource
I did not realise the file should be called MySqlImport.groovy and all the red goes away.

However, the line:

source = new com.mysql.jdbc.jdbc2.optional.MysqlDataSource()

gives an Unexpected exception: groovy.lang.MissingPropertyException : No such property: source for class:

I will move this discussion to the groovy newsgroup. Thanks.