Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
促进Java应用程序中的SQL表查询功能_Java_Sql_Data Structures - Fatal编程技术网

促进Java应用程序中的SQL表查询功能

促进Java应用程序中的SQL表查询功能,java,sql,data-structures,Java,Sql,Data Structures,假设我有两个专栏,分别是名字、姓氏、电子邮件、电话 我想根据动态列选择查询行 假设应用程序将根据1)姓氏和电话或2)姓氏3)电话和电子邮件请求记录 有没有适合我的需要的数据结构,而不是创建一个表来执行SQL查询来根据列数据查找行我用Java编写代码,因此如果有内置API,请推荐一个 FirstName | LastName | Email | Phone abc | xyz | abc@m.com | 123 pqr | qwe | pqr@m

假设我有两个专栏,分别是名字、姓氏、电子邮件、电话

我想根据动态列选择查询行

假设应用程序将根据1)姓氏和电话或2)姓氏3)电话和电子邮件请求记录

有没有适合我的需要的数据结构,而不是创建一个表来执行SQL查询来根据列数据查找行我用Java编写代码,因此如果有内置API,请推荐一个

FirstName | LastName | Email     | Phone
abc       | xyz      | abc@m.com | 123
pqr       | qwe      | pqr@m.com | 342
ijk       | uio      | ijk@m.com | 987

假设您有类似于HashMap的field=>value 然后你可以这样做(如果你不想用value进行查询,你可以去掉where语句)


我将为您指出任何可用的内存中SQL Db库:

  • 德比
  • HSQL
  • 或者您想要一个可索引、可查询的内存存储:

  • 黑兹卡斯特
  • Ehcache

  • 其中任何一个都允许您针对存储的数据编写查询。

    如果您希望将信息加载到内存中并可用于多个查询,我将使用使用映射(例如HashMap)和ArrayList的查找结构

    注意:如果您只想查询一次,我会在阅读这些行时直接在look中进行查询

    例如:
    HashMap lookup=newhashmap()

    示例:

    import java.util.ArrayList;
    import java.util.HashMap;
    
    public class WordLookup {
    
        public static void main(String args[]) {
            WordLookup wl = new WordLookup();
    
            String[] simulatedFileRows = new String[5];
            simulatedFileRows[0] = "cat,dog";
            simulatedFileRows[1] = "hen,dog";
            simulatedFileRows[2] = "cat,mouse";
            simulatedFileRows[3] = "moose,squirrel";
            simulatedFileRows[4] = "chicken,rabbit";
    
            String columns[];
            String row;
            int column = 0;
    
            for(int i=0; i<simulatedFileRows.length; i++)   //Simulated readline
            {
                row = simulatedFileRows[i];
                columns = row.split(",");
                column=0;
    
                for(String col:columns)
                {
                    column++;
                    wl.addWord(col, i, column);
                }
            }
    
    
            //Where is moose?
    
            ArrayList<wordLocation> locs = wl.getWord("moose");
            if(locs!=null)
            {
                System.out.println("Moose found at:");
                for(wordLocation loc: locs)
                System.out.println("\t line:"+ loc.line + " column" + loc.column);
            }
    
    
        }
    
        private HashMap<String, ArrayList<wordLocation>> lookup= new HashMap<String, ArrayList<wordLocation>>();
    
        public void addWord(String word, int line, int column)
        {
            ArrayList<wordLocation> wordLocArr = lookup.get(word);
            if(wordLocArr == null)
            {
                wordLocArr = new ArrayList<wordLocation>();
                lookup.put(word,wordLocArr);
            }
    
            wordLocArr.add( new wordLocation(line, column));
        }
    
        public ArrayList<wordLocation> getWord(String word)
        {
            return lookup.get(word);
        }
    
        class wordLocation{
            public int line, column;
    
            public wordLocation(int l, int c)
            {this.line = l; this.column = c;}
        }
    
    
    }
    
    import java.util.ArrayList;
    导入java.util.HashMap;
    公共类查字{
    公共静态void main(字符串参数[]){
    WordLookup wl=新的WordLookup();
    String[]simulatedFileRows=新字符串[5];
    模拟文件行[0]=“猫,狗”;
    模拟文件行[1]=“母鸡,狗”;
    simulatedFileRows[2]=“猫,鼠标”;
    simulatedFileRows[3]=“驼鹿,松鼠”;
    simulatedFileRows[4]=“鸡、兔”;
    字符串列[];
    字符串行;
    int列=0;
    
    对于(inti=0;i来说,如果不为SQL DB中的列编制索引,这大致相当于简单地拥有一个数组,其中每个元素对应一行

    如果对列进行索引,这与为每个索引添加类似
    TreeMap
    (字符串或整数或某些对象集合,取决于字段的类型)的内容大致相同(至少基于我对DBs底层结构的有限了解——实际上我认为数据库通常使用,但据我所知,标准Java API中没有b树结构)

    实际上,数组索引的
    TreeMap
    对于非唯一索引是不够的,您必须有数组索引列表的
    TreeMap
    ,或者(不在标准API中)

    如果没有任何给定查询的索引,则必须遍历所有行以找到正确的行。类似地,必须遍历整个数组以找到正确的元素

    因此,如果您只想查询单个列(并且这样做很有效),而这可以是任何列,那么您必须为每个列提供一个
    TreeMap
    (或类似内容),以及一个数组或类似内容作为基本结构

    但是,如果我们讨论的是查询任何列的组合,您不太可能得到一个特别有效的通用解决方案,因为组合太多,无法为所有列(即使是少数列)提供一个结构


    注意:我说的是
    TreeMap
    而不是
    HashMap
    ,因为这更接近数据库的实际工作方式。如果您运行的查询类型不需要排序数据,您可以使用
    HashMap

    您想使用where语句吗?还是只需要一组列?我不想这样做执行sql stmt。基于动态列选择,我希望返回一条记录。我正在分析一个文件,不知道哪些列将包含数据。我希望从该文件中找到一条包含可用信息的记录的最佳途径。因此,您基本上是在查找哪一行包含一个包含您要查找的条目的列?您可以使用一个具有关键字的哈希映射单词,并包含位置的另一个数据结构。答案中的示例。通过“内置API”,我假设您的意思是“标准API中的此功能”。因为只有一个“内置”Java API。问题中的措辞可能有点模棱两可,但OP似乎并没有要求外部库。如果是这样,它将脱离主题,这导致我得出结论,这个答案不符合指导原则。实际上,问题在于原始问题的措辞误导了原始回答者。如至少应该是“Fanclitate SQL表查询功能”,更具体地说,OP承认的基本要求是“查找哪一行有一个具有搜索值的列”.事实上,我不同意。OP明确要求回答一个问题,其中有许多著名的、已经解决的库,可以完全满足他的要求。如果我问了一个问题,有人向我提供了一个替代的、不太容易出错的解决方案,我会很感激。有许多库已经满足了OP的要求,but要求建立这样一个图书馆是离题的,而且可能是出于同样的原因,答案建议建立这样的图书馆(作为答案的主要部分)对于没有明确要求这样的库的问题是不合适的。OP是否会感激并不影响它是否合适。我认为它可能适合作为一个评论。
    import java.util.ArrayList;
    import java.util.HashMap;
    
    public class WordLookup {
    
        public static void main(String args[]) {
            WordLookup wl = new WordLookup();
    
            String[] simulatedFileRows = new String[5];
            simulatedFileRows[0] = "cat,dog";
            simulatedFileRows[1] = "hen,dog";
            simulatedFileRows[2] = "cat,mouse";
            simulatedFileRows[3] = "moose,squirrel";
            simulatedFileRows[4] = "chicken,rabbit";
    
            String columns[];
            String row;
            int column = 0;
    
            for(int i=0; i<simulatedFileRows.length; i++)   //Simulated readline
            {
                row = simulatedFileRows[i];
                columns = row.split(",");
                column=0;
    
                for(String col:columns)
                {
                    column++;
                    wl.addWord(col, i, column);
                }
            }
    
    
            //Where is moose?
    
            ArrayList<wordLocation> locs = wl.getWord("moose");
            if(locs!=null)
            {
                System.out.println("Moose found at:");
                for(wordLocation loc: locs)
                System.out.println("\t line:"+ loc.line + " column" + loc.column);
            }
    
    
        }
    
        private HashMap<String, ArrayList<wordLocation>> lookup= new HashMap<String, ArrayList<wordLocation>>();
    
        public void addWord(String word, int line, int column)
        {
            ArrayList<wordLocation> wordLocArr = lookup.get(word);
            if(wordLocArr == null)
            {
                wordLocArr = new ArrayList<wordLocation>();
                lookup.put(word,wordLocArr);
            }
    
            wordLocArr.add( new wordLocation(line, column));
        }
    
        public ArrayList<wordLocation> getWord(String word)
        {
            return lookup.get(word);
        }
    
        class wordLocation{
            public int line, column;
    
            public wordLocation(int l, int c)
            {this.line = l; this.column = c;}
        }
    
    
    }