I am new to MySQL and thinking to use MySQL for my new Java Project which creates a database on the local client machine and uses it to perform all data operations. But before proceeding with MySQL I'm in a bit of doubt that I have MySQL server installed so it is working fine on my machine but what if I give this software to an end user which do not have it installed or with some other username and password. Like I have following code for creating a database, it will most probably not work on any target client machine. I know its bit of stupid question but i'm unable to think of a solution for it.

public class CreateDatabase {
  public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;
    try {
      Class.forName("org.gjt.mm.mysql.Driver").newInstance();
      String url = "jdbc:mysql://localhost/mysql";
      
      connection = DriverManager.getConnection(url, "root", "root");

      statement = connection.createStatement();
      String dbSQL = "CREATE DATABASE mynewdb";
      statement.executeUpdate(dbSQL);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (statement != null) {
        try {
          statement.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException e) {
        } // nothing we can do
      }
    }
  }

}

Recommended Answers

All 3 Replies

Try using H2 or JavaDB instead. Both are embedded databases that do not need to be separately installed.

Member Avatar for iamthwee

Wow I never knew that.

Try using H2 or JavaDB instead. Both are embedded databases that do not need to be separately installed.

Thanks, javadb is working great for my requirements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.