Reputation: 95
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.
Upvotes: 4
Views: 1075
Reputation: 3871
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.
Upvotes: 0
Reputation: 121619
You have three choices:
Each desktop will have it's own, private database.
Each desktop talks to one, remote database server.
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.
Upvotes: 1
Reputation: 3572
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.
Upvotes: 1
Reputation: 8787
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.
Upvotes: 1