获取用户输入,然后存储在resultSet列表中

时间:2013-10-07 08:23:20

标签: java

我目前有以下代码,我的Gui为我的主要gui的部分图像搜索器,从我的角度来看它的目标是获取用户输入然后打印出用户输入并将用户输入添加到另一个集合中然后使用此输入来搜索我的数据库中的匹配图像,用户无需选择所有3个搜索条件,只需选择一个。我的问题是,我需要有关如何将用户输入存储在结果集列表中的指导,然后使用此存储的信息来查询我的数据库。

到目前为止,我已尝试抓取用户输入,但我不确定接下来需要做什么。

class DisplaySearch {
    // database URL                              

    static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
    static final String USERNAME = "root";
    static final String PASSWORD = "root";

    // launch the application
    public static void Search(String imageName, String DateStamp, String imageTag) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Connection connection = null; // manages connection
        Statement statement = null; // query statement
        ResultSet resultSet = null; // manages results

        // connect to database books and query database
        try {
            // establish connection to database
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

            // create Statement for querying database
            statement = connection.createStatement();

            // query database                                        
            resultSet = statement.executeQuery(
                    "SELECT imageName, DateStamp, imageTag FROM images");

            // process query results   
            System.out.println("Search Criteria");
            while (resultSet.next()) { 
                // Position the cursor 
                imageName = resultSet.getString(0); 
                DateStamp= resultSet.getString(1); 
                imageTag = resultSet.getString(2); 
                System.out.println("datas : " + imageName + " " + DateStamp + " " + imageTag ); 
            //while (resultSet.next()) {
                if (imageName != "" || imageName != null) {
                    String Name = resultSet.getString(imageName);
                    System.out.print("Image Name: " + Name);
                    resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageName='"
                            + imageName + "'");
                }

                if (DateStamp != null) {
                    String Date = resultSet.getString(DateStamp);
                    System.out.print(", Date: " + Date);
                    resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where DateStamp='"
                            + DateStamp + "'");
                }

                if (imageTag != "" || imageTag != null) {
                    String Tag = resultSet.getString(imageTag);
                    System.out.print(", Tag: " + Tag);
                    resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageTag='"
                            + imageTag + "'");
                }
            } // end while
        } // end try
        catch (SQLException sqlException) {
            sqlException.printStackTrace();
        } // end catch                                                     
        finally // ensure resultSet, statement and connection are closed
        {
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } // end try                                               
            catch (Exception exception) {
                exception.printStackTrace();
            } // end catch                                             
        } // end finally                                             
    }

3 个答案:

答案 0 :(得分:1)

为什么要将数据存储在ResultSet对象中?我认为这不是一种推荐的方式。 您可以使用某个变量来存储值,并将它们与查询一起使用以将数据导入结果集

答案 1 :(得分:1)

您可以在代码中添加一些限制,例如:

if(imagename !="" || imageName !=null)
{
    resultSet = statement.executeQuery
    ("select imagename, DateStamp, ImageTag from images where imageName= '" + imageName + "'");
}

if(DateStamp != null
{
    resultSet = statement.executeQuery
    ("select imageName, DateStamp, imageTag from images where imageName = '"+imageName+"');
}

if(imageTag !="" || imageTag != null)
{
    resultSet = statement.executeQuery
    ("select imageName, DateStamp, imageTag from images where imageName = '"+imageName+"');
}

答案 2 :(得分:1)

首先,使用String.equals(...),并检查null 之前的相等性检查。另外,Java变量名称以小写字母开头。

一个简单的解决方案是使用*(通配符)初始化三个变量,并且仅在用户输入一些文本时更改值,因此如果没有用户输入,您最终会得到如下内容:

SELECT imageName, DateStamp, imageTag FROM images WHERE imageName='*' AND DateStamp='*' AND imageTag='*'

加上我会建议您使用PreparedStatement ?作为占位符。

PreparedStatement prepStmt = conn.prepareStatement("SELECT imageName, DateStamp, imageTag FROM images WHERE imageName=? AND DateStamp=? AND imageTag=?");
prepStmt.setString(1, "image-name");
prepStmt.setString(2, "date-stamp"); // maybe .setDate(...)
prepStmt.setString(3, "image-tag");
ResultSet rs = prepStmt.executeQuery();