I've created a program in Java for a school project. I created the database and tables using MySQL Workbench, but now I need my program to work on other computers and therefore I need it to check if the database and tables exist, and if not create them on its own.
This is the relevant code in my driver class as it stands:
private static Connection conn;
static
{
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException exc)
{
javax.swing.JOptionPane.showMessageDialog(null, "Oops! Something seems to have gone wrong!\n\n" + exc, "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static Connection getMySQLConnection() throws SQLException
{
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tournament_manager", "studentCouncil", "****");
return conn;
}
As above, the database I would like to create, if it does not exist, is to be called "tournament_manager". The tables to be created with the database are as follows:
Table 1 Name: password
Column 1: passwordID Type: int(11) Nullable: NO
Column 2: password Type: varchar(20) Nullable: YES Character Set: latin1
Table 2 Name: tournaments
Column 1: tournamentID Type: int(11) Nullable: NO Extra: auto_increment
Column 2: tournamentName Type: varchar(20) Nullable: NO Character Set: latin1
Column 3: date Type: date Nullable: NO
Column 4: startTime Type: int(11) Nullable: NO
Column 5: matchDuration Type: int(11) Nullable: NO
Column 6: breakDuration Type: int(11) Nullable: NO
Column 7: numTeams Type: int(11) Nullable: NO
Column 8: numVenues Type: int(11) Nullable: NO
Table 3 Name: teams
Column 1: teamID Type: int(11) Nullable: NO Extra: auto_increment
Column 2: teamName Type: varchar(20) Nullable: NO Character Set: latin1
Column 3: points Type: int(11) Nullable: NO
Column 4: tournaments_tournamentID Type: int(11) Nullable: NO
I am new to stack overflow, so I may have not included all the relevant information or included irrelevant information. In the case that I haven't included relevant details, please ask.
Thank you in advance for the support!
DatabaseMetaDataobject from yourConnectionobject viagetMetaData(). Once you have that object, you can query it for existing tables and create them using standard SQL, if they are not there using yourConnectionobject.