Reputation: 1187
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource
import com.mysql.jdbc.*
import groovy.sql.*
/* the commented code works fine
MysqlDataSource ds = new MysqlDataSource()
ds.user = 'root'
ds.password = ""
ds.url = 'jdbc:mysql://localhost:3306/test'
Sql sql=Sql.newInstance(ds)
sql.close()
*/
d=Class.forName("com.mysql.jdbc.Driver").newInstance()
println d.class // class com.mysql.jdbc.Driver
Sql sql=Sql.newInstance(
'jdbc:mysql://localhost:3306/test',
'root',
"",
'com.mysql.jdbc.Driver'
)
the code commented works fine and I can get the instance of Driver
But when I use the
Sql sql=Sql.newInstance(
'jdbc:mysql://localhost:3306/test',
'root',
"",
'com.mysql.jdbc.Driver'
)
it throws a exception:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
I can not fix it ,is there anyboy coming to help me?
Upvotes: 8
Views: 8656
Reputation: 171114
The JDBC driver management in Java looks at the system classloader for the JDBC jars.
So to run a mysql accessing script in the GroovyConsole, you either need to use:
@GrabConfig( systemClassLoader=true )
@Grab( 'mysql:mysql-connector-java:5.1.27' )
in your script, or you need to start the console with the jar on the classpath by running it with:
groovyconsole -cp mysql-connector-java-5.1.27-bin.jar
I don't think there's a way to tell the add jars to path option to use the systemClassLoader :-(
Upvotes: 23