mysql jdbc 官方编程示例
时间:2019-03-15
本文章向大家介绍mysql jdbc 官方编程示例,主要包括mysql jdbc 官方编程示例使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
/* Basic example of an application using JDBC API of Connector/C++ */ /* Standard C++ includes */ #include <stdlib.h> #include <iostream> #include <sstream> #include <stdexcept> /* Note: Boost must be in the include path to build code which uses the JDBC API. */ #include <boost/scoped_ptr.hpp> #include <jdbc/mysql_connection.h> #include <jdbc/mysql_driver.h> #include <jdbc/cppconn/resultset.h> #include <jdbc/cppconn/statement.h> #define DEFAULT_URI "tcp://127.0.0.1" #define EXAMPLE_USER "root" #define EXAMPLE_PASS "" #define EXAMPLE_DB "test" using namespace std; /* Usage example for Driver, Connection, (simple) Statement, ResultSet */ int main(int argc, const char **argv) { const char *url = (argc > 1 ? argv[1] : DEFAULT_URI); const string user(argc >= 3 ? argv[2] : EXAMPLE_USER); const string pass(argc >= 4 ? argv[3] : EXAMPLE_PASS); const string database(argc >= 5 ? argv[4] : EXAMPLE_DB); cout << endl; cout << "Connector/C++ standalone program example..." << endl; cout << endl; try { sql::Driver * driver = sql::mysql::get_driver_instance(); /* Using the Driver to create a connection */ cout << "Creating session on " << url << " ..." << endl << endl; boost::scoped_ptr< sql::Connection > con(driver->connect(url, user, pass)); con->setSchema(database); boost::scoped_ptr< sql::Statement > stmt(con->createStatement()); boost::scoped_ptr< sql::ResultSet > res(stmt->executeQuery("SELECT 'Welcome to Connector/C++' AS _message")); cout << "\t... running 'SELECT 'Welcome to Connector/C++' AS _message'" << endl; while (res->next()) { cout << "\t... MySQL replies: " << res->getString("_message") << endl; cout << "\t... say it again, MySQL" << endl; cout << "\t....MySQL replies: " << res->getString(1) << endl; } } catch (sql::SQLException &e) { /* The JDBC API throws three different exceptions: - sql::MethodNotImplementedException (derived from sql::SQLException) - sql::InvalidArgumentException (derived from sql::SQLException) - sql::SQLException (derived from std::runtime_error) */ cout << "# ERR: SQLException in " << __FILE__; cout << "(" << "EXAMPLE_FUNCTION" << ") on line " << __LINE__ << endl; /* Use what() (derived from std::runtime_error) to fetch the error message */ cout << "# ERR: " << e.what(); cout << " (MySQL error code: " << e.getErrorCode(); cout << ", SQLState: " << e.getSQLState() << " )" << endl; return EXIT_FAILURE; } cout << endl; cout << "... find more at http://www.mysql.com" << endl; cout << endl; return EXIT_SUCCESS; }
- MySQL 教程
- MySQL 安装
- MySQL 管理与配置
- MySQL PHP 语法
- MySQL 连接
- MySQL 创建数据库
- MySQL 删除数据库
- MySQL 选择数据库
- MySQL 数据类型
- MySQL 创建数据表
- MySQL 删除数据表
- MySQL 插入数据
- MySQL 查询数据
- MySQL where 子句
- MySQL UPDATE 查询
- MySQL DELETE 语句
- MySQL LIKE 子句
- mysql order by
- Mysql Join的使用
- MySQL NULL 值处理
- MySQL 正则表达式
- MySQL 事务
- MySQL ALTER命令
- MySQL 索引
- MySQL 临时表
- MySQL 复制表
- 查看MySQL 元数据
- MySQL 序列 AUTO_INCREMENT
- MySQL 处理重复数据
- MySQL 及 SQL 注入
- MySQL 导出数据
- MySQL 导入数据
- MYSQL 函数大全
- MySQL Group By 实例讲解
- MySQL Max()函数实例讲解
- mysql count函数实例
- MYSQL UNION和UNION ALL实例
- MySQL IN 用法
- MySQL between and 实例讲解
- Python | 时间戳转换
- Stata | 2020年国家社科基金立项名单分析
- 还在用Guava Cache?它才是Java本地缓存之王
- 死信队列的消息处理方案
- 【Ceph】Rook 中使用 External Cluster 的方式管理外部集群
- Redis排行榜的设计与实现
- 血的教训 | 一次订单号重复的事故差点被开除
- 60分钟看懂HMM的基本原理
- R语言确实会蛮耗费磁盘空间哦
- 很多时候你就是不知道如何提问
- seurat标准流程实例之2个10x样本的项目(GSE135927数据集)
- 使用 Jenkins 和 Ansible 实现 CI/CD
- 太刺激了,面试官让我手写跳表,而我用两种实现方式吊打了TA!
- 被 Google 选择的下一代数据面 Cilium 是什么 - 上手实践
- K8S 生态周报| 是时候从 k8s v1.16 升级了