简体   繁体   English

如何执行 MySQL sql '开始...结束;' 使用 JDBC

[英]How to execute MySQL sql 'begin… end;' using JDBC

I'm using Mysql 5.7 and JDBC, I need to run below query and get result data using JDBC:我正在使用 Mysql 5.7 和 JDBC,我需要在查询下运行并使用 JDBC 获取结果数据:

set @rownum := 0;
select *
from (select *, @rownum := @rownum + 1 as rownum from test) t
where t.rownum >= 1
  and t.rownum <= 10;

table structure and init sql:表结构和初始化 sql:

create table test(id int, name varchar(16));
insert into test(id, name) VALUES (1,'rollsbean');

I tried executeQuery() and conn.prepareCall() and run failed.我尝试了executeQuery()conn.prepareCall()并运行失败。

My test code:我的测试代码:

public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = DriverManager.getConnection("jdbc:mysql://172.16.100.115:3306/valid?user=drpeco&password=DT@Stack#123&serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false");
            String plsql = "set @rownum := 0;\n" +
                    "select *\n" +
                    "from (select *, @rownum := @rownum + 1 as rownum from test) t\n" +
                    "where t.rownum >= 1\n" +
                    "  and t.rownum <= 10;";
            CallableStatement cstmt = conn.prepareCall(plsql1);
            cstmt.execute();
            Object object = cstmt.getObject(1);
//            testExecute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
stmt = conn.createStatement();
            String plsql = "set @rownum := 0;\n" +
                    "select *\n" +
                    "from (select *, @rownum := @rownum + 1 as rownum from test) t\n" +
                    "where t.rownum >= 1\n" +
                    "  and t.rownum <= 10;";
            rs = stmt.executeQuery(plsql);
            while (rs.next()) {
                Object object = rs.getObject(1);
            }

What i want: I want to do paging query using MySQL custom variable, mysql 5.7 don't support row_number() over () .我想要什么:我想使用 MySQL 自定义变量进行分页查询,mysql 5.7 不支持row_number() over ()

Question: how to execute these query and get resulte data using JDBC.问题:如何使用 JDBC 执行这些查询并获取结果数据。

Additional: the target table i searched don't have specific columns and index, if i use limit to do paging query.附加:如果我使用limit进行分页查询,我搜索的目标表没有特定的列和索引。 the efficiency will very low in deep pages, so i want to try row_number , if anyone have better solution to query, please share with me, i will very happy for your answer.深度页面的效率会很低,所以我想试试row_number ,如果有人有更好的查询解决方案,请与我分享,我会很高兴得到你的回答。

My Requirement: as i said in the Additional, my app will run sql jobs and generate many template table with data, the data is depend on the sql, and we can download all the data from one specific template table without any conditions, in this step, i will do paging query to read我的要求:正如我在附加中所说,我的应用程序将运行 sql 作业并生成许多带有数据的模板表,数据取决于 sql,我们可以在没有任何条件的情况下从一个特定模板表中下载所有数据,在此一步,我会做分页查询来阅读
data.数据。

Use LIMIT with two parameters.将 LIMIT 与两个参数一起使用。 For example, to return results 11-60 (where result 1 is the first row), use:例如,要返回结果 11-60(其中结果 1 是第一行),请使用:

SELECT * FROM foo LIMIT 10, 50

For more details look here更多详情请看这里

Why dont you use OFFSET.为什么不使用偏移量。 I believe this would help you.我相信这会对你有所帮助。

SELECT id, name FROM any_table LIMIT 15000 OFFSET 15000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM