· 9 years ago · Oct 31, 2016, 02:08 AM
1/** Enables a connection to the chessleaguedb MySQL database
2 * @author Erdi Rowlands
3 */
4public class DatabaseConnection
5{
6 private Console console; // needed for relevant method to mask console input
7 private Scanner keyboard; // reads user input
8 private String user; // MySQL user account
9 private String pass; // MySQL account password
10 private String host; // MySQL host
11 static Connection conn; // application needs to communicate with JDBC driver
12 static Statement st; // issuing commands against the connection is reqiured
13
14 /* When instantiated the JDBC driver attempts to load */
15 public DatabaseConnection()
16 {
17 this.loadDriver();
18 }
19
20 public void loadDriver()
21 {
22 try
23 {
24 Class.forName ("com.mysql.jdbc.Driver");
25 }
26 catch (ClassNotFoundException e)
27 {
28 System.out.println("Could not load the driver");
29 }
30 }
31
32 public void connectToDatabase()
33 {
34 try
35 {
36 this.readLogin();
37 // prompts user to enter login info to console
38 this.conn = DriverManager.getConnection
39 ("jdbc:mysql://"+host+":3306/chessleaguedb", user, pass);
40 System.out.println("nSuccessfully connected to database: "
41 + "'chessleaguedb'");
42 }
43
44 catch (SQLException ex)
45 {
46 Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
47 }
48 }
49
50/** Enables the creation and population of the MySQL database 'chessleaguedb' tables
51 * @author Erdi Rowlands
52 */
53public class DatabaseTables
54{
55 public DatabaseTables()
56 {
57
58 }
59
60 public void createPlayerTable()
61 {
62 try
63 {
64 DatabaseConnection.st = DatabaseConnection.conn.createStatement();
65 DatabaseConnection.st.executeUpdate("CREATE TABLE IF NOT EXISTS"
66 + "(PlayerName VARCHAR(30)PRIMARY KEY,"
67 + "DateOfBirth DATE,"
68 + "FIDERating tinyint,"
69 + "ClubName FOREIGN KEY fk_club(Clubname) REFERENCES club(ClubName)");
70 // Create Actor table
71 }
72 catch (SQLException ex)
73 {
74 Logger.getLogger(DatabaseConnection.class.getName()).log(Level.SEVERE, null, ex);
75 }
76 }
77}