View Javadoc

1   /*
2    *  Copyright 2004 University of Hannover
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package olr.samples;
18  
19  import java.util.List;
20  
21  import olr.Olr;
22  import olr.om.RdfUser;
23  import olr.om.RdfUserPeer;
24  
25  /***
26   * This demonstrate how we can use <a href="http://db.apache.org/torque/">Torque's</a>
27   * peer classes to execute custom SQL queries with the advantage
28   * of populating the corresponding Torque data objects in the same step.
29   * Using JDBC direct, one would have to loop over the JDBC ResultSet
30   * calling lots of setters and getters.
31   * @version $Id: SqlSample.java,v 1.4 2004/08/02 18:36:42 roku Exp $
32   */
33  public class SqlSample
34  {
35      public static void main(String[] args)
36      {
37          try
38          {
39              Olr.initTorque();
40              
41              String sql = "SELECT * FROM rdf_user, rdf_group "
42                  + "WHERE rdf_user.group_id=rdf_group.id "
43                  + "AND rdf_group.name='Administrator'";
44              
45              List users = RdfUserPeer.populateObjects(RdfUserPeer.executeQuery(sql));
46              
47              System.out.println("Administrators:\n");
48              
49              for(int i=0; i<users.size(); i++)
50                  System.out.println(((RdfUser)users.get(i)).getFirstName());
51          }
52          catch(Exception e) 
53          {
54              e.printStackTrace();
55          }
56      }
57  }