判断是否与数据库是否有相同项

实例如下:

/**
	 * 根据设备类型ID以及设备类型编码来判断是否有与之同编码的其他设备类型
	 */
	@Override
	public boolean hasSameDCatCode(String DCatCode,String DCatId) throws DAOException {
		boolean flag =false;
		List<TIbmsInfoDeviceCategory> lists=new ArrayList<TIbmsInfoDeviceCategory>();
		StringBuffer hql=new StringBuffer(" from TIbmsInfoDeviceCategory t where t.DCatCode= ?");
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(DCatName);
		lists=DevCatDao.find(hql.toString(),paramList.toArray());
		if(lists.size()>0){
			//再for循环判断是否是自己,不跟自己比
			for(TIbmsInfoDeviceCategory list:lists){
				if(list.getDCatId().equals(DCatId)){
					flag=false;
				}else{
					flag=true;
				}
			}
		}
		return flag;
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 C++ 的 MySQL Connector/C++ 库来连接 MySQL 数据库。具体实现步骤如下: 1. 下载和安装 MySQL Connector/C++ 库。 2. 在 C++ 代码中引入 MySQL Connector/C++ 库的头文件。 ```cpp #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/statement.h> ``` 3. 创建数据库连接。 ```cpp sql::Driver *driver; sql::Connection *con; driver = get_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "root", "password"); con->setSchema("your_database_name"); ``` 4. 执行 SQL 查询语句。 ```cpp sql::Statement *stmt; sql::ResultSet *res; stmt = con->createStatement(); res = stmt->executeQuery("SELECT * FROM your_table_name"); ``` 5. 解析查询结果。 ```cpp while (res->next()) { int id = res->getInt("id"); std::string name = res->getString("name"); // ... } ``` 6. 执行 SQL 插入语句。 ```cpp std::string name = "your_data_name"; int id = 1; sql::PreparedStatement *pstmt; pstmt = con->prepareStatement("INSERT INTO your_table_name (id, name) VALUES (?, ?)"); pstmt->setInt(1, id); pstmt->setString(2, name); pstmt->execute(); ``` 7. 关闭数据库连接。 ```cpp delete res; delete stmt; delete con; ``` 对于你的需求,你可以先执行一个查询语句,判断是否存在相同,如果存在,则获取该的编号加一后插入数据库。如果不存在,则直接插入新数据。具体实现逻辑可以参考以下代码: ```cpp int get_next_id(sql::Connection *con, std::string name) { sql::Statement *stmt; sql::ResultSet *res; stmt = con->createStatement(); std::string query = "SELECT MAX(id) AS max_id FROM your_table_name WHERE name = '" + name + "'"; res = stmt->executeQuery(query); int max_id = 0; if (res->next()) { max_id = res->getInt("max_id"); } delete res; delete stmt; return max_id + 1; } void insert_data(sql::Connection *con, std::string name) { int id = get_next_id(con, name); sql::PreparedStatement *pstmt; pstmt = con->prepareStatement("INSERT INTO your_table_name (id, name) VALUES (?, ?)"); pstmt->setInt(1, id); pstmt->setString(2, name); pstmt->execute(); delete pstmt; } int main() { sql::Driver *driver; sql::Connection *con; driver = get_driver_instance(); con = driver->connect("tcp://127.0.0.1:3306", "root", "password"); con->setSchema("your_database_name"); std::string name = "your_data_name"; insert_data(con, name); delete con; return 0; } ``` 注意,这里的代码仅供参考,你需要根据实际情况进行调整。同时,为了保证安全性,建议使用预处理语句来防止 SQL 注入攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值