Added more checks for failed database connections.

Should prevent unnecessary null pointer exceptions if anything goes wrong.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han
2015-03-07 20:39:27 -05:00
parent 8c24ef97c7
commit c6041d8cba
3 changed files with 92 additions and 36 deletions

View File

@@ -20,6 +20,10 @@ package itdelatrisu.opsu.db;
import itdelatrisu.opsu.ErrorHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Database controller.
*/
@@ -50,4 +54,19 @@ public class DBController {
OsuDB.closeConnection();
ScoreDB.closeConnection();
}
/**
* Establishes a connection to the database given by the path string.
* @param path the database path
* @return the Connection, or null if a connection could not be established
*/
public static Connection createConnection(String path) {
try {
return DriverManager.getConnection(String.format("jdbc:sqlite:%s", path));
} catch (SQLException e) {
// if the error message is "out of memory", it probably means no database file is found
ErrorHandler.error(String.format("Could not connect to database: '%s'.", path), e, true);
return null;
}
}
}