在 Groovy 控制台中注册和使用 JDBC 驱动程序

问题描述 投票:0回答:1

我是 Groovy 新手,需要一些帮助来创建使用 JDBC 驱动程序连接到 Databricks 的脚本。我拥有的是...

// Import the Groovy class required to work with SQL databases
import groovy.sql.Sql
import groovy.sql.DataSet
import java.sql.*

//Register driver --have tried both of these options separately with no success
//DriverManager.registerDriver(new com.databricks.client.jdbc42.Driver())
//Class.forName('com.databricks.client.jdbc42.Driver')

// Set up database connection properties
def url = 'jdbc:databricks://<<databricks server info>>:443' 
def user = 'username'
def password = 'pwd'
def driver = 'com.databricks.client.jdbc42.Driver'


// Connect to the SQL instance
def sql = Sql.newInstance(url, user, password, driver)

在阅读了本网站和其他网站上的大量帖子后,我将环境变量 CLASSPATH 定义为本地系统上 databricks JDBC .jar 文件的文件夹结构,并假设我根据 DataBricks 上的文档正确定义了“驱动程序”地点。但是,如果没有这两行来注册驱动程序,我会收到消息“java.lang.ClassNotFoundException:com.databricks.client.jdbc42.Driver”。使用注册行“DriverManager.registerDriver(new com.databricks.client.jdbc42.Driver())”我收到异常消息“无法解析类com.databricks.client.jdbc42.Driver”,并使用注册行“Class.forName(com.databricks.client.jdbc42.Driver)”我收到异常消息“groovy.lang.MissingPropertyException: No such property: com for class: ConsoleScript21”或“java.lang.ClassNotFoundException: com.databricks.客户端.jdbc42.驱动程序”

我想了解如何注册 JDBC 驱动程序以及“com.databricks.client.jdbc42.Driver”如何告诉脚本在我的系统上哪里可以找到驱动程序(并且请随时与我交谈,就像我是 4-一岁了,所以我了解它最基本的水平)。

我看过帖子What is classpath for Groovy Console / jdbc driver prblem?,但无法解决我的问题。

jdbc groovy databricks-sql
1个回答
0
投票

不确定你在jdbc类名中取了

42
在哪里...

根据文档,类名应该是:

com.databricks.client.jdbc.Driver

加载jdbc驱动程序的代码:

@Grab(group='com.databricks', module='databricks-jdbc', version='2.6.36')
@GrabConfig(systemClassLoader=true)

def cl = Class.forName('com.databricks.client.jdbc.Driver')

Grab
的参数可以通过类名搜索:https://search.maven.org/search?q=fc:com.databricks.client.jdbc.Driver

使用

@GrabConfig(systemClassLoader=true)
非常重要 - 它使 jar 在系统类加载器下加载,这使得 jdbc 驱动程序管理器可见。

© www.soinside.com 2019 - 2024. All rights reserved.