• Advertisement
Sign in to follow this  

[java] MySQL and java Queries (ho ho)

This topic is 4683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

right im doing a scrabble game and i need to perform queries on a database of some sort to check that the words are legit. I downloaded a dictionary that someone pointed me to but i haven't really got the faintest idea how to go about creating and utilising a database. ( i do know how to perform queries in mysql) but have no other knowledge on how to set one up and connect to it many thanks

Share this post


Link to post
Share on other sites
Advertisement
ok i read through that but im still not 100% about actually creating the database itself (probably the wrong forum :/) but i figured someone would know how to? i have mysql server running if thats any help

Share this post


Link to post
Share on other sites
The MySQL manual should be able to help you through most of the setup you need to do. If you just want a quick set of examples this is what I used for one of my apps: db.sql.

I also have some Java code DBManager.java which is fairly generic and you can adapt it for your use if you like. I suggest looking over the tutorial posted above first.

Edit:
If you're on unix you can just run one of the scripts like this 'mysql < db.sql'. I'm not sure about how the windows version works but I'd assume there's a similar command line client.

Share this post


Link to post
Share on other sites
Here's 2 java classes I started recently. One class handles the connection to the database and retrieving results, the other classes just displays the data in a jtable.

download

Share this post


Link to post
Share on other sites
Here's about the simplest possible way to accomplish this without MySQL. I just stuck all the words in a Hashtable. It takes a few seconds to construct the Hashtable, but checks are incredibly fast.


package driver;
import java.io.*;
import java.util.*;

public class Driver {
public static void main(String[] args) {
System.out.print("Loading . . . ");
BufferedReader br;

// construct a new hashtable
// this will be your "database" of legal words
// string hashing functions are very effective, and
// lookup times will be O(1).
Hashtable table = new Hashtable();

// read in the list of legal words from a file
// there are some good files at http://wordlist.sourceforge.net/
// you can use for this

try {
br = new BufferedReader(new FileReader("C:\\eclipse\\workspace\\stuff\\driver\\wordlist.dat"));
String line = "";
while((line = br.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(line, " ", false);
line = tokenizer.nextToken();
table.put(line, line);
}
} catch(IOException e) {
System.out.print(e);
}
System.out.print("done.\n");
br = new BufferedReader(new InputStreamReader(System.in));
String cont = "yes";

// ask a user to enter a word
// program will inform the user whether or not the word is legal
while(cont.equalsIgnoreCase("y") || cont.equalsIgnoreCase("yes")) {
System.out.print("Type in a word to see if it exists: ");
String word = "";
try {
word = new String(br.readLine());
} catch(Exception e) {
System.out.print(e);
}

// this is where you look to see if the word is
// in the hashtable
String result = (String)table.get(word);
if(result != null)
System.out.print("Result: \"" + result + "\" exists in database.\nAgain? ");
else
System.out.print("Result: \"" + word + "\" doesn't exist in database.\nAgain? ");
try {
cont = new String(br.readLine());
} catch(IOException e) {
System.out.print(e);
}
}
}
}




[Edited by - Kevinator on April 26, 2005 8:51:25 PM]

Share this post


Link to post
Share on other sites
I recommend using a dictionary file or files rather than an SQL database.

Unless your dictionary file is very large, you can just read the words into memory and stick them in a HashMap (don't use Hashtable).

Jon

Share this post


Link to post
Share on other sites
Quote:
Original post by TomButcher
so you're saying a hash map would be more efficient?


Yes. If I remember rightly Hashtables are thread safe (synchonized), which means they can handle being accessed by multiple threads at one time. However, this has a performance cost, and you don't really need the Hashtable to be synchronised in a scrabble game (I assume). This is unlike the rest of the Collections framework, which doesnt as a rule contain synchronized classes - you have to do the synchronization yourself normally.

If you really need a HashMap to handle access by multiple threads you should use ConcurrentHashMap (which synchronizes at the bucket level, rather than the whole collection) - I can point you at the code for the class if you need it.

Share this post


Link to post
Share on other sites
OK then. The class source code is here if you need it. Read the licence - its written by Doug Lea and is not available for commercial use (I think).

I believe this class, or something like it, is in the J2SE 5.0 platform, Tiger, or whatever its called (haven't played with it yet).

Remember, you only really need to use this class if multiple threads will be WRITING to the Map. If you initialise the map once at the start of the game, and thereafter your game is only ever performing READ (i.e. get) operations on it (no matter how many threads are reading at the same time), then you can just use HashMap.

Good luck,
Jon

[Edited by - jonpolly99 on April 27, 2005 5:29:48 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
scrabble? dictionary? hashmap? NOOOOOOOO! (actually, it's a fair enough way to do it, in that it works, but it's not the most appropriate way)

Learn about lexical searchin, and then do it in a list :P.

Fast search trivial to implement:
1. put all words in lexographical order in a list
2. to check a word, jump to list.get( list.size()/2 )
3. if word is after that, then jump ahead half the remaining indexes. If it's before that, jump back half the remaining indexes.
4. Repeat until you hit the word, or your jumps are <

Share this post


Link to post
Share on other sites
What have you been smoking? That's a helluva lot slower than hashing...

Do you not realize that hashtable lookups are O(1)? With an externally chained hashtable with a good hashing function, your average number of lookups is going to be only 2 or 3.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
scrabble? dictionary? hashmap? NOOOOOOOO! (actually, it's a fair enough way to do it, in that it works, but it's not the most appropriate way)

Learn about lexical searchin, and then do it in a list :P.

Fast search trivial to implement:
1. put all words in lexographical order in a list
2. to check a word, jump to list.get( list.size()/2 )
3. if word is after that, then jump ahead half the remaining indexes. If it's before that, jump back half the remaining indexes.
4. Repeat until you hit the word, or your jumps are <


That sounds like a binary search .. I think the HashMap is more efficient.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Using a HashMap may be ok, and it may not.

If all you're doing is having the program double-check legality of the words that are entered, and all words are being entered by humans, a HashMap is fine. Not quite optimal, but FAR easier than building a trie (not a typo, a specialized data structure for looking up a list of words) for it.

On the other hand, if you're building an AI to play Scrabble, using a HashMap is bad. Using it to look up legality of words requires generating words by permutation first, and generating and discarding a huge number of permutations is not efficient. In this case, using a trie to limit the permutation to only even generate valid words is ridiculously more efficient.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement