4

I'm currently in the process of learning Java and swing and in doing so am trying to create a desktop app.
As part of this app I have set up a mysql database which the app is connected to, however I'm uncertain as to how this would work if I was to distribute the app for other users, how would I create a database that they are able to use on their system if they don't have mySQL installed or the database initiated.
Help with this would be much appreciated.

Kyle Byrne
  • 85
  • 1
  • 8

4 Answers4

1

You should use embedded database. I would not recommend MySQL for commercial applications, because it is very expensive. Try to use HSQLDB - very fast and do not has memory leaks (at least I didn't noticed).

Here is an implementation example:

private static Connection conn;

/**
 * Gets database connection.
 *
 * @return the database connection.
 * @throws SQLException if database error occurs.
 */
public static Connection getConnection() throws SQLException {

    if (conn == null || conn.isClosed()) {
        conn = DriverManager.getConnection("jdbc:hsqldb:file:data", "somedb", "");
        // conn.setAutoCommit(false);
    }
    return conn;
}

Make sure you have added hsqldb.jar file to your libraries.

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
  • Do you mean `Enterprise Edition` by *MySQL for commercial applications*? – Aleksandr M Jul 08 '15 at 19:44
  • I have somewhere participated in the discussion about using MySQL for commercial applications - and found that for small projects it is too expensive. So switched to HSQLDB - which is great. Also tried JavaDB/Derby - but experienced memory leaks. Not sure at present moment about MySQL licensing, you should check yourself. – Ernestas Gruodis Jul 08 '15 at 19:50
  • There is `Community Edition` which is free. So how can it be *too expensive*? – Aleksandr M Jul 08 '15 at 19:52
  • You mean `Community Edition` is OK for commercial applications? It would be great also I think in such case. – Ernestas Gruodis Jul 08 '15 at 19:56
  • http://stackoverflow.com/q/1921437/1700321 and http://www.xaprb.com/blog/2009/02/17/when-are-you-required-to-have-a-commercial-mysql-license/. – Aleksandr M Jul 08 '15 at 20:01
  • Maybe here is the answer: "OEMs, ISVs and VARs that want the benefits of embedding commercial binaries of MySQL software in their commercial applications but do not want to be subject to the GPL and do not want to release the source code for their proprietary applications should purchase a commercial license from Oracle. Purchasing a commercial license means that the GPL does not apply, and a commercial license includes the assurances that distributors typically find in commercial distribution agreements." – Ernestas Gruodis Jul 08 '15 at 20:12
1

I would suggest looking at SQLite instead of mySQL. It sounds like you need a standalone database that does not require a server to run. I've done both Java (and Android), Swing, as well as C#. I found that SQLite was the best solution to have a database on the different platforms and not install a server.

SteveFerg
  • 3,466
  • 7
  • 19
  • 31
  • Would it be simple to convert from the mySQL i'm currently using to SQLite? – Kyle Byrne Jul 08 '15 at 19:43
  • I found it to be very simple, though there is a learning curve like with anything else. – SteveFerg Jul 08 '15 at 19:45
  • For whatever it's worth, we've also used SqlLite successfully with Borland Delphi (now Embarcadero XE). The product was a distributed bowling application. The main database was SQL Server. But each pair of lanes had a SQLLite database to maintain a local backup of names and scores (in case the connection to MSQL ever went down). – paulsm4 Jul 08 '15 at 22:32
1

You have three choices:

  1. Each desktop will have it's own, private database.

  2. Each desktop talks to one, remote database server.

  3. Each desktop talks to a web front-end to the remote database server.

"Option 3" is the most common scenario. One option is a "web app" (in which case you don't need the "Swing desktop" app at all). Another option is a "thick client Java applet". You can use Swing, most of the processing will be local ... but your Swing applet will communicate with the remote database for database access. You might consider a REST interface.

If you really want option 1 (app and database completely local to the desktop), then I'd definitely go with an embedded database. Sqlite is an excellent choice. In fact, Android uses Sqlite. HSQLDB or even MS-Access can also be good choices.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

Regarding your problem, I'd ask if you're sharing the information of the database among the users of your application. I mean if you need the data centralized in one database the users can access through the desktop Swing application.

If so, then you'd need another application that would provide the information that is stored in the database (you can use Web Services at this point), then you'd just need to implement Web Service client code to interact with your database through the Swing application.

Now, if you want independent application with a database per user. Then you can go for many options such:

Hope this can give you orientation in what you need to implement.

Happy coding.

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51