我目前有以下代码,它是我的 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
}