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}}
 
 
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.
  
  
Line 12: Line 11:
 
exec sql
 
exec sql
 
select * from example
 
select * from example
where account_no = lcAccountNo
+
where account_no = lcAccountNo
into cursor curExample;
+
into cursor curExample;
return setresultset(“curExample”)
+
return setresultset("curExample")
 
// end of getexamplecursor.prg
 
// end of getexamplecursor.prg
 
</code>
 
</code>
Line 37: Line 36:
 
String s;
 
String s;
  
System.out.println(“Recital resultset example program started.);
+
System.out.println("Recital resultset example program started.");
 
try {
 
try {
 
new RecitalDriver();
 
new RecitalDriver();
String url = “jdbc:Recital:+
+
String url = "jdbc:Recital:" +
“SERVERNAME=?;+
+
"SERVERNAME=?;" +
“DATABASE=southwind;+
+
"DATABASE=southwind;" +
“USERNAME=?;+
+
"USERNAME=?;" +
“PASSWORD=?;
+
"PASSWORD=?";
 
Connection con = DriverManager.getConnection(url);
 
Connection con = DriverManager.getConnection(url);
 
Statement stmt = con.createStatement();
 
Statement stmt = con.createStatement();
  
rs = stmt.executeQuery(“recital getexamplecursor(‘0001’));
+
rs = stmt.executeQuery("recital getexamplecursor(‘0001’)");
 
rsmd = rs.getMetaData();
 
rsmd = rs.getMetaData();
 
int nr_cols = rsmd.getColumnCount();
 
int nr_cols = rsmd.getColumnCount();
Line 54: Line 53:
 
for (i = 1; i <= nr_cols; i++) {
 
for (i = 1; i <= nr_cols; i++) {
 
s = rs.getString(i);
 
s = rs.getString(i);
System.out.println(rsmd.getColumnName(i) +(+ rsmd.getColumnTypeName(i)+) = +s);
+
System.out.println(rsmd.getColumnName(i) +" ("+ rsmd.getColumnTypeName(i)+") = "+s);
 
}
 
}
 
System.out.println("***** Next Record *****");
 
System.out.println("***** Next Record *****");
 
}
 
}
System.out.println(“End of results:);
+
System.out.println("End of results:");
 
stmt.close();
 
stmt.close();
 
con.close();
 
con.close();
Line 64: Line 63:
 
System.out.flush();
 
System.out.flush();
 
System.err.flush();
 
System.err.flush();
DriverManager.println(“Driver exception: + e.getMessage());
+
DriverManager.println("Driver exception: " + e.getMessage());
 
e.printStackTrace();
 
e.printStackTrace();
 
}
 
}
 
try {
 
try {
System.out.println(“Press any key to continue...);
+
System.out.println("Press any key to continue...");
 
System.in.read();
 
System.in.read();
 
} catch(IOException ie) {
 
} catch(IOException ie) {
Line 76: Line 75:
 
}
 
}
 
</code>
 
</code>
 +
 +
[[Category:Documentation]]
 +
[[Category:Reference]]
 +
[[Category:JDBC]]

Revision as of 10:02, 26 March 2009

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) {
			;
		}
	}
}