1

我目前有以下代码,它是我的 Gui,用于我的主要 gui 的一部分的图像搜索器,从我的角度来看,它的目标是获取用户输入,然后打印出用户输入并将用户输入添加到另一组 JTextFields 和然后使用这个输入在我的数据库中搜索匹配的图像,用户不需要选择所有 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                                             
    }
4

3 回答 3

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+"');
}
于 2013-10-07T08:39:25.417 回答
1

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

于 2013-10-07T08:30:57.920 回答
1

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

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

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

另外,我建议您使用PreparedStatementwhere a?作为占位符。

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();
于 2013-10-07T18:18:03.547 回答