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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user