2005/5/10

     
 

ACDK SQL Basic Operations

artefaktur

| Installation | Types | Basic Operations | Prepared Statements | Stored Procedures | Database information |

Basic JDBC-like Operations supported.



Content of this chapter:

     Using standard SQL in ACDK
       Open a connection
       Select statement
       Insert Statement
       Create Table
       Drop Table
       Cleanup


 Using standard SQL in ACDK


ACDK SQL classes are quite similar to the JDBC (Java Database Connection) standard.

 Open a connection


  // This Statement is currently needed to load the ODBC-DLL.
  // If no Class from acdk::sql::odbc is used, Class::forName will fail!
  // So let's start with a dummy line ...
  RDriver tdriver = (RDriver)Class::forName("acdk::sql::odbc::ODBCDriver")->newInstance();
  RString url = "jdbc:odbc:acdk/user=acdk/password=acdk";
  RDriver driver = DriverManager::getDriver(url);
  if (driver == Nil)
    testAssertComment(false, "Cannot load driver: " + url);
  
  // get a connection
  RConnection connection = driver->connect(url, Nil);

 Select statement


    // create a statment from the connection
    RStatement statement = connection->createStatement();
    RString sql = "SELECT intcol, realcol, stringcol FROM acdk_select_test"; 
    RResultSet rset = statement->executeQuery(sql);
    while (rset->next() == true) 
    { 
      // read a column via position (starts with 1, not 0)
      int intcol = rset->getInt(1);
      // read a column via name
      float floatcol = rset->getFloat("realcol");
      RString sval = rset->getString("stringcol");
    }
  }

 Insert Statement


{
    // insert some data into the table
    for (int i = 0; i < 2; i++)
    {
      float f = i + 0.5;
      RString sql = SBSTR("INSERT INTO acdk_select_test VALUES ( " << i << ", " << f << ", " << "'stm: " << i << "')"
                          );
      RStatement statement = connection->createStatement();
      int rows = statement->executeUpdate(sql);
    }
  }

 Create Table


    // create a table
    RString sql = "CREATE TABLE acdk_select_test ( intcol int, realcol real, stringcol Varchar(100) )";
    RStatement statement = connection->createStatement();
    statement->executeUpdate(sql);

 Drop Table


    {
    // delete a table, if it already exists
    RStatement statement;
    try {
      RString sql = "DROP TABLE acdk_select_test";
      RStatement statement = connection->createStatement();
      int rows = statement->executeUpdate(sql);
    } catch (RSQLException ex) { 
     
    }

 Cleanup

If the ODBC driver is no longer needed you can call:

  DriverManager::deregisterDriver(tdriver);