KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > samples > FunctionMultiReturn


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.samples;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.DriverManager JavaDoc;
9 import java.sql.PreparedStatement JavaDoc;
10 import java.sql.ResultSet JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.sql.Statement JavaDoc;
13 import java.sql.Types JavaDoc;
14
15 import org.h2.tools.SimpleResultSet;
16
17 public class FunctionMultiReturn {
18     
19     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
20         Class.forName("org.h2.Driver");
21         Connection JavaDoc conn = DriverManager.getConnection("jdbc:h2:mem:", "sa", "");
22         Statement JavaDoc stat = conn.createStatement();
23         stat.execute("CREATE ALIAS P2C FOR \"org.h2.samples.FunctionMultiReturn.polar2Cartesian\" ");
24         PreparedStatement JavaDoc prep = conn.prepareStatement("SELECT X, Y FROM P2C(?, ?)");
25         prep.setDouble(1, 5.0);
26         prep.setDouble(2, 0.5);
27         ResultSet JavaDoc rs = prep.executeQuery();
28         while(rs.next()) {
29             double x = rs.getDouble(1);
30             double y = rs.getDouble(2);
31             System.out.println("result: (x=" + x + ", y="+y+")");
32         }
33         
34         stat.execute("CREATE TABLE TEST(ID IDENTITY, R DOUBLE, A DOUBLE)");
35         stat.execute("INSERT INTO TEST(R, A) VALUES(5.0, 0.5), (10.0, 0.6)");
36         stat.execute("CREATE ALIAS P2C_SET FOR \"org.h2.samples.FunctionMultiReturn.polar2CartesianSet\" ");
37         rs = conn.createStatement().executeQuery("SELECT * FROM P2C_SET('SELECT * FROM TEST')");
38         while(rs.next()) {
39             double r = rs.getDouble("R");
40             double a = rs.getDouble("A");
41             double x = rs.getDouble("X");
42             double y = rs.getDouble("Y");
43             System.out.println("(r="+r+" a="+a+") : (x=" + x + ", y="+y+")");
44         }
45         
46         stat.execute("CREATE ALIAS P2C_A FOR \"org.h2.samples.FunctionMultiReturn.polar2CartesianArray\" ");
47         rs = conn.createStatement().executeQuery("SELECT R, A, P2C_A(R, A) FROM TEST");
48         while(rs.next()) {
49             double r = rs.getDouble(1);
50             double a = rs.getDouble(2);
51             Object JavaDoc o = rs.getObject(3);
52             Object JavaDoc[] xy = (Object JavaDoc[]) o;
53             double x = ((Double JavaDoc)xy[0]).doubleValue();
54             double y = ((Double JavaDoc)xy[1]).doubleValue();
55             System.out.println("(r="+r+" a="+a+") : (x=" + x + ", y="+y+")");
56         }
57         
58         rs = conn.createStatement().executeQuery("SELECT R, A, ARRAY_GET(E, 1), ARRAY_GET(E, 2) FROM (SELECT R, A, P2C_A(R, A) E FROM TEST)");
59         while(rs.next()) {
60             double r = rs.getDouble(1);
61             double a = rs.getDouble(2);
62             double x = rs.getDouble(3);
63             double y = rs.getDouble(4);
64             System.out.println("(r="+r+" a="+a+") : (x=" + x + ", y="+y+")");
65         }
66         
67         conn.close();
68     }
69     
70     /**
71      * The function may be called twice, once to retrieve the result columns (with null parameters),
72      * and the second time to return the data.
73      */

74     public static ResultSet JavaDoc polar2Cartesian(Double JavaDoc r, Double JavaDoc alpha) throws SQLException JavaDoc {
75         SimpleResultSet rs = new SimpleResultSet();
76         rs.addColumn("X", Types.DOUBLE, 0, 0);
77         rs.addColumn("Y", Types.DOUBLE, 0, 0);
78         if(r != null && alpha != null) {
79             double x = r.doubleValue() * Math.cos(alpha.doubleValue());
80             double y = r.doubleValue() * Math.sin(alpha.doubleValue());
81             rs.addRow(new Object JavaDoc[] { new Double JavaDoc(x), new Double JavaDoc(y) });
82         }
83         return rs;
84     }
85     
86     /**
87      * The function may be called twice, once to retrieve the result columns (with null parameters),
88      * and the second time to return the data.
89      */

90     public static Object JavaDoc[] polar2CartesianArray(Double JavaDoc r, Double JavaDoc alpha) throws SQLException JavaDoc {
91         double x = r.doubleValue() * Math.cos(alpha.doubleValue());
92         double y = r.doubleValue() * Math.sin(alpha.doubleValue());
93         return new Object JavaDoc[]{new Double JavaDoc(x), new Double JavaDoc(y)};
94     }
95
96     public static ResultSet JavaDoc polar2CartesianSet(Connection JavaDoc conn, String JavaDoc query) throws SQLException JavaDoc {
97         SimpleResultSet result = new SimpleResultSet();
98         result.addColumn("R", Types.DOUBLE, 0, 0);
99         result.addColumn("A", Types.DOUBLE, 0, 0);
100         result.addColumn("X", Types.DOUBLE, 0, 0);
101         result.addColumn("Y", Types.DOUBLE, 0, 0);
102         if(query != null) {
103             ResultSet JavaDoc rs = conn.createStatement().executeQuery(query);
104             while(rs.next()) {
105                 double r = rs.getDouble("R");
106                 double alpha = rs.getDouble("A");
107                 double x = r * Math.cos(alpha);
108                 double y = r * Math.sin(alpha);
109                 result.addRow(new Object JavaDoc[] { new Double JavaDoc(r), new Double JavaDoc(alpha), new Double JavaDoc(x), new Double JavaDoc(y) });
110             }
111         }
112         return result;
113     }
114
115 }
116
Popular Tags