首页 \ 问答 \ Java ResultSet如何检查是否有任何结果(Java ResultSet how to check if there are any results)

Java ResultSet如何检查是否有任何结果(Java ResultSet how to check if there are any results)

Resultset没有hasNext的方法。 我想检查resultSet是否有任何值

这是正确的方法吗?

if (!resultSet.next() ) {
    System.out.println("no data");
} 

Resultset has no method for hasNext. I want to check if the resultSet has any value

is this the correct way

if (!resultSet.next() ) {
    System.out.println("no data");
} 

原文:https://stackoverflow.com/questions/867194
更新时间:2024-01-09 21:01

最满意答案

如果你喜欢,你可以像使用任何其他线程程序一样同步使用threading.Lock访问共享资源,而不是复制它。

无论如何,我认为在进行优化之前,有必要对代码进行基准测试,然后进行深度复制和其他测量,以确定性能的好坏。 或许它的速度缓慢的原因与深度拷贝无关。

关于使用锁定编辑:我的意思是,你可以使用更多的细粒度锁定此资源。 我假设你的线程不仅仅是访问共享资源。 您可以尝试从多个正在工作的线程中受益,然后将访问同步到只涉及写入共享资源的“关键部分”。 您也可以调查使您的共享资源线程安全。 例如,如果有一个共享对象, SillyExampleFriendsList

class SillyExampleFriendsList(object):
    """Just manipulates a couple lists"""
    def __init__(self):
       self._lock = threading.RLock()
       self._friends = []
       self._enemies = []

    def unfriend(self, x):
       # we lock here to ensure that we're never in a state where
       # someone might think 'x' is both our friend and our enemy.
       self._lock.acquire()
       self._friends.remove(x)
       self._enemies.append(x)
       self._lock.release()

这里的要点只是上面的对象可能在多个线程之间共享,而不需要通过仔细使用锁来进行深度复制。 识别所有这些可能是必要的情况并不是微不足道的,精细的锁定策略可能更难以调试,并且仍然会产生开销。

也就是说,你可能根本不需要线程,锁或深度拷贝,也不需要对代码进行基准测试,但是如果你有需要解决的性能问题还不清楚。 我很好奇,是什么让你认为你的代码应该更快或者更快?


If you like you could just synchronize access to the shared resource with threading.Lock just like you would in any other threaded program rather than copying it.

Regardless, I think it's worth benchmarking your code with and without the deepcopy and otherwise measuring to figure out how good/bad the performance really is before making optimizations. Perhaps the reason it is slow has nothing to do with deepcopy.

EDIT regarding using locking: What I mean is that you can use more fine grained locking around this resource. I assume that your threads are doing more than accessing a shared resource. You can try to benefit from multiple threads doing work and then synchronize access to just the one "critical section" that involves writing to the shared resource. You might also investigate making your shared resource threadsafe. For example, if have a shared object, SillyExampleFriendsList:

class SillyExampleFriendsList(object):
    """Just manipulates a couple lists"""
    def __init__(self):
       self._lock = threading.RLock()
       self._friends = []
       self._enemies = []

    def unfriend(self, x):
       # we lock here to ensure that we're never in a state where
       # someone might think 'x' is both our friend and our enemy.
       self._lock.acquire()
       self._friends.remove(x)
       self._enemies.append(x)
       self._lock.release()

The point here is just that the above object could potentially be shared between multiple threads without deepcopy by careful use of locks. It's not trivial to identify all the cases where this might be necessary and fine grained locking strategies can be more difficult to debug and still introduce overhead.

That said, you may not need threads, locks, or deepcopy at all and without benchmarking your code it's not clear if you have a performance problem that needs to be solved. I'm curious what makes you think that your code should be, or needs to be, faster?

相关问答

更多
  • 如果我正确地记住/思考,在SQLAlchemy中,通常每次只有一个对象与给定的数据库记录相对应。 这样做是为了让SQLAlchemy能够保持你的Python对象与数据库同步,反之亦然(当然,如果Python外部有并发的数据库变异,那又是另一回事了)。 所以问题在于,如果您要复制这些映射对象中的一个,则会得到两个对应于相同数据库记录的不同对象。 如果您更改一个,那么它们将具有不同的值,并且数据库不能同时匹配它们。 我认为您可能需要做的是决定是否希望数据库记录反映您在更改副本属性时所做的更改。 如果是这样,那么 ...
  • 如果你喜欢,你可以像使用任何其他线程程序一样同步使用threading.Lock访问共享资源,而不是复制它。 无论如何,我认为在进行优化之前,有必要对代码进行基准测试,然后进行深度复制和其他测量,以确定性能的好坏。 或许它的速度缓慢的原因与深度拷贝无关。 关于使用锁定编辑:我的意思是,你可以使用更多的细粒度锁定此资源。 我假设你的线程不仅仅是访问共享资源。 您可以尝试从多个正在工作的线程中受益,然后将访问同步到只涉及写入共享资源的“关键部分”。 您也可以调查使您的共享资源线程安全。 例如,如果有一个共享对象 ...
  • 似乎没有解决方案。 所以我写了两个函数来保存和恢复LpVariables值。 这对我有所帮助。 def saveVarsAsDict(lpProblem): varsNameValue = {} for v in lpProblem._variables: if v.varValue != 0: varsNameValue[v.name] = v.varValue return varsNameValue def restoreVarsVa ...
  • 使用IronPython版本2.6.1 ,并在安装程序中选择标准库选项 。 我的安装示例: IronPython 2.6.1 (2.6.10920.0) on .NET 2.0.50727.4927 Type "help", "copyright", "credits" or "license" for more information. >>> import copy >>> help(copy.deepcopy) Help on function deepcopy in module copy: de ...
  • 在tensorflow中进行如下深度拷贝: tt = tf.get_variable('t',shape=[2,2]) deepcopy = tf.Variable(tt.initialized_value()) tt1= tf.identity(tt[0].assign([1,1])) tt2 = tf.identity(deepcopy[1].assign([2,2])) 这会给你所需的输出: [[ 1. 1. ] [-1.01704359 -1.162369 ...
  • 所以事实证明这个问题是Python的Multiprocessing.Pool类的问题(我没有提到,因为我认为它不是问题的一部分)。 默认情况下,池中的每个工作程序都在池打开的整个时间段内存在。 由单个工作程序执行的所有进程在任何时候都保留在内存中,并且由于将单个类实例传递给这些工作程序,因此每次执行解决方案时都会更新该类实例。 解决这个问题的简单方法是使用Multiprocessing.Pool的“maxtasksperchild”参数来限制单个工作程序在被杀死之前可以处理的任务数量以及在其位置生成的新任务 ...
  • deepcopy似乎有一些开销来检查它能够处理的所有不同情况。 如果你的列表总是列表的列表(一层嵌套),那么你可以尝试使用s = [list(x) for x in s]或s = list(map(list, s)) 。 两者似乎都快了很多: In [9]: s = [[random.randint(1, 1000) for _ in range(100)] for _ in range(100)] In [10]: %timeit copy.deepcopy(s) 10 loops, best of 3: ...
  • Python的copy模块不会生成不可变对象的副本,这会非常低效。 decimal.Decimal()对象是不可变的,所以它们只是为了复制操作而返回self : >>> from decimal import Decimal >>> d = Decimal() >>> d.__copy__() is d True >>> d.__deepcopy__({}) is d True 请参阅decimal模块文档 : 十进制数是不可变的。 因为它们是不可改变的,所以创建副本没有意义 。 你可以在任何地方使用副本 ...
  • 使用Twisted的内置流程生成API生成多个流程。 如果您的服务器将在UNIX上运行,您可以使用reactor套接字传输方法在服务器中的进程之间移动连接和侦听端口(尽管这些接口有一天也可能移植到Windows)。 由于全局解释器锁定 ,在Python中生成多个线程(无论您使用的是Twisted还是类似的系统)都不允许您使用多个核心。 Spawn multiple processes, using Twisted's built-in process-spawning API. You can use th ...
  • 请建议如何避免此错误消息。 避免这种情况的最好和最正确的方法是不要调用Thread.Abort() 。 一般来说, Thread.Abort()确实是一个坏主意。 相反,您应该围绕框架中构建的协作取消模型构建您的例程。 Please suggest how to avoid this error message. The best, and correct, way to avoid this is to not call Thread.Abort(). Thread.Abort() is really a ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。