热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ArrayList中的每个String[]具有相同的值-EveryString[]withinanArrayListhasthesamevalue

IamtryingtocreateOracleSQLscriptsforcreatingreplacingseveralviewsbyusinginformationa

I am trying to create Oracle SQL scripts for creating/replacing several views by using information acquired from the data dictionary.

我正在尝试使用从数据字典中获取的信息创建Oracle SQL脚本以创建/替换多个视图。

I already managed to set up a working database connection via JDBC drivers and a query to the data dictionary, which works as well and returns the correct values.

我已经设法通过JDBC驱动程序和对数据字典的查询来建立工作数据库连接,该数据字典也可以工作并返回正确的值。

However upon storing the queried information in a String array - which is then added to an ArrayList of String[] - something seems to go wrong, as all arrays seem to have the same values in their respective index positions and I don't have a clue why that is.

然而,在将查询的信息存储在String数组中时 - 然后将其添加到String []的ArrayList中 - 似乎出现了问题,因为所有数组似乎在各自的索引位置具有相同的值,而我没有提示为什么会这样。

Here's my code, I'd appreciate if someone can spot the error:

这是我的代码,如果有人能发现错误,我会很感激:

public ArrayList  getDataDictionary(ArrayList dbInfo, String table) throws SQLException {

ArrayList result = new ArrayList();
String[] resultTemp = new String[2];

... connection variables (URL, User, Pass)
... get connection, etc.

try {
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME,DATA_TYPE FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '" + table + "'");

            while (rs.next()) {
                resultTemp[0] = rs.getString("COLUMN_NAME");
                resultTemp[1] = rs.getString("DATA_TYPE");

                // database values
                System.out.println(rs.getString("COLUMN_NAME"));
                System.out.println(rs.getString("DATA_TYPE"));
                // array values
                System.out.println(resultTemp[0]);
                System.out.println(resultTemp[1]);

                //The above sout's return the proper values for each pass of the loop
                //This is what feels the strangest to me. The values are correct here, but when queried later they are wrong

                result.add(resultTemp);
            }

            String[] test = new String[2];

            // sout's return wrong values now, i.e. the value returned is always the same for all arrays queried in the ArrayList
            //I don't understand how that can be, because the correct values were added to the ArrayList a few lines above and now they are wrong with no changes made
            test = result.get(0);
            System.out.println(test[0]);
            System.out.println(test[1]);

            test = result.get(1);
            System.out.println(test[0]);
            System.out.println(test[1]);

            test = result.get(2);
            System.out.println(test[0]);
            System.out.println(test[1]);

            rs.close();
            statement.close();
            con.close();

            return result;  
} catch(Exception e)
        {
            e.printStackTrace();
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error!");
            alert.setHeaderText("Invalid SQL!");
            alert.setContentText("Please verify the information you provided!");
            alert.showAndWait();
            return null;
        }

3 个解决方案

#1


3  

You should create the array instance inside the loop.

您应该在循环内创建数组实例。

       while (rs.next()) {
            String[] resultTemp = new String[2];
            resultTemp[0] = rs.getString("COLUMN_NAME");
            resultTemp[1] = rs.getString("DATA_TYPE");
            ....

Failing to do so causes the same array to be added to result multiple times.

如果不这样做会导致将相同的数组多次添加到结果中。

#2


3  

You store a reference to the same object in every loop.

您在每个循环中存储对同一对象的引用。

You have to create in every lopp a new Array:

你必须在每个lopp中创建一个新数组:

 while (rs.next()) {
      resultTemp = new String[2];

                resultTemp[0] = rs.getString("COLUMN_NAME");
                resultTemp[1] = rs.getString("DATA_TYPE");

                // database values
                System.out.println(rs.getString("COLUMN_NAME"));
                System.out.println(rs.getString("DATA_TYPE"));
                // array values
                System.out.println(resultTemp[0]);
                System.out.println(resultTemp[1]);

                //The above sout's return the proper values for each pass of the loop
                //This is what feels the strangest to me. The values are correct here, but when queried later they are wrong

                result.add(resultTemp);
            }

#3


2  

You are overriding the same array , String[] resultTemp = new String[2];

你重写了相同的数组,String [] resultTemp = new String [2];

while (rs.next()) {
            String[] resultTemp = new String[2];
            resultTemp[0] = rs.getString("COLUMN_NAME");
            resultTemp[1] = rs.getString("DATA_TYPE");

Intialise it inside the while loop. So that when you add

在while循环中初始化它。所以当你添加

result.add(resultTemp);

result will hold the list of resultTemp[] objects inside it .

result将保存其中的resultTemp []对象列表。


推荐阅读
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • 本文介绍了游标的使用方法,并以一个水果供应商数据库为例进行了说明。首先创建了一个名为fruits的表,包含了水果的id、供应商id、名称和价格等字段。然后使用游标查询了水果的名称和价格,并将结果输出。最后对游标进行了关闭操作。通过本文可以了解到游标在数据库操作中的应用。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • IhaveconfiguredanactionforaremotenotificationwhenitarrivestomyiOsapp.Iwanttwodiff ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
author-avatar
我就是老笨2013
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有