java获取sql查询出的字段,如何从SQL查询(Jooq,Java)获取列名

Hello I have a problem with obtain values from SQL query (in Java with using library of jooq)?

create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))

or

insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')

What I need to obtain (parse/get) are values: Meno, Priezvisko, Vek.

Is it possible somehow get it from sql query name of columns of table (with some jooq method)?

解决方案

From your question, I'm assuming you'd like to use the jOOQ parser API to parse your SQL string and then extract the column names from jOOQ's meta model.

Currently (as of jOOQ 3.11), the meta model is not available through a public API. You can access it only by means of using a VisitListener, which is an SPI that is called on every QueryPart (i.e. expression tree element) that is contained in the meta model. This example implementation can give you an idea:

import org.jooq.*;

import org.jooq.impl.*;

public class Columns {

public static void main(String[] args) {

var parser =

DSL.using(new DefaultConfiguration().set(new DefaultVisitListener() {

@Override

public void visitStart(VisitContext ctx) {

if (ctx.queryPart() instanceof Field

&& !(ctx.queryPart() instanceof Param))

System.out.println(((Named) ctx.queryPart()).getQualifiedName());

}

})).parser();

System.out.println("Query 1");

System.out.println("-------");

parser.parseQuery("create table `filetest`(`id` int not null auto_increment, `Meno` varchar(21) null, `Priezvisko` varchar(24) null, `Vek` int null, constraint `pk_filetest` primary key (`id`))").getSQL();

System.out.println();

System.out.println("Query 2");

System.out.println("-------");

parser.parseQuery("insert into `filetest` (`Meno`, `Priezvisko`, `Vek`) values ('Jack', 'Daniels', '21')").getSQL();

}

}

It will print:

Query 1

-------

"id"

"Meno"

"Priezvisko"

"Vek"

"id" -- Field is referenced again from the constraint

Query 2

-------

"Meno"

"Priezvisko"

"Vek"

Another option is, of course, to use reflection to access jOOQ's internals.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值