赞
踩
#include <mysql/jdbc.h>
try { //初始化接口并连接 sql::mysql::MySQL_Driver* driver; sql::Connection* con; sql::Statement* stat; driver = sql::mysql::get_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); con->setSchema("schema");//数据库名称 //查询 stat = con->createStatement(); /// 准备 SQL 查询语句,并执行 sql::Statement* stmt = con->createStatement(); //PreparedStatement* pstmt = conn->prepareStatement("SELECT * FROM temperature ORDER BY time DESC LIMIT 1"); sql::ResultSet* res = stmt->executeQuery("SELECT * FROM temperature ORDER BY time DESC LIMIT 1"); // 遍历结果集,并输出查询结果 while (res->next()) { double temperature = res->getDouble("temperature"); string time = res->getString("time"); cout << "temperature: " << temperature << ", time: " << time << endl; } // 释放资源 delete res; delete stmt; delete con; } //异常抛出 catch (sql::SQLException& e) { cout << "Error: " << e.what() << endl; }
相关链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。