CINXE.COM
SQL query | Intro | Ebean
<!doctype html> <html lang="en"> <head> <title>SQL query | Intro | Ebean</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="shortcut icon" href="/images/favicon.ico"> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro|Ubuntu&display=swap"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous"> <link rel="stylesheet" href="/css/reset3.css"> <link rel="stylesheet" href="/css/site3.css"> <link rel="stylesheet" href="/css/pygments3.css"> </head> <body> <div id="main"> <div id="banner"> <header> <nav id="top"> <h1 id="breadcrumb"> <a class="nav-logo" href="/"><img src="/images/logo-200.png" height="35"></a> <a href="/docs">Documentation</a><span class="sep"> / </span><a href="/docs/intro/">Introduction</a><span class="sep"> / </span><a href="/docs/intro/queries">Queries</a><span class="sep"> / </span><span class="last">SQL query</span> </h1> <ul> <li><a onclick="toggleTheme();" title="switch dark light theme"><i class="fas fa-adjust"></i></a></li> </ul> </nav> </header> </div> <div class="grid grid-docs"> <aside> <nav class="side"> <ul> <li class="nav0 "> <a href="/docs/getting-started">Getting started</a> </li> <li class="nav0 active"> <a class="active" href="/docs/intro">Introduction</a> <ul> <li class="nav1 "> <a href="/docs/intro/database">Database & DB</a> </li> <li class="nav1 "> <a href="/docs/intro/configuration">Configuration</a> </li> <li class="nav1 "> <a href="/docs/intro/first-entity">First entity</a> </li> <li class="nav1 active"> <a class="active" href="/docs/intro/queries">Queries</a> <ul> <li > <a href="/docs/intro/queries/orm-query">ORM query</a> </li> <li > <a href="/docs/intro/queries/dto-query">DTO query</a> </li> <li class="active"> <a class="active" href="/docs/intro/queries/sql-query">SQL query</a> </li> <li > <a href="/docs/intro/queries/jdbc-query">JDBC query</a> </li> </ul> </li> <li class="nav1 "> <a href="/docs/intro/logging">Logging</a> </li> <li class="nav1 "> <a href="/docs/intro/db-migrations">DB Migrations</a> </li> <li class="nav1 "> <a href="/docs/intro/limitations">Limitations</a> </li> <li class="nav1 "> <a href="/docs/trouble-shooting">Trouble shooting</a> </li> </ul> </li> <li class="nav0 "> <a href="/docs">Documentation</a> </li> <li class="nav0 "> <a href="/support">Getting help</a> </li> <li class="nav0 "> <a target="_blank" href="/apidoc/13">API Javadoc</a> </li> <li class="nav0 "> <a href="/videos">Videos</a> </li> <li class="nav0 "> <a href="/docs/upgrading">Upgrading</a> </li> <li class="nav0 "> <a href="/releases">Releases</a> </li> </ul> </nav> </aside> <article> <form action="https://www.google.com/search" method="get" class="inline-form"> <input type="hidden" name="as_sitesearch" value="ebean.io"> <div id="page-search"> <div class="input-group"> <input class="frm" name="q" id="searchinput" type="text" placeholder="Search... (press 's' to focus)" data-placeholder-focus="Search... (use '↑', '↓' and '⏎' to select results)" data-placeholder-blur="Search... (press 's' to focus)" autocomplete="off"> <div class="input-group-btn"> <button class="frm" type="submit"><i class="fas fa-search"></i></button> </div> </div> <div id="page-search-results" style="display: none;"> <ul id="search-results-container" class="search-results"><li class=" active"><a href="/docs" title="Docs"><span style="color:#777;">Docs</span> Documentation </a></li><li class=""><small style="color:#999;">And 101 more...</small></li></ul> </div> </div> </form> <h2>SQL Query</h2> <p> Sometimes it is useful to not use entity beans or dto beans but instead execute sql queries producing <code>SqlRow</code> objects or reading directly from JDBC ResultSet. </p> <p> In typical recent applications around <code>2%</code> of queries were SqlQuery type queries. </p> <h3>SqlQuery and SqlRow</h3> <p> Note that with SqlQuery we can get Ebean to add <code>limit/offset</code> clause appropriate to the database platform and we can bind parameters using all the mapped types (Enums, java.time, array types etc). </p> <p> For more information goto <a href="/docs/query/sqlquery">docs / query / sqlquery</a> </p> <ul class="code nav nav-tabs mytabs"> <li role="presentation" class="javaActive active"><a onclick="setLang('java');">java</a></li> <li role="presentation" class="kotlinActive"><a onclick="setLang('kt');">kotlin</a></li> </ul> <div class="code-java"> <div class="syntax java"><div class="highlight"><pre><span></span><span class="n">String</span> <span class="n">sql</span> <span class="o">=</span> <span class="s">"select id, name, when_active from customer where status = ?"</span><span class="o">;</span> <span class="n">List</span><span class="o"><</span><span class="n">SqlRow</span><span class="o">></span> <span class="n">rows</span> <span class="o">=</span> <span class="n">DB</span><span class="o">.</span><span class="na">sqlQuery</span><span class="o">(</span><span class="n">sql</span><span class="o">)</span> <span class="o">.</span><span class="na">setParameter</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">"NEW"</span><span class="o">)</span> <span class="o">.</span><span class="na">setMaxRows</span><span class="o">(</span><span class="mi">100</span><span class="o">)</span> <span class="o">.</span><span class="na">findList</span><span class="o">();</span> <span class="k">for</span> <span class="o">(</span><span class="n">SqlRow</span> <span class="n">sqlRow</span> <span class="o">:</span> <span class="n">rows</span><span class="o">)</span> <span class="o">{</span> <span class="n">Long</span> <span class="n">id</span> <span class="o">=</span> <span class="n">row</span><span class="o">.</span><span class="na">getLong</span><span class="o">(</span><span class="s">"id"</span><span class="o">)</span> <span class="n">String</span> <span class="n">name</span> <span class="o">=</span> <span class="n">row</span><span class="o">.</span><span class="na">getString</span><span class="o">(</span><span class="s">"name"</span><span class="o">)</span> <span class="n">Timestamp</span> <span class="n">whenActivated</span> <span class="o">=</span> <span class="n">row</span><span class="o">.</span><span class="na">getTimestamp</span><span class="o">(</span><span class="s">"when_activated"</span><span class="o">)</span> <span class="o">}</span> </pre></div> </div> </div> <div class="code-kt"> <div class="syntax kotlin"><div class="highlight"><pre><span></span><span class="k">val</span> <span class="py">sql</span> <span class="p">=</span> <span class="s">"""</span> <span class="s"> select id, name, when_activated</span> <span class="s"> from customer</span> <span class="s"> where name like ?</span> <span class="s">"""</span><span class="p">.</span><span class="n">trimIndent</span><span class="p">()</span> <span class="k">val</span> <span class="py">rows</span> <span class="p">=</span> <span class="n">DB</span><span class="p">.</span><span class="n">sqlQuery</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> <span class="p">.</span><span class="n">setParameter</span><span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="s">"Rob%"</span><span class="p">)</span> <span class="p">.</span><span class="n">setMaxRows</span><span class="p">(</span><span class="m">100</span><span class="p">)</span> <span class="p">.</span><span class="n">findList</span><span class="p">()</span> <span class="k">for</span> <span class="p">(</span><span class="n">row</span> <span class="k">in</span> <span class="n">rows</span><span class="p">)</span> <span class="p">{</span> <span class="k">val</span> <span class="py">id</span> <span class="p">=</span> <span class="n">row</span><span class="p">.</span><span class="n">getLong</span><span class="p">(</span><span class="s">"id"</span><span class="p">)</span> <span class="k">val</span> <span class="py">name</span> <span class="p">=</span> <span class="n">row</span><span class="p">.</span><span class="n">getString</span><span class="p">(</span><span class="s">"name"</span><span class="p">)</span> <span class="k">val</span> <span class="py">whenActivated</span> <span class="p">=</span> <span class="n">row</span><span class="p">.</span><span class="n">getTimestamp</span><span class="p">(</span><span class="s">"when_activated"</span><span class="p">)</span> <span class="p">}</span> </pre></div> </div> </div> <h3>SqlQuery and RowMapper</h3> <p> Alternatively we can use a RowMapper to read from the JDBC <em>ResultSet</em>. </p> <ul class="code nav nav-tabs mytabs"> <li role="presentation" class="javaActive active"><a onclick="setLang('java');">java</a></li> <li role="presentation" class="kotlinActive"><a onclick="setLang('kt');">kotlin</a></li> </ul> <div class="code-java"> <div class="syntax java"><div class="highlight"><pre><span></span><span class="n">String</span> <span class="n">sql</span> <span class="o">=</span> <span class="s">"select id, name, status from customer order by name desc"</span><span class="o">;</span> <span class="n">DB</span><span class="o">.</span><span class="na">sqlQuery</span><span class="o">(</span><span class="n">sql</span><span class="o">)</span> <span class="o">.</span><span class="na">findEachRow</span><span class="o">((</span><span class="n">resultSet</span><span class="o">,</span> <span class="n">rowNum</span><span class="o">)</span> <span class="o">-></span> <span class="o">{</span> <span class="c1">// read directly from ResultSet</span> <span class="kt">long</span> <span class="n">id</span> <span class="o">=</span> <span class="n">resultSet</span><span class="o">.</span><span class="na">getLong</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span> <span class="n">String</span> <span class="n">name</span> <span class="o">=</span> <span class="n">resultSet</span><span class="o">.</span><span class="na">getString</span><span class="o">(</span><span class="mi">2</span><span class="o">);</span> <span class="c1">// do something interesting with the data</span> <span class="o">});</span> </pre></div> </div> </div> <div class="code-kt"> <div class="syntax kotlin"><div class="highlight"><pre><span></span><span class="k">val</span> <span class="py">sql</span> <span class="p">=</span> <span class="s">"select id, name, status from customer order by name desc"</span> <span class="n">DB</span><span class="p">.</span><span class="n">sqlQuery</span><span class="p">(</span><span class="n">sql</span><span class="p">)</span> <span class="p">.</span><span class="n">findEachRow</span> <span class="p">{</span> <span class="n">resultSet</span><span class="p">,</span> <span class="n">rowNum</span> <span class="p">-></span> <span class="c1">// read directly from ResultSet</span> <span class="k">val</span> <span class="py">id</span> <span class="p">=</span> <span class="n">resultSet</span><span class="p">.</span><span class="n">getLong</span><span class="p">(</span><span class="m">1</span><span class="p">)</span> <span class="k">val</span> <span class="py">name</span> <span class="p">=</span> <span class="n">resultSet</span><span class="p">.</span><span class="n">getString</span><span class="p">(</span><span class="m">2</span><span class="p">)</span> <span class="c1">// do something interesting with the data</span> <span class="p">}</span> </pre></div> </div> </div> <nav class="next"> <p class="edit-page"> <a href="https://github.com/ebean-orm/website-source/blob/master/docs/intro/queries/sql-query.html"><i class="fab fa-github"></i> Edit Page</a> </p> <p class="next"> <a href="/docs/intro/queries/jdbc-query" class="btn btn-info">Next: JDBC Query</a> </p> </nav> </article> </div> </div> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="/js/site3.js"></script> <script src="/js/search3.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-75181644-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'UA-75181644-1'); </script> </body> </html>