简体   繁体   English

在Java中读取两个不同的arrayList值,并将其添加到同一数组Object

[英]Read two different arrayList value in java and add it to a same array Object

I am trying to create update sql query from csv file. 我正在尝试从csv文件创建更新sql查询。

So, for that I am trying to read two different array list value, one is coming from CSV file and other one is coming from database. 因此,为此,我尝试读取两个不同的数组列表值,一个来自CSV文件,另一个来自数据库。 I want to update my Books Database but there are some fields which should be as it is. 我想更新我的图书数据库,但是有些字段应该保持原样。

I wrote below piece of code to do that and I am able to print the query, however I feel my code is not efficient. 我写了下面的代码来做到这一点,并且能够打印查询,但是我觉得我的代码效率不高。 I could have done this in a more efficient way like instead of reading indexes. 我本可以以更有效的方式完成此任务,而不是读取索引。

public List<Book> generateSqlQuery(List<Book> booksFromCSV) throws Exception{

    String update_query = "UPDATE Books SET book_name={0}, author_name={1}";

    String updateQuery = null;
    Object [] book = null;

    MessageFormat messageFormat = new MessageFormat(update_query);

    List<Book> booksFromDB = BookDao.getBooks();


    for(int i=0; i<booksFromCSV.size()-1; i++){

        book = new Object[] { booksFromDB.get(i).getBookName(),
                booksFromCSV.get(i).getAuthorName()};


        updateQuery = messageFormat.format(book);
        System.out.println(updateQuery);
    }
    return booksFromCSV;
}

Please give me some advice or different approaches, of how to read two different arraylist value and keep it in same array. 请给我一些建议或不同的方法,关于如何读取两个不同的arraylist值并将其保存在同一数组中。

I apologize in advance for the grammar mistake or any other mistake. 对于语法错误或任何其他错误,我事先表示歉意。

This could create scenarios of SQL Injection. 这可能会创建SQL注入方案。 Use bind variables and loop against the list. 使用绑定变量并针对列表进行循环。 If you do believe there are chances of a long list, you can try batch update. 如果您确实认为列表很长,可以尝试批量更新。

从 ArrayList 中查找 object<object> 对于相同的 object.value - Java 8<div id="text_translate"><p> 我需要从ArrayList&lt;Object&gt;中找到具有类似object.trackingId的对象。</p><p> 例如:</p><pre> ArrayList&lt;Ilist&gt; ilistArray = new ArrayList&lt;&gt;(); ilistArray.addAll(...); // array is filled with Ilist objects</pre><pre> class Ilist { String name; String trackingId; String place; // Et cetera }</pre><p> 我需要从具有相似 trackingId 的ilistArray中找到那些 object。</p></div></object> - Find object from ArrayList<Object> for same object.value - Java 8

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java - 是ArrayList和ArrayList <Object> 两个不同的班级? - Java - Are ArrayList and ArrayList<Object> two different classes? 添加到数组对象更改相同的值java - add into array object change same value java 读取文本文件并将其添加到Java中的对象的ArrayList中 - Read and Add Text File to an ArrayList of Object in Java Android两种不同类型的对象arraylist添加到单个arraylist - Android Two different type object arraylist add into single arraylist 从 ArrayList 中查找 object<object> 对于相同的 object.value - Java 8<div id="text_translate"><p> 我需要从ArrayList&lt;Object&gt;中找到具有类似object.trackingId的对象。</p><p> 例如:</p><pre> ArrayList&lt;Ilist&gt; ilistArray = new ArrayList&lt;&gt;(); ilistArray.addAll(...); // array is filled with Ilist objects</pre><pre> class Ilist { String name; String trackingId; String place; // Et cetera }</pre><p> 我需要从具有相似 trackingId 的ilistArray中找到那些 object。</p></div></object> - Find object from ArrayList<Object> for same object.value - Java 8 java将对象添加到arrayList - java add object to arrayList 如何在Java中将文本文件读取到不同对象类型的ArrayList? - How to read text file to ArrayList of different object types in java? java:将2D数组的值与同一列的arraylist值进行比较 - java: comparing value of 2D-array with value of arraylist at the same column 如何在ArrayList中检索值 <Object> 它包含两个不同的对象类型arraylist。 - How to retrieve a value in ArrayList<Object> where it contains two different object type arraylist..? Java数组列表 <Object> 如果对象具有相同的属性值,则要合并 - Java Arraylist<Object> want to merge the objects if they have the same property value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM