简体   繁体   English

java jdbc连接 - 结果集输出问题

[英]java jdbc connection - result set output problems

I am currently trying to implement a jdbc connection that returns all the data in a table when i "search" for anything that matches the input with '%input%'. 我正在尝试实现一个jdbc连接,当我“搜索”与“%input%”匹配输入的任何内容时,它返回表中的所有数据。

eg ResultSet rs4 = stm4.executeQuery("select imageTime from image_data where imageName like '%" + value3 + "%' or imageTime like '%" + value3 + "%' or imageLocation like '" + value3 + "'" ); 例如ResultSet rs4 = stm4.executeQuery("select imageTime from image_data where imageName like '%" + value3 + "%' or imageTime like '%" + value3 + "%' or imageLocation like '" + value3 + "'" );

i am trying to return ALL the rows in the result set as search results. 我试图将结果集中的所有行作为搜索结果返回。 but if i have Resultset.next commanded when there is no more rows to go to it causes the following results sets to all null,.... 但如果我在没有更多行去的情况下命令Resultset.next,则会导致以下结果集全部为null,....

if anything id love a method to output the entire result set, thanks. 如果有任何id喜欢输出整个结果集的方法,谢谢。

EDIT editing the question: to be more direct; 编辑编辑问题:更直接; i need a way to get every piece of data from each row in each containing column of the result set. 我需要一种方法来从结果集的每个包含列中的每一行获取每一段数据。 so i can output it. 所以我可以输出它。 This is my attempt of this below. 这是我在下面的尝试。

rs4 = a Resultset as declared below. rs4 =如下所述的结果集。

here is my code; 这是我的代码;

if(name_time_location == 1)
        {                
            String value3=searchInput.getText();//Sets the search Input as value3


            // selecting the cominbation from table, that match input options                               
            try{
                con = DriverManager.getConnection("jdbc:mysql:blah blah");

                // Query the database for the correct username and passord                    
                Statement stm3 = con.createStatement();  
                Statement stm4 = con.createStatement();
                Statement stm5 = con.createStatement();


                //queries database for password from input username 
                ResultSet rs3 = stm3.executeQuery("select imageName from image_data    where imageName like '%" + value3 + "%' or imageTime like '%" + value3 + "%' or imageLocation like '" + value3 + "'" );
                //ResultSetMetaData rsmd = rs3.getMetaData();
                //stm3.setFetchSize(5);
                //rs3.last();
                //int numberOfRows = rs3.getRow();


                //String[] resultList; 
                //resultList = new String[numberOfRows];
                // Fetch each row from the result set
                rs3.beforeFirst();
                while(rs3.next())
                {
                    imageSearchResult1 = rs3.getString(1); 
                    rs3.next();
                    imageSearchResult11 = rs4.getString(1);
                    rs3.next();
                    imageSearchResult12 = rs4.getString(1);
                    rs3.next();
                    imageSearchResult13 = rs4.getString(1);
                    rs3.next();
                    imageSearchResult14 = rs4.getString(1); 
                }rs3.close();

            }catch (Exception e)
            {
                    //System.out.println("Exception: " + e + "");
            }

            System.out.println("Search Results: \nName: " + imageSearchResult1 + "         Time stamp: " + imageSearchResult2 + "   Location: " + imageSearchResult3 + "\n" +
                    "Name: " + imageSearchResult11 + "   Time stamp: " + imageSearchResult21 + "   Location: " + imageSearchResult31 + "\n" + 
                    "Name: " + imageSearchResult12 + "   Time stamp: " + imageSearchResult22 + "   Location: " + imageSearchResult32 + "\n" +
                    "Name: " + imageSearchResult13 + "   Time stamp: " + imageSearchResult23 + "   Location: " + imageSearchResult33 + "\n" +
                    "Name: " + imageSearchResult14 + "   Time stamp: " + imageSearchResult24 + "   Location: " + imageSearchResult34 + "\n" );

I think you can achieve the same thing by modifying the query and instead of creating 3 queries, get the 3 values in the same query as: 我认为您可以通过修改查询来实现相同的目的,而不是创建3个查询,在同一个查询中获取3个值:

select imageName,imageLocation,imageTime from .....

Then use this query to generate the ResultSet and get the three values as rs.getType(1),rs.getType(2),rs.getType(3) . 然后使用此查询生成ResultSet并获取三个值,如rs.getType(1),rs.getType(2),rs.getType(3)

In the same while(rs.next()) loop, you can print the data that you want to print. while(rs.next())循环中,您可以打印要打印的数据。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM