Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDBCfirst.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RDBCfirst.C
1 // $Id: RDBCfirst.C,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
2 //
3 // This file is part of the RDBC
4 // Author: Valeriy Onuchin <onuchin@sirius.ihep.su>
6 //
7 // This example corresponds to the following JDBC program
8 //
10 //
11 // A sample JDBC program.
12 //
13 // import java.sql.*;
14 //
15 // //****************************************************************************
16 // //* JDBCExample ; *
17 // //****************************************************************************
18 //
19 // public class
20 // JDBCExample
21 // {
22 //
23 // //****************************************************************************
24 // //* main *
25 // //****************************************************************************
26 //
27 // public static void
28 // main( String args[] )
29 // throws Exception
30 // {
31 // // Find the class...
32 // Class.forName( "weblogic.jdbc.oci.Driver" );
33 //
34 // // Open a connection...
35 // Connection myConnection = DriverManager.getConnection(
36 // "jdbc:weblogic:oracle:tcp-loopback.world",
37 // "scott",
38 // "tiger" );
39 //
40 // // Create a statement...
41 // Statement myStatement = myConnection.createStatement();
42 //
43 // // Execute a query...
44 // try
45 // {
46 // // Execute the query...
47 // myStatement.execute( "select * from emp" );
48 //
49 // // Get the result set...
50 // ResultSet mySet = myStatement.getResultSet();
51 //
52 // // Advance to the next row...
53 // while ( mySet.next() )
54 // {
55 // // Get the data...
56 // int empno = mySet.getInt( "empno" );
57 // String ename = mySet.getString( "ename" );
58 // long salary = mySet.getLong( "sal" );
59 //
60 // // Print it all out...
61 // System.out.println( Integer.toString( empno ) + " - " +
62 // ename + " - " + Integer.toString( sal ) );
63 // }
64 // }
65 // catch ( SQLException e )
66 // {
67 // System.out.println( "SQL Error: " + e.toString() );
68 // }
69 //
70 // // Close everything up...
71 // myStatement.close();
72 // myConnection.close();
73 // }
74 //
75 // }
76 //
78 // REQUIREMENTS
79 //
80 // o ORACLE database user SCOTT exists with demo table EMP
82 //
83 // Usage:
84 //
85 // root[] gSystem->Load("libRDBC.so"); // load library
86 // root[] .L RDBCfirst.C++ // compile & load macro
87 // root[] RDBCfirst(dsn,[usr],[pwd]); // execute function from macro
88 //
89 //
90 
91 #include <TError.h>
92 #include <TString.h>
93 #include <RDBC/TSQLDriverManager.h>
94 #include <RDBC/TSQLConnection.h>
96 #include <RDBC/TSQLResultSet.h>
100 #include <RDBC/TSQLTypes.h>
101 
102 //___________________________________________________________________
103 Int_t RDBCfirst(const Text_t* dsn,
104  const Text_t* usr="scott",
105  const Text_t* pwd="tiger")
106 {
107  // Open a connection...
108  TSQLConnection* myConnection = TSQLDriverManager::GetConnection( dsn,
109  usr,
110  pwd );
111  if(!myConnection) return -1; // return on error
112 
113  // Create a statement...
114  TSQLStatement* myStatement = myConnection->CreateStatement();
115  if(!myStatement) return -1; // return on error
116 
117  // Set exception handler
118  TSQL::SetHandler("Catch(TSQLException*)");
119 
120  // Execute the query...
121  Bool_t success = myStatement->Execute( "select * from EMP" );
122  if(!success) return -1; // return on error
123 
124  // Get the result set...
125  TSQLResultSet* mySet = myStatement->GetResultSet();
126  if(!mySet) return -1; // return on error
127 
128  // Advance to the next row...
129  while ( mySet->Next() ) {
130 
131  // Get the data...
132  int empno = mySet->GetInt( "empno" );
133  TString ename = mySet->GetString( "ename" );
134  long salary = mySet->GetLong( "sal" );
135 
136  // Print it all out...
137  printf( "%d - %s - %ld\n",empno,ename.Data(),salary );
138  }
139 
140  // Close everything up...
141  myConnection->Close();
142  return 0;
143 }
144 
145 //___________________________________________________________________
147 {
148  // handle exceptions
149 
150  TString str = e->GetMessage();
151  printf("SQL Error: %s\n",str.Data());
152 }
153 
154 
156 #ifdef STANDALONE
157 
158 #include <TROOT.h>
159 #include <TSystem.h>
160 #include <iostream>
161 
162 //---- Main program ------------------------------------------------------------
163 
164 TROOT root("RDBCfirst", "My first program with RDBC");
165 
166 int main(int argc, char **argv)
167 {
168  if(argc!=3 && argc!=4) {
169  cerr << "Usage: " << argv[0] << " url username" << endl
170  << "or " << argv[0] << " url username password" << endl;
171  return 0;
172  }
173 
174  gSystem->Load("libRDBC");
175  Int_t ret;
176 
177  if(argc==3) ret=RDBCfirst(argv[1],argv[2],"");
178  else ret=RDBCfirst(argv[1],argv[2],argv[3]);
179 
180  return ret;
181 }
182 #endif