Difference between revisions of "JDBC: Accessing a Resultset from a Stored Procedure"

From Recital Documentation Wiki
Jump to: navigation, search
 
Line 1: Line 1:
 
{{YLM to tidy up}}
 
{{YLM to tidy up}}
Accessing a Resultset from a Stored Procedure
 
 
 
Stored procedures and user-defined functions are collections of SQL statements and optional control-of-flow statements written in the Recital 4GL (compatible with VFP) stored under a name and saved in a Database.  Both stored procedures and user-defined functions are just-in-time compiled by the Recital database engine.
 
Stored procedures and user-defined functions are collections of SQL statements and optional control-of-flow statements written in the Recital 4GL (compatible with VFP) stored under a name and saved in a Database.  Both stored procedures and user-defined functions are just-in-time compiled by the Recital database engine.
  
Stored Procedures can return a Resultset using the 4GL SETRESULTSET() function.  The ‘recital’ command is used to execute the 4GL Stored Procedure and return the Resultset to the client application for processing.  Resultset that are returned from Stored Procedures are read-only.
+
Stored Procedures can return a Resultset using the 4GL [[SETRESULTSET()]] function.  The ‘recital’ command is used to execute the 4GL Stored Procedure and return the Resultset to the client application for processing.  Resultset that are returned from Stored Procedures are read-only.
  
In this example, the getexamplecursor.prg stored procedure is in the ‘southwind’ database on the server.  It accepts a parameter and runs a query, saving the results into a cursor.  This cursor is then returned as a Resultset using the SETRESULTSET() 4GL function.
+
In this example, the getexamplecursor.prg stored procedure is in the ‘southwind’ database on the server.  It accepts a parameter and runs a query, saving the results into a cursor.  This cursor is then returned as a Resultset using the [[SETRESULTSET()]] 4GL function.
  
 +
 +
<code lang="recital">
 
// Stored Procedure getexamplecursor.prg
 
// Stored Procedure getexamplecursor.prg
 
lparameters lcAccountNo
 
lparameters lcAccountNo
Line 16: Line 16:
 
return setresultset(“curExample”)
 
return setresultset(“curExample”)
 
// end of getexamplecursor.prg
 
// end of getexamplecursor.prg
 +
</code>
 +
  
 
The Java program establishes the JDBC connection, calls the Stored Procedure and displays the data.
 
The Java program establishes the JDBC connection, calls the Stored Procedure and displays the data.
  
  
 +
<code lang="java">
 
import java.sql.*;
 
import java.sql.*;
 
import java.io.*;
 
import java.io.*;
Line 72: Line 75:
 
}
 
}
 
}
 
}
 +
</code>

Revision as of 09:48, 26 March 2009

Template:YLM to tidy up Stored procedures and user-defined functions are collections of SQL statements and optional control-of-flow statements written in the Recital 4GL (compatible with VFP) stored under a name and saved in a Database. Both stored procedures and user-defined functions are just-in-time compiled by the Recital database engine.

Stored Procedures can return a Resultset using the 4GL SETRESULTSET() function. The ‘recital’ command is used to execute the 4GL Stored Procedure and return the Resultset to the client application for processing. Resultset that are returned from Stored Procedures are read-only.

In this example, the getexamplecursor.prg stored procedure is in the ‘southwind’ database on the server. It accepts a parameter and runs a query, saving the results into a cursor. This cursor is then returned as a Resultset using the SETRESULTSET() 4GL function.


// Stored Procedure getexamplecursor.prg
lparameters lcAccountNo
exec sql
select * from example
where account_no = lcAccountNo
into cursor curExample;
return setresultset(“curExample”)
// end of getexamplecursor.prg


The Java program establishes the JDBC connection, calls the Stored Procedure and displays the data.


import java.sql.*;
import java.io.*;
import java.net.URL;
import Recital.sql.*;
 
public class rs_example {
 
public static void main(String[] args) {
 
int	i;
ResultSet rs;
ResultSetMetaData rsmd;
		String s;
 
System.out.println(“Recital resultset example program started.”);
try {
new RecitalDriver();
String url = “jdbc:Recital:+
“SERVERNAME=?;+
“DATABASE=southwind;+
“USERNAME=?;+
“PASSWORD=?;
Connection con = DriverManager.getConnection(url);
Statement stmt = con.createStatement();
 
rs = stmt.executeQuery(“recital getexamplecursor(‘0001’));
rsmd = rs.getMetaData();
int nr_cols = rsmd.getColumnCount();
while (rs.next()) {
for (i = 1; i <= nr_cols; i++) {
					s = rs.getString(i);
System.out.println(rsmd.getColumnName(i) +(+ rsmd.getColumnTypeName(i)+) =+s);
}
System.out.println("***** Next Record *****");
}
System.out.println(“End of results:);
stmt.close();
con.close();
} catch (Exception e) {
System.out.flush();
System.err.flush();
DriverManager.println(Driver exception:+ e.getMessage());
e.printStackTrace();
}
try {
System.out.println(“Press any key to continue...”);
System.in.read();
} catch(IOException ie) {
;
}
}
}