I will try to explain my title a little better but first a little background to what I'm trying to do here. I'm working on a game where I will need to search a local database for a word that the user has given the program. This is done so that it can verify that the user has actually used a word that fits the rules of the game. In this bit of code I would want to search a database of countries to verify that the user has actually given the game a country and not a random word.
Since I'm fairly new to java and just beginning to understand SQLite and JDBC I searched the internet for how to query my database and I found some useful bit of code that I tweaked to my liking. Now the problem is that, from the beginning, the main-method would establish a connection to the database and just pick out every single thing from the database. Not really my intention so I thought I could ask the user for some input, store that in a string (beneath referred as 'word') and the use the SQL command SELECT * FROM [table] WHERE [column] LIKE [condition].
When I then put my newly created string 'word' as condition and try to search the database, the program does not seem to recognize word for what it is or even use it at all. Intellij marks 'word' in gray, so supposedly it is never used.
How come that the .executeQuery(SQL-command) won't recognize that it should use word as parameter? How can I make this work so that the user can decide what to search for without explicitly typing the SQL-commmand themselves?
Code:
package SQL;
//STEP 1. Import required packages
import javax.swing.*;
import java.sql.*;
public class SQLTests {
public static void main(String[] args) throws Exception {
String word = JOptionPane.showInputDialog(null, "Input Country name:",
"Country search", 1);
Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection("jdbc:sqlite:countries.db");
Statement stat = connection.createStatement();
ResultSet rs = stat.executeQuery("select * from Country where name like word;");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
rs.close();
connection.close();
}
}