Home>

I have created a system that creates a database with sql and searches it on html using java.
I am trying to create a database that contains multiple information such as movie titles and release years, and try to search on html, but it does not work.
I am instructed by e-mail what to do next, but it's difficult and I don't understand.

I want to get the results of a database search in a file called Model.java.
In order to do so, it seems that the process of getting the search result should be rewritten to hold it in ArrayList.

I'm not sure what to hold in an ArrayList
It is said that it is already done in the previously created file called DataAcces.java. What part of DataAcces.java is
Form that retains the process of obtaining the search result in ArrayList
I don't know, so I would appreciate it if you could tell me.

package movie;
import java.sql.*;
import java.util.HashMap;

public class DataAccess
{
private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet resultSet = null;
private HashMapTitle_hash;

private String sql = null;

public HashMapresult_search(String search){
Title_hash = new HashMap<>();
int i = 0;

try {
sql = "SELECT title_name,years_publication,performance_income,production_cost,public_evaluation,screening_time,genre_name,original_name,cast_name,directed_name "+
"FROM movie inner join original_list on movie.original_number=original_list.original_number "+
"inner join genre_list on movie.genre_number=genre_list.genre_number "+
"inner join directed_list on movie.directed_number=directed_list.directed_number "+
"inner join cast_list on movie.cast_number=cast_list.cast_number where title_name like? or directed_name like ?";
conn = ConnectUtilMy.connectDatabase();
stmt = conn.prepareStatement(sql);
stmt.setString(1,"%"+search+"%");
stmt.setString(2,"%"+search+"%");
System.out.println(search);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
TitleListResult result = new TitleListResult();
result.Set_Title_name( resultSet.getString("title_name"));
result.Set_years_publication( resultSet.getString("years_publication"));
result.Set_performance_income( resultSet.getDouble("performance_income"));
result.Set_Production_cost( resultSet.getDouble("production_cost"));
result.Set_performance_income( resultSet.getDouble("performance_income"));
result.Set_Public_evaluation( resultSet.getDouble("public_evaluation"));
result.Set_Screening_time( resultSet.getString("screening_time"));
result.Set_Genre_name( resultSet.getString("genre_name"));
result.Set_directed_name( resultSet.getString("directed_name"));
result.Set_cast_name( resultSet.getString("cast_name"));
result.Set_original_name( resultSet.getString("original_name"));

Title_hash.put(i,result);
i++;

}
}
catch (SQLException ex) {
System.out.println("Error code:" + ex.getErrorCode());
System.out.println("SQL state:" + ex.getSQLState());
ex.printStackTrace();
}
finally {
try {
if (resultSet != null) {
resultSet.close();
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
try {
if (stmt != null) {
stmt.close();
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
try {
if (conn != null) {
conn.close();
}
}
catch (SQLException ex) {
ex.printStackTrace();
}

}
return Title_hash;
}

}

  • Answer # 1

    ArrayList<TitleListResult>resultList = new ArrayList<TitleListResult>();
    while (resultSet.next()) {
    TitleListResult result = new TitleListResult();
    result.Set_Title_name( resultSet.getString("title_name"));
    result.Set_years_publication( resultSet.getString("years_publication"));
    result.Set_performance_income( resultSet.getDouble("performance_income"));
    result.Set_Production_cost( resultSet.getDouble("production_cost"));
    result.Set_performance_income( resultSet.getDouble("performance_income"));
    result.Set_Public_evaluation( resultSet.getDouble("public_evaluation"));
    result.Set_Screening_time( resultSet.getString("screening_time"));
    result.Set_Genre_name( resultSet.getString("genre_name"));
    result.Set_directed_name( resultSet.getString("directed_name"));
    result.Set_cast_name( resultSet.getString("cast_name"));
    result.Set_original_name( resultSet.getString("original_name"));
    resultList.add(result);
    }


    I suppose this is because it will be rewritten,

    if possible,
    "Currently, I'm putting the fetched results from the DB in a HashMap. Is this meant to be an ArrayList instead of a HashMap?"
    I should ask the person giving the instruction.