-4

I have a problem with mysql database when I try to find a code that can help me with getting multiple results for the word "get". I find only one suggestion when I search through a database. Here is what I get

Enter a word to search_______________: get

Consider this alternative____________: access (E.g. You can access some of the best lawyers)

But I have other entries that are also important when it comes to the word "get", and the words that can fit here as alternatives are 7. So what can I do in Java to solve this problem? I have a table called grammarchecker, and a column named wrongwords, while the other one is rightwords.

How can I code this in Java? I have watched some videos on hashing a dictionary, regular expressions on matching and pattern matching on arrays, but regular expressions cannot do it in JAVA on a mysql database. Secondly, when it comes to Mysql database, hashing is mostly done on a text file. Is it possible to get multiple results for the word "get" with this kind of function in java: rs.next()

Because I don't know how Hashing a dictionary from mysql database is possible. I just want a solution.

My problem is getting multiple answers from Mysql database for different words through the scanner in java. Not just for the word get!

  • What in the world does hashing have to do with such a problem? Do you perhaps mean "indexing"? – John Bollinger Sep 25 '15 at 21:37
  • My problem is getting multiple answers from Mysql database for any word that is inputted through the scanner in java. – Meanrhei Nqiofg Sep 25 '15 at 21:39
  • This sounds like a problem that would best be solved in the database, rather than in Java. Either way, though, it's unclear how to recognize the results you want to retrieve. – John Bollinger Sep 25 '15 at 21:40
  • I want to enter any word to check, and then offer different suggestions by retrieving them from the database in the second column – Meanrhei Nqiofg Sep 25 '15 at 21:43
  • In order to obtain multiple results, you need to perform a query that will return multiple results (rows). Are you performing such a query? – John Bollinger Sep 25 '15 at 21:44
  • Not just the word get, because if it was for one word only I would do it from the database. But if it is multiple words, I need to find a way to do it. – Meanrhei Nqiofg Sep 25 '15 at 21:44
  • I have been searching online but could not find something that could fit the bill. – Meanrhei Nqiofg Sep 25 '15 at 21:45

1 Answers1

0

I interpret your question to say that given a user-provided word, you want to look up that word among the wrongwords in your table grammarchecker, and obtain all of the associated rightwords. That might look like this:

List<String> findAlternativeWords(String userWord, Connection conn) throws SQLException {
    List<String> words = new ArrayList<String>();
    PreparedStatement ps = conn.prepareStatement(
            "select rightwords from gammarchecker where wrongwords = ?");

    try {
        ResultSet results;

        ps.setString(1, userWord);
        results = ps.executeQuery();

        while (results.next()) {
            words.add(results.getString(1));
        }
    } finally {
        ps.close();
    }

    return words;
}

That assumes you already have an open database connection that you will pass to the method. Note that I suggest a PreparedStatement here, as opposed to building an SQL statement directly in a string, because user-provided data must be handled very carefully. Otherwise, you open yourself (in this case) to an SQL injection attack. Inserting the user's word via a prepared statement parameter protects you from that.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157