I just need clarification on whether the following design breaks object-oriented design best practice (in relation to implementing a MySQL database). This is not Java EE scale but rather second-year undergrad work and all I could find in Oracle's tutorials are design guidelines for large-scale databases with Java such as Data Access Object
I have a class that handles loading the JDBC driver and MySQL Database. It has the Connection and Statement objects defined as static
/** Enables a connection to the chessleaguedb MySQL database
* @author Erdi Rowlands
*/
public class DatabaseConnection
{
private Console console; // needed for relevant method to mask console input
private Scanner keyboard; // reads user input
private String user; // MySQL user account
private String pass; // MySQL account password
private String host; // MySQL host
static Connection conn; // application needs to communicate with JDBC driver
static Statement st; // issuing commands against the connection is reqiured
/* When instantiated the JDBC driver attempts to load */
public DatabaseConnection()
{
this.loadDriver();
}
public void loadDriver()
{
try
{
Class.forName ("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println("Could not load the driver");
}
}
public void connectToDatabase()
{
try
{
this.readLogin();
// prompts user to enter login info to console
this.conn = DriverManager.getConnection
("jdbc:mysql://"+host+":3306/chessleaguedb", user, pass);
System.out.println("\nSuccessfully connected to database: "
+ "'chessleaguedb'");
}
catch (SQLException ex)
{
Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
I have a class that is used to create and populate database tables:
/** Enables the creation and population of the MySQL database 'chessleaguedb' tables
* @author Erdi Rowlands
*/
public class DatabaseTables
{
public DatabaseTables()
{
}
public void createPlayerTable()
{
try
{
DatabaseConnection.st = DatabaseConnection.conn.createStatement();
DatabaseConnection.st.executeUpdate("CREATE TABLE IF NOT EXISTS"
+ "(PlayerName VARCHAR(30)PRIMARY KEY,"
+ "DateOfBirth DATE,"
+ "FIDERating tinyint,"
+ "ClubName FOREIGN KEY fk_club(Clubname) REFERENCES club(ClubName)");
// Create Actor table
}
catch (SQLException ex)
{
Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Am I breaking OOD best practice by using those static objects in the second class so as to carry out database commands? If not, I'll continue making classes that deal with stored procedures etc. and will be happy that I am not doing something wrong. I do not want to have everything in the main method naturally, and can't see any other way around this other than loading the JDBC driver and Connection in each class that needs them (seems redundant).