首页 \ 问答 \ 如何验证数据库中是否存在记录?(How to verify existence of a record in database?)

如何验证数据库中是否存在记录?(How to verify existence of a record in database?)

我需要验证数据库中是否存在记录,但无论我传递给以下代码的ID如何,它都不会显示任何内容。 记录是否存在。

       session = HibernateUtil.getSession();
        session.beginTransaction();
        Query q = session.createQuery("select count(*) from Users where id = :id");
        q.setInteger("id", id);
        Integer count = (Integer)q.uniqueResult();
        if(count > 0) {
            System.err.println(">");
        }
        else {
            System.err.println("<");
        }
        session.getTransaction().commit();
        session.close();

我也使用了以下代码,但仍然有同样的问题

  try{
        session = HibernateUtil.getSession();
        session.beginTransaction();
        Users users = null;
        users = (Users) session.load(Users.class, id);
    }catch(Exception e){
              System.err.println("<");
      }

I need to verify the existence of a record in database, but regardless of the id that I pass to the following code, it does not show anything. either the record exists or not.

       session = HibernateUtil.getSession();
        session.beginTransaction();
        Query q = session.createQuery("select count(*) from Users where id = :id");
        q.setInteger("id", id);
        Integer count = (Integer)q.uniqueResult();
        if(count > 0) {
            System.err.println(">");
        }
        else {
            System.err.println("<");
        }
        session.getTransaction().commit();
        session.close();

I've used the following code as well but still have the same problem

  try{
        session = HibernateUtil.getSession();
        session.beginTransaction();
        Users users = null;
        users = (Users) session.load(Users.class, id);
    }catch(Exception e){
              System.err.println("<");
      }

原文:https://stackoverflow.com/questions/17825045
更新时间:2024-05-24 19:05

最满意答案

可以从MySQL中的information_schema.columns表查询特定表的列列表。

但是,在这种情况下,您需要使用预准备语句创建insert语句,因为这允许您将sql命令创建为字符串,然后您可以执行它。

...
declare s_fields, s_sql varchar(1000); --length should depend on how many fields you have
select group_concat(column_name) into s_fields
  from information_schema.columns
  where table_name='yourtable'
  group by table_name
  limit 1;
set s_sql=concat('insert into tablename (',s_fields, ') values (', variable_holding_comma_separated_list_of_values,')');
prepare stmt from s_sql;
execute stmt;
deallocate prepare stmt;
...

It is possible to query the list of columns of a particular table from information_schema.columns table within MySQL.

However, in this case you need to create your insert statement using prepared statement because this allows you to create the sql command as a string and then you can execute it.

...
declare s_fields, s_sql varchar(1000); --length should depend on how many fields you have
select group_concat(column_name) into s_fields
  from information_schema.columns
  where table_name='yourtable'
  group by table_name
  limit 1;
set s_sql=concat('insert into tablename (',s_fields, ') values (', variable_holding_comma_separated_list_of_values,')');
prepare stmt from s_sql;
execute stmt;
deallocate prepare stmt;
...

相关问答

更多

相关文章

更多

最新问答

更多
  • 谁有JAVA的视频教程啊 最好从基础开始到精通 打包好的
  • 仅根据表格的一列删除重复的值(Remove duplicate values based on only one column of the table)
  • 从数据库值填充数组的最有效方法?(Most efficient way to populate array from database values?)
  • 我在data属性中有一个变量,并从prop传递相同的变量。(I have one variable in data attribute, and pass the same variable from prop. why the {{}} template display the value from prop rather than data)
  • Reg Ex Django Url Conf(Reg Ex Django Url Conf)
  • 本地化不适用于主要活动(Localization does not work on main activity)
  • 我有一个同学让我去福州科闽计算机学校学习室内设计,这个学校好吗?
  • 变量的模板参数中的Decltype(Decltype in template parameter of variable)
  • 代码签名错误与Xcode 4.2(Code Sign error with Xcode 4.2)
  • 在保留表达式生命周期的同时强制执行参数评估顺序(Enforce parameter evaluation order while retaining lifetime of of expressions)
  • 查询以获取早于开始日期的日期行(不是简单的WHERE)(Query to get date rows older than a start date (not a simple WHERE))
  • 武清哪家会计培训机构通过率高?
  • Java正则表达式逻辑OR(Java regex logical OR)
  • 使用for循环创建异步Vue组件(Use for loop to create async Vue component)
  • 从magento数据库获取客户名称和电子邮件(Get customer names and emails from magento database)
  • 运行Ruby冒泡排序(Running Through a Ruby Bubble Sort)
  • 减去返回多于1行的两个SQL子查询的结果(Subtracting the results of the two SQL subqueries that returns more than 1 row)
  • 对于Ruby http请求,响应“呃?”是间歇性的(The response “er ?” is getting intermittently for Ruby http request)
  • 请问嘉定区有没有培训上海市电脑中级的,有的话在哪里?
  • 根据日期范围查询最小分区键(聚类键)(Query min partition key based on date range (clustering key))
  • int listinsert(sqlist * & L)中的&是什么意思
  • 原始套接字的会话管理(Session Management on Raw Socket)
  • 将Type用作“属性”是不好的做法?(Is it bad practise to use Type as a 'Property'?)
  • 电脑等级证书二级和三级有什么区别啊 !!我们学校有二级和三级培训到底该选着那个呢????????
  • linux 安装新硬盘如何设置权限
  • 无法从RecyclerView的onBindViewHolder更改根视图的大小或位置(Cannot change root view's size or position from RecyclerView's onBindViewHolder)
  • spin.js没出现在我的网站上?(spin.js is not showing up on my site?)
  • Gulp + Bootstrap字体(Gulp + Bootstrap Fonts)
  • 如何使用短信Alfresco.util.PopupManager.displayPrompt显示图标(How to show icon with text message Alfresco.util.PopupManager.displayPrompt)
  • Skype.getAllChats()未检测到基于云的群组聊天(Skype.getAllChats() not detecting cloud-based group chats)