首页 \ 问答 \ 如何处理ResultSet,你知道它只有一个记录(How to process ResultSet you know has only one record in it)

如何处理ResultSet,你知道它只有一个记录(How to process ResultSet you know has only one record in it)

我正在努力完成一项家庭作业,并且我正忙着处理一些SQL查询。

我的查询是询问库存数据库中某个项目的数量。 在给定主键的情况下,查询从表中请求名为quantity_in_stock的列。

我已经初步化了一些准备好的陈述。 这是我在这里使用的那个:

stmtFindColumn = Database.getConnection().prepareStatement(String.format("select ? from %s where %s = ?",
            INVENTORY_TABLE_NAME, SKU) );

现在调用一个单独的方法。 我传递一个静态const QTY_IN_STOCK,它定义为“quantity_in_stock”和项目的SKU编号,它是表中的主键。

private int getIntegerFromTable(String column, String key) {

    int toReturn = 0;

    try {
        // Complete the prepared statement 
        stmtFindColumn.setString(1, column);
        stmtFindColumn.setString(2, key);

        ResultSet result = stmtFindColumn.executeQuery();               
        toReturn = result.getInt(column);

    } catch (SQLException e) {
        LOG.error(e.getMessage());
        e.printStackTrace();
    }
    return toReturn;
}

当我运行查询时,我得到一个sql异常,告诉我:无效的列名称quantity_in_stock。

我尝试使用while循环处理result.next()并得到相同的错误。 当您知道只返回一条记录时,我找不到任何关于如何正确获取结果的示例。

帮帮我!

编辑:好的,我发现我的部分问题是我没有得到结果集,我应该期待一个。 有任何想法吗?

更新:我已经测试了我的代码,使用了一个花园变量语句和一个普通的字符串查询,它工作得很好。 所以问题在于我使用了准备好的声明。 有人可以检查我是否正在使用? 通配符正确吗? 谢谢!


I'm struggling with a homework assignment and am getting hung up on some SQL queries.

My query is interrogating an inventory database for the quantity of some item. The query requests the column with the name quantity_in_stock from the table, given the primary key.

I have initialized some prepared statements. This is the one I'm using here:

stmtFindColumn = Database.getConnection().prepareStatement(String.format("select ? from %s where %s = ?",
            INVENTORY_TABLE_NAME, SKU) );

Now a separate method is called. I pass it a static const QTY_IN_STOCK, which is defined as "quantity_in_stock" and the item's SKU number, which is the primary key in the table.

private int getIntegerFromTable(String column, String key) {

    int toReturn = 0;

    try {
        // Complete the prepared statement 
        stmtFindColumn.setString(1, column);
        stmtFindColumn.setString(2, key);

        ResultSet result = stmtFindColumn.executeQuery();               
        toReturn = result.getInt(column);

    } catch (SQLException e) {
        LOG.error(e.getMessage());
        e.printStackTrace();
    }
    return toReturn;
}

When I run the query I get an sql exception that tells me: Invalid column name quantity_in_stock.

I have tried using a while loop processing result.next() and get the same error. I can't find any examples of how to properly get the results when you know only a single record is being returned.

Help!

EDIT: OK, I've found that part of my problem is I'm not getting a result set, where I should expect one. Any ideas?

UPDATE: I've tested my code, using a garden variety statement and a plain string query instead and it works just fine. So the problem is in my use of the prepared statement. Can someone check if I'm using the ? wildcards correctly? Thanks!


原文:https://stackoverflow.com/questions/8283786
更新时间:2023-10-23 10:10

最满意答案

解决方案: http : //code.google.com/p/chromium/issues/detail?id=91666

要清除所有断点,打开检查员的检查员(停用第一个检查器并按ctrl-shift-I打开第二个),并在第二个检查器的控制台中运行“WebInspector.settings.domBreakpoints.set([])”。


solution here: http://code.google.com/p/chromium/issues/detail?id=91666

To purge all breakpoints open inspector on inspector (undock first inspector and hit ctrl-shift-I to open the second) and run "WebInspector.settings.domBreakpoints.set([])" in second inspector's console.

相关问答

更多
  • 解决方案:您需要清单文件,后台脚本和内容脚本。 这在文档中不太清楚,您必须使用它以及如何使用它。 为了提醒整个dom,请看这里 。 因为我很难找到一个真正有效的完整解决方案,而不仅仅是像我这样对新手无用的片段,所以我提供了一个特定的解决方案: 的manifest.json { "manifest_version": 2, "name": "Test Extension", "version": "0.0", "background": { "persi ...
  • Chrome会自动创建与每个元素对应的全局变量。 在生产代码中使用这些变量是一个非常糟糕的主意,因为在javascript中没有变量是静态的 - 有人可能稍后为myelement分配了其他内容而你无法知道。 如果您确实使用此功能(可能在测试环境中,如控制台),我猜它会比getElementById()快,因为将myelement分配给相应节点的代码首先运行。 Chrome automatically creates global variables corresponding to each element ...
  • 要在Chrome中设置断点会弹出如上所示的检查器,然后单击顶部的脚本选项。 这将允许您查看页面上使用的脚本,并在该页面上插入断点。 以及通过他们和其他有用的调试选项。 以上是针对javascript,打破dom元素,右键单击要打破的元素(内部的检查器),它将弹出上下文菜单,允许您打破子树修改和类似的东西。 To set break points in Chrome bring up the inspector like you have shown above and click on the script ...
  • 解决方案: http : //code.google.com/p/chromium/issues/detail?id=91666 要清除所有断点,打开检查员的检查员(停用第一个检查器并按ctrl-shift-I打开第二个),并在第二个检查器的控制台中运行“WebInspector.settings.domBreakpoints.set([])”。 solution here: http://code.google.com/p/chromium/issues/detail?id=91666 To purge ...
  • 术语“背景页”,“弹出”,“内容脚本”仍然令您感到困惑; 我强烈建议您更深入地了解Google Chrome扩展程序文档 。 关于你的问题,如果内容脚本或背景页面是要走的路: 内容脚本 :绝对 内容脚本是可以访问网页DOM的扩展的唯一组件。 背景页面/弹出式窗口 :也许(可能是两个中最多的1个) 您可能需要让内容脚本将DOM内容传递到后台页面或弹出窗口进行进一步处理。 让我重复一遍,我强烈建议对可用的文件进行更仔细的研究! 也就是说,这是一个示例扩展,它检索StackOverflow页面上的DOM内容,并将 ...
  • 我猜想区别在于304表示浏览器发送了请求, 服务器表明资源没有改变,而from cache状态可能表示Chrome甚至没有发送请求 - 浏览器知道使用没有联系服务器的缓存。 这可以通过像Fiddler这样的工具进行验证,以查看是否曾经请求过from cache资源。 I would guess that the distinction is that a 304 indicates the browser sent a request and the server indicated the resourc ...
  • 我找到的最好的方法是使用selenium.webdriver : import selenium.webdriver as webdriver import lxml.html as lh import lxml.html.clean as clean browser = webdriver.Chrome() # Get local session of Chrome browser.get("http://www.webpage.com") # Load page content=browser.pa ...
  • 我解决了这个问题。 Visual Studio Code Debugger仅适用于适用于Chrome,Angular CLI和Angular的调试器的特定版本组合。 它不适用于Angular 4.0和1.0版本的Angular CLI。 但是Angular 4.1.3和Angular CLI 1.0.6工作正常。 按着这些次序: 安装Chrome调试程序可视代码扩展 在“调试”菜单上,单击“打开配置” 将.vscode \ launch.json修改为如下所示: { "version": "0.2. ...
  • 在“元素”选项卡中,右侧部分包含Styles ,“ Computed ,“ Event Listeners ,“ DOM Breakpoints ”和“ Properties 。 选择Event Listneners 。 In the Elements tab, the right-hand part has options Styles, Computed, Event Listeners, DOM Breakpoints, and Properties. Select Event Listneners. ...
  • DOM(文档对象模型)是一组对象的通用术语。 document是一个DOM对象; 你只是看到了两种不同的表现方式。 您可以通过.children属性访问DOM层次结构。 有关更多信息,请参阅文档 。 The DOM (Document Object Model) is a generic term for a set of objects. document is a DOM object; you're just seeing two different ways of representing it. ...

相关文章

更多

最新问答

更多
  • 获取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的基本操作命令。。。