Jump to content
  • Advertisement
Sign in to follow this  
TomButcher

[java] MySQL and java Queries (ho ho)

This topic is 4863 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!