
October 25th, 2012, 03:20 PM
|
|
|
|
Can you help me optimize this code?
Hey all, what I am trying to do is retrieve a version number from a database. Ideally, the version number would be a String in the form of "1.0" or "1.1", but other valid entries are "1.0.6" or "1.1.12.3". Additionally, the code needs to gracefully handle strings that don't fit that format at all, such as "noVersionAvailable" or "asjfdjasdg".
I've written some code that takes care of this, but I'm really not pleased with my solution. It just looks very sloppy and ugly. Can anyone of you java experts suggest a cleaner way of taking care of this problem?
Code:
//determine version number
if (connectionValid) {
Statement getVersion = conn.createStatement();
String query = "SELECT * FROM <tableName> WHERE [key]='version'";
ResultSet rs = getVersion.executeQuery(query);
if (rs.next()) {
String version = rs.getString("value");
if (version != null) {
Scanner scanner = new Scanner(version);
int[] versionNumber = {0, 0};
int i = 0;
while (scanner.hasNextInt() && i < 2) {
versionNumber[i] = scanner.nextInt();
}
if ((versionNumber[0] != 0) && (versionNumber[1] != 0)) {
ip.setVariable(EXISTING_DB_VERSION_IA_VARIABLE, versionNumber[0] + ":" + versionNumber[1]);
}
}
}
}
|