Cam
Cam

Reputation: 1

Netbeans with MySql - Database not working with other computers

I'm having an issue with Netbeans and a database from MySql. I have the proper libraries for the connector and such because it works completely fine on the home computer (which has the database.sql file on it). However when I transfer the 'dist' folder which contains the executable .jar file and the lib folder (containing the database connector) and run it on a different computer, the commands I had for selecting items from the database are completely blank. Does anyone have a solution? This is for a computer programming class and my teacher is the one who is making us use MySql for this project yet I've heard that it requires a local server on the new computer, which would render the program I have useless. She wouldn't give us these guidelines if it weren't possible, so if there is anyone with a proper solution, I'd greatly appreciate it.

Upvotes: 0

Views: 840

Answers (1)

skytreader
skytreader

Reputation: 11707

The problem is that you hard coded the DB path.

DriverManager.getConnection("jdbc:mysql://localhost:3306/chutesandladders", "root", "password");

The best approach is to have the arguments to this call configurable. That is, read from a config file or take it in as command-line parameters.

Example Assuming we go the config file route (read the doc for Java's Properties class I linked above), and that you have properly loaded a Properties object into a variable cfgFile your code snippet above then becomes:

DriverManager.getConnection(cfgFile.getProperty("dbconstr"), cfgFile.getProperty("dbuser"), cfgFile.getProperty("dbpass"));

Far from the cleanest design but should do the trick.

When working with Properties, you need to also know how to read a file/open file InputStream objects.

Using command line parameters is a bit simpler but that is left as an exercise to the reader.

Upvotes: 1

Related Questions