List:Commits« Previous MessageNext Message »
From:anthony.bedford Date:December 8 2008 3:39pm
Subject:svn commit - mysqldoc@docsrva: r12842 - trunk/refman-common
View as plain text  
Author: tbedford
Date: 2008-12-08 15:39:03 +0100 (Mon, 08 Dec 2008)
New Revision: 12842

Log:
Stand-alone example 2 added.

Modified:
   trunk/refman-common/connector-cpp.xml


Modified: trunk/refman-common/connector-cpp.xml
===================================================================
--- trunk/refman-common/connector-cpp.xml	2008-12-08 14:16:52 UTC (rev 12841)
+++ trunk/refman-common/connector-cpp.xml	2008-12-08 14:39:03 UTC (rev 12842)
Changed blocks: 2, Lines Added: 124, Lines Deleted: 14; 5564 bytes

@@ -948,23 +948,23 @@
 
 <programlisting>/* Copyright 2008 Sun Microsystems, Inc.
 
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; version 2 of the License.
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; version 2 of the License.
 
-   There are special exceptions to the terms and conditions of the GPL
-   as it is applied to this software. View the full text of the
-   exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
-   software distribution.
+There are special exceptions to the terms and conditions of the GPL
+as it is applied to this software. View the full text of the
+exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
+software distribution.
 
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */
 
 /* Standard C++ includes */

@@ -1033,6 +1033,116 @@
 
     </section>
 
+    <section id="connector-cpp-examples-complete-example-2">
+
+      <title>MySQL Connector/C++ Complete Example 2</title>
+
+      <para>
+        The following code shows a complete example of how to use MySQL
+        Connector/C++:
+      </para>
+
+<programlisting>/* Copyright 2008 Sun Microsystems, Inc.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; version 2 of the License.
+
+There are special exceptions to the terms and conditions of the GPL
+as it is applied to this software. View the full text of the
+exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this
+software distribution.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+*/
+
+/* Standard C++ includes */
+#include &lt;stdlib.h&gt;
+#include &lt;iostream&gt;
+
+/*
+  Include directly the different
+  headers from cppconn/ and mysql_driver.h + mysql_util.h
+  (and mysql_connection.h). This will reduce your build time!
+*/
+#include "mysql_connection.h"
+
+#include &lt;cppconn/driver.h&gt;
+#include &lt;cppconn/exception.h&gt;
+#include &lt;cppconn/resultset.h&gt;
+#include &lt;cppconn/statement.h&gt;
+#include &lt;cppconn/prepared_statement.h&gt;
+
+using namespace std;
+
+int main(void)
+{
+cout &lt;&lt; endl;
+cout &lt;&lt; "Let's have MySQL count from 10 to 1..." &lt;&lt; endl;
+
+try {
+  sql::Driver *driver;
+  sql::Connection *con;
+  sql::Statement *stmt;
+  sql::ResultSet *res;
+  sql::PreparedStatement *pstmt;
+
+  /* Create a connection */
+  driver = get_driver_instance();
+  con = driver-&gt;connect("tcp://127.0.0.1:3306", "root", "root");
+  /* Connect to the MySQL test database */
+  con-&gt;setSchema("test");
+
+  stmt = con-&gt;createStatement();
+  stmt-&gt;execute("DROP TABLE IF EXISTS test");
+  stmt-&gt;execute("CREATE TABLE test(id INT)");
+  delete stmt;
+
+  /* '?' is the supported placeholder syntax */
+  pstmt = con-&gt;prepareStatement("INSERT INTO test(id) VALUES (?)");
+  for (int i = 1; i &lt;= 10; i++) {
+    pstmt-&gt;setInt(1, i);
+    pstmt-&gt;executeUpdate();
+  }
+  delete pstmt;
+
+  /* Select in ascending order */
+  pstmt = con-&gt;prepareStatement("SELECT id FROM test ORDER BY id ASC");
+  res = pstmt-&gt;executeQuery();
+
+  /* Fetch in reverse = descending order! */
+  res-&gt;afterLast();
+  while (res-&gt;previous())
+    cout &lt;&lt; "\t... MySQL counts: " &lt;&lt;
res-&gt;getInt("id") &lt;&lt; endl;
+  delete res;
+
+  delete pstmt;
+  delete con;
+
+} catch (sql::SQLException &amp;e) {
+  cout &lt;&lt; "# ERR: SQLException in " &lt;&lt; __FILE__;
+  cout &lt;&lt; "(" &lt;&lt; __FUNCTION__ &lt;&lt; ") on line "
&raquo;
+     &lt;&lt; __LINE__ &lt;&lt; endl;
+  cout &lt;&lt; "# ERR: " &lt;&lt; e.what();
+  cout &lt;&lt; " (MySQL error code: " &lt;&lt; e.getErrorCode();
+  cout &lt;&lt; ", SQLState: " &lt;&lt; e.getSQLState() &lt;&lt;
&raquo;
+     " )" &lt;&lt; endl;
+}
+
+cout &lt;&lt; endl;
+
+return EXIT_SUCCESS;
+}</programlisting>
+
+    </section>
+
   </section>
 
   <section id="connector-cpp-debug-tracing">


Thread
svn commit - mysqldoc@docsrva: r12842 - trunk/refman-commonanthony.bedford8 Dec