Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:22:02

0001 // $Id: RDBCfirst.C,v 1.1.1.1 2004/02/18 20:58:02 dave Exp $
0002 //
0003 //     This file is part of the RDBC
0004 //     Author: Valeriy Onuchin <onuchin@sirius.ihep.su>
0005 /////////////////////////////////////////////////////////////////////
0006 //
0007 // This example corresponds to the following JDBC program
0008 //
0009 ////////////////////////////////////////////////////////////////////////////////////
0010 //
0011 // A sample JDBC program.
0012 //
0013 // import                            java.sql.*; 
0014 //
0015 // //**************************************************************************** 
0016 // //* JDBCExample                                          ;                     * 
0017 // //**************************************************************************** 
0018 //
0019 //    public class
0020 //    JDBCExample
0021 //    {
0022 //
0023 //    //**************************************************************************** 
0024 //    //* main                                                                      * 
0025 //    //**************************************************************************** 
0026 //
0027 //    public static void
0028 //    main( String args[] )
0029 //    throws Exception
0030 //    {
0031 //       //    Find the class...
0032 //       Class.forName( "weblogic.jdbc.oci.Driver" );
0033 //
0034 //       //    Open a connection...
0035 //       Connection myConnection = DriverManager.getConnection(
0036 //             "jdbc:weblogic:oracle:tcp-loopback.world", 
0037 //             "scott", 
0038 //             "tiger" );
0039 //
0040 //       //    Create a statement...
0041 //       Statement myStatement = myConnection.createStatement();
0042 //
0043 //       //    Execute a query...
0044 //       try
0045 //       {
0046 //          //    Execute the query...
0047 //          myStatement.execute( "select * from emp" );
0048 //
0049 //          //    Get the result set...
0050 //          ResultSet mySet = myStatement.getResultSet();
0051 //
0052 //          //    Advance to the next row...
0053 //          while ( mySet.next() )
0054 //          { 
0055 //             //    Get the data...
0056 //             int empno = mySet.getInt( "empno" );
0057 //             String ename = mySet.getString( "ename" );
0058 //             long salary = mySet.getLong( "sal" );
0059 //
0060 //             //    Print it all out...
0061 //             System.out.println( Integer.toString( empno ) + " - " +
0062 //                   ename + " - " + Integer.toString( sal ) );
0063 //          } 
0064 //       }
0065 //       catch ( SQLException e )
0066 //       {
0067 //          System.out.println( "SQL Error: " + e.toString() );
0068 //       }
0069 //
0070 //       //    Close everything up...
0071 //       myStatement.close(); 
0072 //       myConnection.close(); 
0073 //    }
0074 //
0075 // } 
0076 //
0077 ////////////////////////////////////////////////////////////////////////////////
0078 //    REQUIREMENTS
0079 //
0080 //      o ORACLE database user SCOTT exists with demo table EMP
0081 ////////////////////////////////////////////////////////////////////////////////////
0082 //
0083 // Usage:
0084 //  
0085 // root[] gSystem->Load("libRDBC.so");    // load library
0086 // root[] .L RDBCfirst.C++                // compile & load macro
0087 // root[] RDBCfirst(dsn,[usr],[pwd]);     // execute function from macro
0088 //
0089 //
0090 
0091 #include <TError.h>
0092 #include <TString.h>
0093 #include <RDBC/TSQLDriverManager.h>
0094 #include <RDBC/TSQLConnection.h>
0095 #include <RDBC/TSQLDatabaseMetaData.h>
0096 #include <RDBC/TSQLResultSet.h>
0097 #include <RDBC/TSQLResultSetMetaData.h>
0098 #include <RDBC/TSQLPreparedStatement.h>
0099 #include <RDBC/TSQLCallableStatement.h>
0100 #include <RDBC/TSQLTypes.h>
0101 
0102 //___________________________________________________________________
0103 Int_t RDBCfirst(const Text_t* dsn,
0104                const Text_t* usr="scott",
0105                const Text_t* pwd="tiger")
0106 {
0107    //    Open a connection...
0108    TSQLConnection* myConnection = TSQLDriverManager::GetConnection( dsn, 
0109                                                                     usr, 
0110                                                                     pwd );
0111    if(!myConnection) return -1;  // return on error
0112       
0113    //    Create a statement...
0114    TSQLStatement* myStatement = myConnection->CreateStatement();
0115    if(!myStatement) return -1;   // return on error
0116    
0117    //   Set exception handler 
0118    TSQL::SetHandler("Catch(TSQLException*)");
0119    
0120    //    Execute the query...
0121    Bool_t success = myStatement->Execute( "select * from EMP" );
0122    if(!success) return -1; // return on error
0123    
0124    //    Get the result set...
0125    TSQLResultSet* mySet = myStatement->GetResultSet();
0126    if(!mySet) return -1; // return on error
0127 
0128    //    Advance to the next row...
0129    while ( mySet->Next() ) { 
0130                             
0131       //    Get the data...
0132       int empno = mySet->GetInt( "empno" );
0133       TString ename = mySet->GetString( "ename" );
0134       long salary = mySet->GetLong( "sal" );
0135 
0136       //    Print it all out...
0137       printf( "%d - %s - %ld\n",empno,ename.Data(),salary );
0138    } 
0139       
0140    //    Close everything up...
0141    myConnection->Close();
0142    return 0;
0143 }
0144 
0145 //___________________________________________________________________
0146 void Catch(TSQLException* e)
0147 {
0148    // handle exceptions
0149    
0150    TString str = e->GetMessage();
0151    printf("SQL Error: %s\n",str.Data()); 
0152 }
0153 
0154 
0155 //////////////////////////// Main program ////////////////////////////////////
0156 #ifdef STANDALONE
0157 
0158 #include <TROOT.h>
0159 #include <TSystem.h>
0160 #include <iostream>
0161 
0162 //---- Main program ------------------------------------------------------------
0163 
0164 TROOT root("RDBCfirst", "My first program with RDBC");
0165 
0166 int main(int argc, char **argv)
0167 {
0168    if(argc!=3 && argc!=4) {
0169       cerr << "Usage: " << argv[0] << " url username" << endl
0170            << "or     " << argv[0] << " url username password" << endl;
0171       return 0;
0172    }
0173 
0174    gSystem->Load("libRDBC");
0175    Int_t ret;
0176 
0177    if(argc==3) ret=RDBCfirst(argv[1],argv[2],"");
0178    else  ret=RDBCfirst(argv[1],argv[2],argv[3]);
0179 
0180    return ret;
0181 }
0182 #endif