I am trying to make a word game and obviously I need a word list to check the correctness of the words that people enter, so I found a .txt file containing all the words I could ever want and wrote some code to have the .txt file loaded line by line into a database using Java Database Connectivity, but now I am trying to query the database to see if it can retrieve information from the database. If it isn’t able to retrieve anything, then it will tell the user that it is not a word, but if it is able to retrieve info, that means the word is in the database and therefore is a real word… My program doesn’t work though, it keeps giving me the following error when I run it at the point in time that it tries to query the database…
ERROR: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
Here is the code I am using, any ideas?
/*
* The purpose of this class is to test that I can connect to the database
* I created with WordDBMaker.java and WordDBLoader.java and query the database
* to check that words the user enters are in the database and therefore valid
* words.
*/
/**
* @author Josh Branchaud
* @version 12/30/2008
*/
//*************************************
//***** Imported Classes **************
//*************************************
// added for reading user input from the console
import java.util.Scanner;
// added for connecting to and querying the database
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class WordDBTester
{
// create a volatile boolean variable for knowing when to end execution
public volatile boolean endExecution = false;
// the main method for this class
public static void main(String[] args)
{
WordDBTester WDBT = new WordDBTester();
// Scanner called in for reading user input from the console
Scanner in = new Scanner(System.in);
// try/catch block for connecting to the database and querying it.
try
{
// specifying the driver we want to use for database connection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// String variable holding the location of the database
String DBLocation = "jdbc:odbc:WordList1";
// connect to the database found at DBLocation
Connection DBConnection = DriverManager.getConnection(DBLocation);
// system message for confirmation of connection to the database
System.out.println("Successfully connected to " + DBLocation);
Statement s1 = DBConnection.createStatement();
while(!(WDBT.endExecution))
{
System.out.println("Type 'quit!' if you would like to exit, otherwise");
System.out.print("enter the word you would like to check: ");
String inputWord = "";
inputWord = in.nextLine();
System.out.println("");
if(inputWord.compareTo("quit!") == 0)
{
WDBT.endExecution = true;
break;
}
System.out.println("The word you are checking for is " + inputWord);
String wordQuery = "SELECT * FROM WordList1 WHERE Word = " + inputWord;
System.out.println("The query looks like " + wordQuery);
ResultSet rs = s1.executeQuery(wordQuery);
int resultCount = 0;
String thisWord = "";
int pointValue = 0;
while(rs.next())
{
resultCount++;
thisWord = rs.getString("Word");
pointValue = rs.getInt("Points");
}
if(resultCount == 0)
{
System.out.println("Sorry, but " + inputWord + " is not a word.");
}
else
{
System.out.println(thisWord + " is a word and it is worth " + pointValue + " points.");
}
}
// disconnect from the database
DBConnection.close();
// confirmation message of the disconnection from the database
System.out.println("Disconnection successful!");
}
catch(ClassNotFoundException CNFError)
{
System.out.println("ERROR: " + CNFError);
}
catch(SQLException SQLError)
{
System.out.println("ERROR: " + SQLError);
}
catch(Exception Error)
{
System.out.println("ERROR: " + Error);
}
}
}