Changes

Jump to navigation Jump to search
1,846 bytes added ,  15:08, 6 December 2010
Created page with "<p>In the query mode a query can be send to the server and executed in an iterative manner. For this you have to call the <code>query()</code> function of the <code>Session</code..."
<p>In the query mode a query can be send to the server and executed
in an iterative manner. For this you have to call the <code>query()</code> function
of the <code>Session</code> with your defined query. This will return a query object
which comes with an forward-only iterator to get each result of the query.
Furthermore it is possible to bind variables to the query using the <code>bind()</code> function
of the query object.</p>

<h2>Usage</h2>

<p>The query execution works as follows:</p>

<ol start="1">
<li>Create a new session instance with hostname, port, username and password.</li>
<li>Call the <code>query()</code> function of the session with the query as argument to get your query object.</li>
<li>Optionally bind variables to the query with the <code>bind()</code> function.</li>
<li>Initialize query output via <code>init()</code>.</li>
<li>Iterate through the query object with the <code>more()</code> and <code>next()</code> functions.
If an error occurs, an exception is thrown.</li>
<li>Close the query with <code>close()</code>.</li>
</ol>

<h2>Example</h2>

<ol start="1">
<li>Create a session object: <code>Session session = new Session("localhost", 1984, "admin", "admin");</code></li>
<li>Define a query and create a query object:<br/>
<code>String q = "declare variable $name external; " +
"for $i in 1 to 10 return element { $name } { $i }";
Query query = session.query(q);</code></li>
<li>Call the bind method of your query object:<br/>
<code>query.bind("$name", "Number");</code></li>
<li>Call the init method of your query object:<br/>
<code>print query.init();</code></li>
<li>Iterate through the query object:<br/>
<code>while(query.more()) print query.next();</code></li>
<li>Close your query object:<br/>
<code>print query.close();</code></li>
</ol>
bueraucrat, Bureaucrats, editor, reviewer, Administrators
907

edits

Navigation menu