G'day,

I just want to ask how could i run a set up for a MySQL DB through Java.
For example, when installing the application, I need to check if MySQL is installed, and if not, install it.

OR, how would I go about checking for a registry for mysql ?

How would I go about doing this, or if anyone has resources it would be much appreciated.


Regards

Recommended Answers

All 5 Replies

This is possible, but Java is designed to be platform independent, and what you are describing is totally platform-dependent. I'm sorry to say this, but that's a job I would never recommend java for. Why not use one of the various tried and proven installers that are out there - they will have everything you need.

All I can say is, in the distribution instructions/readme tell the user that a mysql needs to be present.

A better idea, if the DB is not suppossed to get all that large, would be to distribute derby.jar with your application and simply create the DB on the fly.

thanks guys,

James, i clearly see your point, but for the sake of discussion, if i were to develop a software package solely for say, windows OS, how would i go about checking the registry to see if mysql is present for example.

masijade, I had thought about derby.jar but the issue is that the database could very easy get to a reasonable size to which derby isn't quite suited. I had also thought about adding the notes in the distribution instructions, but I wanted to take all the hassle out of having the end user downloading this, that and the other just so MY software will work. I wanted to take that guess work out and streamline the entire process for the end user. Thanks again for your input.

I agree with Masijade (which is usually the best thing to do anyway) - if you have a free choice of DB have another look at Derby - you're unlikely to run a nation-wide network of cash dispensers on it, but it does scale well into the mid-range.

If you really really really have to get into the Windows registry I know of two solutions, both of which are undesirable.
Firstly, on the web you will find a number of variants on some code that uses Java Reflection to hack into the private methods that the Preferences API uses to access the registry. The obvious problem with this is that private methods in the API have no published specs, they are private for a good reason, and can/may change totally with any point maintenance release. I personally wouldn't touch this with a very long stick, but here's a typical example: http://www.rgagnon.com/javadetails/java-0630.html
Secondly, you can use the public interface of reg,exe to run it in a Process and pipe the results back to your Java code. This only uses published interfaces, and so is more likely to remain stable, but it's still a hack. Here's a bit of code I used when I had to do this:

public static String queryValue(String key, String value) {
		// returns null if key/value not found (or other error, eg not Windows)

		if (!System.getProperty("os.name").toUpperCase().contains("WINDOWS")) {
			return null;
		}

		try {
			Process p = new ProcessBuilder("reg.exe", "QUERY", key, "/v", value)
					.redirectErrorStream(true).start();
			// (reg.exe requires parameters to be already tokenised)
			InputStream is = p.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));

			String line;
			// scan output & parse interesting lines...
			while ((line = br.readLine()) != null) {
				if (line.contains(value)) {
					// this line displays the value we are looking for
					int i = line.indexOf("REG_SZ");
					if (i >= 0) {
						String result = line.substring(i + 7).trim();
						is.close();
						return result;
					}
				}
				if (line.contains("Error")) {
					if (line.contains("unable to find")) {
						System.err.println("reg.exe error searching for: "
								+ key + " " + value);
					} else {
						System.err.println(line);
					}
					is.close();
					return null;
				}
			}
			System.err.println("ERROR: unable to parse output from reg.exe");
			System.err.println("while searching for: " + key + " " + value);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

Either way, my vote goes with Derby.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.