G97
G97

Reputation: 68

Creating a database and tables in MySQL using a JDBC application

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!

Upvotes: 1

Views: 2674

Answers (2)

hurricane
hurricane

Reputation: 6724

Actually you can do it with Hibernate JPA.

Create a Model

@Entity
@Table(name = "USER")
public class User{

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id
  @Column(name="FIRST_NAME")
  private String firstName
  @Column(name="LAST_NAME")
  private String lastName
}

Define hibernate auto creation

in applicationContext.xml (spring-servlet.xml or hibernate.cfg.xml) add that:

         <properties>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/DB"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.password" value=""/>
            // That property create tables and key options.
            <property name="hbm2ddl.auto" value="create"/>
        </properties>

Start Server

When you start server hibernate will create your tables or update your columns.

Upvotes: 0

Orden
Orden

Reputation: 611

To create tables, you should use the "CREATE TABLE" SQL syntax. This can be done on the database management program but also in JDBC.

  stmt = conn.createStatement();

  String sql = "CREATE TABLE password" +
               "(passwordId INTEGER not NULL, " +
               " password VARCHAR(20), " + 
               " PRIMARY KEY ( passwordId ))"; 

  stmt.executeUpdate(sql);

Upvotes: 1

Related Questions